Multiple Logistic Regression with Interaction between Quantitative and Qualitative Explanatory Variables

前端 未结 2 2035
醉话见心
醉话见心 2021-01-18 20:03

As a follow up to this question, I fitted the Multiple Logistic Regression with Interaction between Quantitative and Qualitative Explanatory Variables. MWE is given below:

2条回答
  •  孤街浪徒
    2021-01-18 20:42

    Your link function of choice (\eta= X\hat\beta) has variance for a new observation (x_0): V_{x_0}=x_0^T(X^TWX)^{-1}x_0

    So, for a set of candidate doses, we can predict the expected percentage of deaths using the inverse function:

    newdata= data.frame(Type=rep(x=LETTERS[1:3], each=5),
                        Conc=rep(x=seq(from=0, to=40, by=10), times=3))
    mm <- model.matrix(fm1, newdata)
    
    # get link on link terms (could also use predict)
    eta0 <- apply(mm, 1, function(i) sum(i * coef(fm1)))
    
    # inverse logit function
    ilogit <- function(x) return(exp(x) / (1+ exp(x)))
    
    # predicted probs
    ilogit(eta0)
    
    
    # for comfidence intervals we can use a normal approximation
    lethal_dose <- function(mod, newdata, alpha) {
      qn <- qnorm(1 - alpha /2)
      mm <- model.matrix(mod, newdata)
      eta0 <- apply(mm, 1, function(i) sum(i * coef(fm1)))
      var_mod <- vcov(mod)
    
      se <- apply(mm, 1, function(x0, var_mod) {
        sqrt(t(x0) %*% var_mod %*% x0)}, var_mod= var_mod)
    
      out <- cbind(ilogit(eta0 - qn * se),
                   ilogit(eta0),
                   ilogit(eta0 + qn * se))
      colnames(out) <- c("LB_CI", "point_est", "UB_CI")
    
      return(list(newdata=newdata,
                  eff_dosage= out))
    }
    
    lethal_dose(fm1, newdata, alpha= 0.05)$eff_dosage
    $eff_dosage
           LB_CI point_est     UB_CI
    1  0.2465905 0.3418240 0.4517820
    2  0.4361703 0.5152749 0.5936215
    3  0.6168088 0.6851225 0.7462674
    4  0.7439073 0.8166343 0.8722545
    5  0.8315325 0.9011443 0.9439316
    6  0.1863738 0.2685402 0.3704385
    7  0.3289003 0.4044270 0.4847691
    8  0.4890420 0.5567386 0.6223914
    9  0.6199426 0.6990808 0.7679095
    10 0.7207340 0.8112133 0.8773662
    11 0.1375402 0.2112382 0.3102215
    12 0.3518053 0.4335213 0.5190198
    13 0.6104540 0.6862145 0.7531978
    14 0.7916268 0.8620545 0.9113443
    15 0.8962097 0.9469715 0.9736370
    

    Rather than doing this manually, you could also manipulate:

    predict.glm(fm1, newdata, se=TRUE)$se.fit

提交回复
热议问题