0

I have estimated temperature values for every middle day of the month, and I wish I could create an array of predictions based on those values. So I have two vectors: one contains days and one contains corresponding temperatures.

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)

I end up having a plot which looks credible to me, but I don't understand what to do next to predict a single value, e.g. tenperature on January, 1 (day 1) or December, 31 (day 365). In theory, I should ititialize that forecast2 with a range of values like 1:365, but I can't find help on doing so. Could anyone give me a hint, please? Thank you.

UPD: Not sure if that's a hat trick or a proper way, but that's what I expected:

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
  • `predict(forecast, newdata=data.frame(day=1:365))` – dww Jan 20 '20 at 16:39
  • @dww Thanks a lot! That solution looks very elegant. You helped me move things forward. But now I see that the curve does bad job fitting to the initial values. And I can't see why the forecast2 is not used anywhere once it's been declared, as if it doesn't play any role in the output. – Vlas Trunov Jan 20 '20 at 17:14

0 Answers0