2016-10-10 67 views
0

我想通过使用darch()将模型拟合到非线性回归中。R - 如何用darch()和predict()来拟合非线性回归模型?

这里是我已经做了代码:

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]) 
} 

但是,当我试图让在预测值,它看起来并不像我在等待值:

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 

我想我的脚本返回新的Y坐标,以便我可以绘制由darch()生成的模型,并查看它是否适合我的点分布。

干杯!

+0

你等待什么值?你能更清楚地知道你的期望吗? – MrFlick

+0

我认为他期待回归的价值。那么,你知道输出层通常会返回相对权重。因此,如果你设置了'preProc.targets = T'(参见'darch')参数,你应该为你的'df'得到一个回归线。 – Drey

+0

嗨! 我想返回新的Y坐标来绘制darch()中适合我的点集的模型。 –

回答

0

你的问题很有意思。 我在代码中找不到问题。 但是,您是否尝试过使用更宽的窗口并对样本数据集进行测试?

+0

嗨mg64。 是的,我尝试了两个建议。 干杯! –