Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Predict function in R not giving an interval

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")))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading