5

现在我有一个大数据集,温度一直在上下波动。我想平滑我的数据并绘制所有温度的最佳拟合线,

这是数据:

weather.data  
    date        mtemp   
1   2008-01-01  12.9        
2   2008-01-02  12.9        
3   2008-01-03  14.5        
4   2008-01-04  15.7            
5   2008-01-05  17.0        
6   2008-01-06  17.8    
7   2008-01-07  20.2        
8   2008-01-08  20.8        
9   2008-01-09  21.4        
10  2008-01-10  20.8        
11  2008-01-11  21.4        
12  2008-01-12  22.0        

依此类推...... 直到 2009 年 12 月 31 日

我当前的图表看起来像这样,我的数据适合回归,例如运行平均值或黄土:

在此处输入图像描述

但是,当我尝试将其与运行平均值相匹配时,它变成了这样:

在此处输入图像描述

这是我的代码。

plot(weather.data$date,weather.data$mtemp,ylim=c(0,30),type='l',col="orange")
par(new=TRUE)

谁能帮我一把?

4

1 回答 1

15

根据您的实际数据以及您希望如何对其进行平滑处理,以及为什么要对其进行平滑处理,有多种选择。

我将向您展示线性回归(一阶和二阶)和局部回归(LOESS)的示例。这些可能是也可能不是用于您的数据的良好统计模型,但如果不看到它就很难判断。任何状况之下:

time <- 0:100
temp <- 20+ 0.01 * time^2 + 0.8 * time + rnorm(101, 0, 5)

# Generate first order linear model
lin.mod <- lm(temp~time)

# Generate second order linear model
lin.mod2 <- lm(temp~I(time^2)+time)

# Calculate local regression
ls <- loess(temp~time)

# Predict the data (passing only the model runs the prediction 
# on the data points used to generate the model itself)
pr.lm <- predict(lin.mod)
pr.lm2 <- predict(lin.mod2)
pr.loess <- predict(ls)

par(mfrow=c(2,2))
plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.lm~time, col="blue", lwd=2)

plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.lm2~time, col="green", lwd=2)

plot(time, temp, "l", las=1, xlab="Time", ylab="Temperature")
lines(pr.loess~time, col="red", lwd=2)

另一种选择是使用移动平均线。

例如:

library(zoo)
mov.avg <- rollmean(temp, 5, fill=NA)
plot(time, temp, "l")
lines(time, mov.avg, col="orange", lwd=2)

平滑示例

于 2014-08-13T10:25:13.923 回答