# create an example timeseries
n = 100
set.seed(1234)
datout<-makedynamics_general(n = n+2,
pdet=log(c(0.8,1)),
proc = -2.5,
detfun = detfun0_sin)
plot(datout$true, type = "l") # plot timeseries
Y = datout$true # extract true values
# run s-mapping
sout = S_map_Sugihara1994(Y = Y, E = 2, theta = 0.5)
s_coef = process_scof(sout$C) # process coefficients from the S-mapping output
# find best E/theta
fitout = data.frame(E = 1:5, theta = NA, RMSE = NA)
for(i in 1:nrow(fitout)) {
E = fitout$E[i]
Ytmp = Y[-c(1:E)]
optout = optimize(f = function(x) {S_map_Sugihara1994(Ytmp, E, x)$RMSE}, interval = c(0,10))
fitout$theta[i] = optout$minimum # get best theta for given E
fitout$RMSE[i] = optout$objective # get error
}
ps = which.min(fitout$RMSE)
E = fitout$E[ps] # get best E
theta = fitout$theta[ps] # get best theta
X = makeblock(Y, E) # get X for analysis
Y = Y[-c(1:E)] # trim NA values (corresponding to positions in X)
X = X[(E+1):nrow(X),] # trim NA values
sout = S_map_Sugihara1994(Y = Y, E = E,
theta = theta, X = X) # run S-mapping for best paramter combination
sout$R2 # look at R-squared
# check fit
plot(sout$Y_hat, Y)
abline(a=0, b=1, lty=2)
Run the code above in your browser using DataLab