Background
Use R to predict the next values ββin a series.
Problem
The following code generates and displays a curve model with some uniform noise:
slope = 0.55 offset = -0.5 amplitude = 0.22 frequency = 3 noise = 0.75 x <- seq( 0, 200 ) y <- offset + (slope * x / 100) + (amplitude * sin( frequency * x / 100 )) yn <- y + (noise * runif( length( x ) )) gam.object <- gam( yn ~ s( x ) + 0 ) plot( gam.object, col = rgb( 1.0, 0.392, 0.0 ) ) points( x, yn, col = rgb( 0.121, 0.247, 0.506 ) )
The model shows the trend, as expected. The problem predicts the following values:
p <- predict( gam.object, data.frame( x=201:210 ) )
Forecasts do not look correct when building:
df <- data.frame( fit=c( fitted( gam.object ), p ) ) plot( seq( 1:211 ), df[,], col="blue" ) points( yn, col="orange" )
Predicted values ββ(starting from year 201) turn out to be too low.
Questions
- Are the predicted values ββshown to be the most accurate predictions?
- If not, how can accuracy be improved?
- What is the best way to combine the two datasets (
fitted.values( gam.object ) and p )?
source share