0

我已经估计了每个月中的一天的温度值,我希望我可以根据这些值创建一系列预测。所以我有两个向量:一个包含天数,一个包含相应的温度。

day <- c(15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345)
celsius <- c(1.7 , 5.7,  10.7,  15.1, 17.4, 14.7, 8.7, 1.8, -5.0, -8.7, -8.7, -4.2)
dfram <- data.frame(day, celsius)


forecast <- lm(celsius ~ poly(day, 2, raw=TRUE), data=dfram)
forecast2 <- lm(day ~ celsius + I(day^2), data=dfram)

plot(day, celsius)
lines(lowess(celsius ~ day))
lines(day, predict(forecast2), col=2)

plot(day,celsius,col='deepskyblue4',xlab='celsius',main='weather forecast')
lines(day,celsius,col='firebrick2',lwd=1)

我最终得到了一个对我来说看起来可信的情节,但我不明白接下来要做什么来预测单个值,例如 1 月 1 日(第 1 天)或 12 月 31 日(第 365 天)的温度。从理论上讲,我应该forecast2使用 1:365 等一系列值来初始化它,但我找不到这样做的帮助。谁能给我一个提示,好吗?谢谢你。

UPD:不确定这是帽子戏法还是正确的方式,但这就是我的预期:

day <- c(15, 45, 75, 105, 135, 165, 195, 225, 255, 285, 315, 345, 380)
celsius <- c(1.7 , 5.7,  10.7,  15.1, 17.4, 14.7, 8.7, 1.8, -5.0, -8.7, -8.7, -4.2, 1.7)
dfram <- data.frame(day, celsius)


a0 = -0.19 
a1 = -0.04
a2 = 0.01

forecast <- lm(celsius ~ poly(a2*day^2 + a1*day + a0, 6, raw=FALSE), data=dfram)
weather <- predict(forecast, newdata=data.frame(day=1:380))
plot(weather[1:365],col='deepskyblue4',xlab='celsius',main='weather forecast')
lines(weather[1:365],col='firebrick2',lwd=1)
weather
4

0 回答 0