在R中,是否有一种简约或有效的方法来获得回归预测,保持所有协变量?

时间:2016-10-23 02:32:06

标签: r regression

我想知道是否有更快的方法从协同变量的某些值的回归模型中获得预测,而无需手动指定配方。例如,如果我想通过协变量得到给定因变量的预测,我可以这样做:

glm(ins ~ retire + age + hstatusg + qhhinc2 + educyear + married + hisp,
    family = binomial, data = dat)
meanRetire <- mean(dat$retire)
meanAge <- mean(dat$age)
meanHStatusG <- mean(dat$hStatusG)
meanQhhinc2 <- mean(dat$qhhinc2)
meanEducyear <- mean(dat$educyear)
meanMarried <- mean(dat$married)
meanYear <- mean(dat$year)

ins_predict <- coef(r_3)[1] + coef(r_3)[2] * meanRetire + coef(r_3)[3] * meanAge +
               coef(r_3)[4] * meanHStatusG + coef(r_3)[5] * meanQhhinc2 +
               coef(r_3)[6] * meanEducyear + coef(r_3)[7] * meanMarried +
               coef(r_3)[7] * meanHisp

1 个答案:

答案 0 :(得分:3)

哦......有一个predict功能:

fit <- glm(ins ~ retire + age + hstatusg + qhhinc2 + educyear + married + hisp,
           family = binomial, data = dat)
newdat <- lapply(dat, mean)    ## column means
lppred <- predict(fit, newdata = newdat)    ## prediction of linear predictor

要获得预测的响应,请使用:

predict(fit, newdata = newdat, type = "response")

或(更有效地来自lppred):

binomial()$linkinv(lppred)