Predict sinusoidal noise values

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 )?
+4
source share
1 answer
  • The simulated data is strange because all the errors that you add to "true" y are greater than 0. ( runif creates numbers on [0,1] , not [-1,1] .)
  • The problem disappears when the term interception in the model is resolved.

For instance:

 gam.object2 <- gam( yn ~ s( x )) p2 <- predict( gam.object2, data.frame( x=201:210 )) points( 1:211, c( fitted( gam.object2 ), p2), col="green") 

The reason for the systematic underestimation in the model without interception may be that gam uses the sum limit to zero for the estimated smooth functions. I think paragraph 2 answers your first and second questions.

The third question needs clarification because the gam object is not data.frame . The two types of data are not mixed.

A more complete example:

 slope = 0.55 amplitude = 0.22 frequency = 3 noise = 0.75 x <- 1:200 y <- (slope * x / 100) + (amplitude * sin( frequency * x / 100 )) ynoise <- y + (noise * runif( length( x ) )) gam.object <- gam( ynoise ~ s( x ) ) p <- predict( gam.object, data.frame( x = 1:210 ) ) plot( p, col = rgb( 0, 0.75, 0.2 ) ) points( x, ynoise, col = rgb( 0.121, 0.247, 0.506 ) ) points( fitted( gam.object ), col = rgb( 1.0, 0.392, 0.0 ) ) 
+3
source

Source: https://habr.com/ru/post/1333539/


All Articles