Make certain parameters have positive coefficients in lm ()

I would like to know how to limit certain parameters in lm() order to have positive coefficients. There are several packages or functions (for example, display ) that can make all coefficients, and intersection, positive.

For example, in this example, I would like only x1 and x2 have positive coefficients.

  x1=c(NA,rnorm(99)*10) x2=c(NA,NA,rnorm(98)*10) x3=rnorm(100)*10 y=sin(x1)+cos(x2)-x3+rnorm(100) lm(y~x1+x2+x3) Call: lm(formula = y ~ x1 + x2 + x3) Coefficients: (Intercept) x1 x2 x3 -0.06278 0.02261 -0.02233 -0.99626 

I tried the nnnpls() function in the nnls package, it can easily control the coefficient sign. Unfortunately, I cannot use it because of problems with NA in the data, since this function does not allow NA .

I saw that the glmc() function can be used to apply restrictions, but I could not get it to work.

Can someone let me know what to do?

+7
source share
2 answers

You can use the package fined :

 set.seed(1) x1=c(NA,rnorm(99)*10) x2=c(NA,NA,rnorm(98)*10) x3=rnorm(100)*10 y=sin(x1)+cos(x2)-x3+rnorm(100) DF <- data.frame(x1,x2,x3,y) lm(y~x1+x2+x3, data=DF) #Call: #lm(formula = y ~ x1 + x2 + x3, data = DF) # #Coefficients: #(Intercept) x1 x2 x3 # -0.02438 -0.01735 -0.02030 -0.98203 

This gives the same thing:

 library(penalized) mod1 <- penalized(y, ~ x1 + x2 + x3, ~1, lambda1=0, lambda2=0, positive = FALSE, data=na.omit(DF)) coef(mod1) #(Intercept) x1 x2 x3 #-0.02438357 -0.01734856 -0.02030120 -0.98202831 

If you limit the coefficients x1 and x2 positive, they become zero (as expected):

 mod2 <- penalized(y, ~ x1 + x2 + x3, ~1, lambda1=0, lambda2=0, positive = c(T, T, F), data=na.omit(DF)) coef(mod2) #(Intercept) x3 #-0.03922266 -0.98011223 
+3
source

An old question, but since it still draws attention:

You can use the colf package for colf . He currently offers two non-linear least squares optimizers: nls or nlxb :

 library(colf) colf_nlxb(y ~ x1 + x2 + x3, data = DF, lower = c(-Inf, 0, 0, -Inf)) #nlmrt class object: x #residual sumsquares = 169.53 on 98 observations # after 3 Jacobian and 3 function evaluations # name coeff SEs tstat pval gradient JSingval #1 param_X.Intercept. -0.0066952 NA NA NA 3.8118 103.3941 #2 param_x1 0.0000000 NA NA NA 103.7644 88.7017 #3 param_x2 0.0000000 NA NA NA 0.0000 9.8032 #4 param_x3 -0.9487088 NA NA NA 330.7776 0.0000 colf_nls(y ~ x1 + x2 + x3, data = DF, lower = c(-Inf, 0, 0, -Inf)) #Nonlinear regression model # model: y ~ param_X.Intercept. * X.Intercept. + param_x1 * x1 + param_x2 * # x2 + param_x3 * x3 # data: model_ingredients$model_data #param_X.Intercept. param_x1 param_x2 param_x3 # -0.0392 0.0000 0.0000 -0.9801 # residual sum-of-squares: 159 # #Algorithm "port", convergence message: both X-convergence and relative convergence (5) 

You can set lower and / or upper limits to specify limits as you like for each of the coefficients.

+2
source

Source: https://habr.com/ru/post/1208113/


All Articles