R:使用二阶多项式预测天气
我估计了每个月中旬的温度值,我希望我能根据这些值创建一系列预测。所以我有两个向量:一个包含天数,另一个包含相应的温度R:使用二阶多项式预测天气,r,prediction,least-squares,polynomials,poly,R,Prediction,Least Squares,Polynomials,Poly,我估计了每个月中旬的温度值,我希望我能根据这些值创建一系列预测。所以我有两个向量:一个包含天数,另一个包含相应的温度 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
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)
predict(forecast,newdata=data.frame(day=1:365))
@dww非常感谢!这个解决方案看起来非常优雅。你帮我把事情向前推进。但现在我看到曲线对初始值的拟合效果很差。而且我不明白为什么forecast2在声明之后没有在任何地方使用,好像它在输出中没有任何作用。
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