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

In R, is there a parsimonious or efficient way to get a regression prediction holding all covariates at their means?

我想知道是否有一种本质上更快的方法可以在不手动指定公式的情况下从回归模型中获取某些协变量值的预测。例如,如果我想根据协变量的平均值对给定的因变量进行预测,我可以这样做:

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

哦...有一个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)