简体   繁体   中英

R - How to fit a model to a nonlinear regression with darch() and predict()?

I am trying to fit a model to a non linear regression by using darch().

Here is the code I already have done :

library(darch)

x = seq(-10, 10, 0.2)  

e = function(x) {
  return(rnorm(n = length(x), 0, sqrt(0.1)))  
}

y = function(x) {
  return(0.1*x**2 + sin(3*x))
}

f = function(x) {
  return(y(x)+e(x))
}

plot (x, y(x), type = 'l' )
points (x,f(x), col = 'blue' )
df=data.frame(x,f(x))

model = darch(x, f(x), bp.learnRate = 0.01, darch.isClass = FALSE, layers = c(1,3,10,3,1))


y_pred = rep(NA, length = length(x))
for (i in 1:length(x)){
  y_pred[i] = predict(model, newdata=x[i])  
}

However, when I try to get the values in prediction, it doesn't look like the values I am waiting for:

y_pred

[1] 0.9990583 0.9992179 0.9991225 0.9991374 0.9983996 0.9992237 0.9984544
[8] 0.9989182 0.9990836 0.9992311 0.9992324 0.9987781 0.9984507 0.9988732
[15] 0.9986921 0.9992085 0.9991967 0.9984051 0.9991378 0.9983898 0.9992204
[22] 0.9990966 0.9991587 0.9991696 0.9986069 0.9984612 0.9991242 0.9985350
[29] 0.9992006 0.9987659 0.9984619 0.9991764 0.9991554 0.9984633 0.9984696
[36] 0.9984600 0.9986129 0.9989958 0.9985773 0.9984808 0.9984107 0.9983901
[43] 0.9985983 0.9991682 0.9985102 0.9985920 0.9984892 0.9991399 0.9992277
[50] 0.9992287 0.9989034 0.9984764 0.9991734 0.9983782 0.9990186 0.9990780
[57] 0.9985477 0.9986955 0.9991586 0.9985124 0.9991473 0.9984565 0.9991716
[64] 0.9991440 0.9985235 0.9990681 0.9990596 0.9991788 0.9991864 0.9984982
[71] 0.9990144 0.9991828 0.9991570 0.9984318 0.9983808 0.9991860 0.9988372
[78] 0.9991675 0.9989534 0.9984602 0.9985535 0.9984757 0.9992286 0.9988527
[85] 0.9991903 0.9984676 0.9992313 0.9990943 0.9991951 0.9985524 0.9992317
[92] 0.9991656 0.9991985 0.9983903 0.9992237 0.9985506 0.9992338 0.9990685
[99] 0.9987815 0.9990695

I would like my script to return new Y coordinates so I can plot the model generated by darch() and see if it fit my distribution of points.

Cheers !

Your question is very interesting. I do not find an issue in your code. However, have you tried to use wider windows and test on an out-of-sample set of data?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM