简体   繁体   中英

R- remove a variable from a stored glm lm model

I have a logistic stored model. I want to remove a variable from the formula, without running the model again (I want to keep the rest of the coefficients).

My model:

> class(lr)
[1] "glm" "lm" 

And the formula is:

> lr$formula
target ~ grupoAntig + nu_seguros_1TRUNC + cd_sexo + grupoEdad + 
    vl_limite_aeQU + vl_ltd_6QU + Revolv3 + nu_servicios_1TRUNC + 
    fl_cliente_hit + nu_resumen_6 + fl_rv

I want to remove fl_cliente_hit.

I did:

a<-strsplit(as.character(lr$formula)[3], "+ ")
a<-a[[1]][a[[1]]!=a[[1]][17]]
a<-a[a!=a[16]]
a0<-paste(a, collapse = ' + ')

lr$formula<-as.formula(paste0("target ~ ",a0))

And I get the desired formula:

> lr$formula
target ~ grupoAntig + nu_seguros_1TRUNC + cd_sexo + grupoEdad + 
    vl_limite_aeQU + vl_ltd_6QU + Revolv3 + nu_servicios_1TRUNC + 
    nu_resumen_6 + fl_rv

But I'm not sure if I run a predict function if the model uses the new formula or the previous one. In this case:

predict(lr, train, type=c("response"))

If it keeps the original model, there must be a way to exclude one variable keeping the rest equal. How can I do this?

Late to the party, but I too was hoping there would be an answer to this. What I plan on doing if I can't find another solution is creating a dummy variable with the value of the coef. That way it at least negates the variable:

train$fl_cliente_hit<-coef(lr)["fl_cliente_hit"]
predict(lr, train, type=c("response"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM