I'm trying to use predict()
in R to compute a prediction interval for a linear model. When I tried this on a simpler model with only one covariate, it gave the expected output of a point estimate with a confidence interval. When I added a categorical predictor to the model, the predict()
output gives what seems like a single-point estimate with no interval. I've Googled to no avail. Can anyone tell me what I've done wrong here?
medcost <- data.frame(
ID = c(1:100),
charges = sample(0:100000, 100, replace = T),
bmi = sample(18:40, 100, replace = T),
smoker = factor(sample(c("smoker", "nonsmoker"), 100, replace = TRUE))
)
mod2 <- glm(charges ~ bmi smoker, data = medcost)
predict(mod2, interval="predict",
newdata = data.frame(bmi=c(29, 31.5), smoker=c("smoker", "smoker")))
CodePudding user response:
If you want to have the standard error, you could use se.fit = TRUE
like this:
mod2 <- glm(charges ~ bmi smoker, data = medcost)
predict(mod2, interval="predict",
newdata = data.frame(bmi=c(29, 31.5), smoker=c("smoker", "smoker")),
se.fit = TRUE)
#> $fit
#> 1 2
#> 47638.66 47106.14
#>
#> $se.fit
#> 1 2
#> 4304.220 4475.473
#>
#> $residual.scale
#> [1] 28850.85
Created on 2023-01-17 with reprex v2.0.2
I would recommend you having a look at this post: R: glm(...,family=poisson) plot confidence and prediction intervals