Non-linear regression example

advertisement
Non-linear Regression Example
Effects of room rates on hotel guest #’s
Step 1: Plot the data. (Data for this exercise is in hotelrates.csv)
plot(guests~roomrates,data=hotelrates)
Step 2: Estimate, and plot, assorted non-linear relationships.
First up – piecewise linear.
ratesp<-pmax(hotelrates$roomrates-119,0)
fit2<-lm(guests~roomrates+ratesp,data=hotelrates)
x<-59:203; z<-pmax(x-115,0)
fcast2<-forecast(fit2,newdata=data.frame(roomrates=x,ratesp=z))
lines(x,fcast2$mean,col="blue")
In this case you’re basically estimating two lines – one based on data up to rate=$119, and another for
higher rates. The two lines intersect at $119, and you take the higher lines (i.e., upper envelope) for
your fitted values.
Next up – quadratic.
fit3<-lm(guests~roomrates+I(roomrates^2), data=hotelrates)
fcast3<-forecast(fit3,newdata=data.frame(roomrates=x,ratesp=z))
lines(x,fcast3$mean,col="red")
Note that the “I(…)” argument has R treat the parenthetical expression as an algebraic expression.
Next – semi-log.
fit1<-lm(guests~log(roomrates), data=hotelrates)
fcast1<-forecast(fit1,newdata=data.frame(roomrates=x,ratesp=z))
lines(x,fcast1$mean,col="green")
Finally, cubic spline.
fit4 <lm(guests~roomrates+I(roomrates^2)+I(roomrates^3)+I(ratesp^3),da
ta=hotelrates)
x<-59:203; z<-pmax(x-115,0)
fcast4 <forecast(fit4,newdata=data.frame(roomrates=x,ratesp=z))
lines(x,fcast4$mean,col="purple")
legend("topright",legend=c("log","step","exp","cubic"),lty=1,col
=c(3,4,2,"purple"))
Step 3: Determine the best fit using the CV() function.
CV(fit1)
CV(fit2)
CV(fit3)
CV(fit4)
Step 4: Calculate forecasts of occupancy for a range of room rate values, along with prediction intervals.
x <- seq(50,225,length=200); z <- pmax(x-119,0)
predicted <predict(fit2,newdata=data.frame(roomrates=x,ratesp=z),interval="
prediction")
matplot(x,predicted,type="l",lty=1,lwd=1.5,col=c("thistle","oran
ge","orange"))
x<-seq(50,225,25); z<-pmax(x-119,0)
predicted <predict(fit2,newdata=data.frame(roomrates=x,ratesp=z),interval="
prediction")
matpoints(x,predicted,type="p",pch=23,bg=3)
Step 5: Calculate correlation between roomrates and roomrates squared, then discuss.
cor(hotelrates$roomrates,I(hotelrates$roomrates^2))
Download