r - Wrong result from constroptim function -


i'm trying use constroptim optimize sum of square errors linear multiple regression. main equation should d = beta1*xa+beta2*xb+beta3*xc+beta4*xd , d,xa,xb,xc,xd imported .csv file, , betas coefficients want find, minimizing quadratic errors.

so far imported file.csv r, named each column ds,xa,xb,xc,xd, created objfunction= function(beta1,beta2,beta3,beta4)'sum(e²)'=(sum(d) - sum(beta1*xa+beta2*xb+beta3*xc+beta4*xd))^2)

created matrix 'c' , vector 'd' configure constraints should restrict beta's <=0. dont know how find feasible region, although i've used initial values made function work.

here code:

> tabela= read.table("simulacao.csv", header=t, sep= ";") > tabela    d  b  c d.1 1 -1 1 -1  0   0 2  4 0  0  1  -1 3  4 1  0 -1   0 4  0 0  1  0  -1 5 -2 1  0  0  -1 > ds= tabela[,1] > xa= tabela[,2] > xb= tabela[,3] > xc= tabela[,4] > xd= tabela[,5] > simulaf= function(x1,x2,x3,x4) { + ds= tabela[,1] + xa= tabela[,2] + xb= tabela[,3] + xc= tabela[,4] + xd= tabela[,5] + j=sum(ds) + h=sum(x1*xa+x2*xb+x3*xc+x4*xd) + sx=(j-h)^2 + return(sx) +  } > s= function(x)  {simulaf(x[1],x[2],x[3],x[4])} > d= c(0,0,0,0) > c= matrix(c(-1,0,0,0,0,-1,0,0,0,0,-1,0,0,0,0,-1),nrow=4,ncol=4,byrow=t) > constroptim(c(-1,-1,-1,-1),s,null,c,d) $par [1] -0.2608199 -0.8981110 -1.1095961 -1.9274866 

the result expect should be:

$par [1] -0.125 0 -0.5 -0.875 

after researching this, conclusions because i'm using bad initial values, parameterization problem (don't understand why needed) or if it's have programmed incorrectly.

what need fix this?

the formula sum of squared errors is

sum((y - yhat)^2) 

and not

(sum(y) - sum(yhat))^2 

where yhat predicted value.

also, if constraints estimated betas should negative (which bit weird, want them positive never mind), don't need constroptim. regular optim(method="l-bfgs-b") or nlminb work so-called box constraints.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -