1
我有我提供以下一個例子,如果簡單線性迴歸一般功能:存儲信息()
x = 1:30
y = 0.7 * x + 32
Data = rnorm(30, mean = y, sd = 2.5);
lin = function(pars = c(grad,cons)) {
expec = pars[1] * x + pars[2];
SSE = sum((Data - expec)^2)
return(SSE)
}
start_vals = c(0.2,10)
lin(start_vals)
estimates = optim(par = start_vals, fn = lin);
## plot the data
Fit = estimates$par[1] * x + estimates$par[2]
plot(x,Data)
lines(x, Fit, col = "red")
所以這是直截了當。我想要的是存儲對最後一組參數的期望,以便一旦完成優化,我就可以查看它們。我一直在使用一個全球集裝箱並試圖如果執行函數填充它嘗試,但它不能正常工作,如
Expectation = c();
lin = function(pars = c(grad,cons)) {
expec = pars[1] * x + pars[2];
Expectation = expec;
SSE = sum((Data - expec)^2)
return(SSE)
}
start_vals = c(0.2,10)
estimates = optim(par = start_vals, fn = lin);
Expectation ## print the expectation that would relate to estimates$par
我知道,這是微不足道的功能之外做的,但我的實際問題(這與此類似)要複雜得多。基本上我需要返回無法追溯計算的內部信息。任何幫助深表感謝。