One can make predictions using predict(fit,newx)
method. But how to predict if I don't have the fit object itself but only the weights of the predictors as vector file and new observations for predictors as a matrix file? The predictors and the outcome both are continuous variables.
Asked
Active
Viewed 494 times
0

Veera
- 861
- 1
- 9
- 18
-
1When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 15 '18 at 20:20
-
1Is this a linear regression or logistic (or something else)? For linear predictions you just multiply the observations by the weights and sum them. For logistic you do the same multiplication and sum but have to transform the result https://en.wikipedia.org/wiki/Logistic_regression#Definition_of_the_logistic_function – Pdubbs Feb 15 '18 at 20:20
-
thanks @Pdubbs thats what I assumed. But wanted to confirm. – Veera Feb 15 '18 at 20:25
-
1@Pdubbs it actually doesn't matter what kind of model, you can do the same procedure with anything in glmnet, since they are all generalized linear models. – thc Feb 15 '18 at 20:26
-
1You can multiply and sum, but to get anything useful to a human out of logistic coefficients you need to transform them with 1/(1+e^(-score)) . Likewise different transforms for things other than OLS – Pdubbs Feb 15 '18 at 20:36
1 Answers
3
You can just use matrix multiplication, which is what glmnet does. In the predict function, it is: as.matrix(cbind2(1, x) %*% coefs)
Example:
library(glmnet)
x=matrix(rnorm(100*20),100,20)
y=rnorm(100)
fit1=glmnet(x,y)
coefs <- coef(fit1,s=0.01) # extract coefficients at a single value of lambda
manaul_pred <- as.matrix(cbind2(1, x) %*% coefs)
pred <- predict(fit1,newx=x,s=0.01)
manual_pred - pred # there is a negligible difference due to numeric precision

thc
- 9,527
- 1
- 24
- 39
-
Will the prediction is valid, if, of all the predictors used in fitting only some are available ? – Veera Feb 15 '18 at 20:28
-
If the missing predictors only have small coefficients, yes. If they have large coefficients, it may not be valid. – thc Feb 15 '18 at 20:31
-
Thanks. Is there a way to assess that while performing the prediction? Can I just compare the means of absolute values of coefficients between predictors present and absent in dataset? – Veera Feb 15 '18 at 20:33