Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compute standard error for predicted data in R using predict

Here is my data:

a <- c(60, 65, 70, 75, 80, 85, 90, 95, 100, 105)
b <- c(26, 24.7, 20, 16.1, 12.6, 10.6, 9.2, 7.6, 6.9, 6.9)
a_b <- cbind(a,b)

plot(a,b, col = "purple")
abline(lm(b ~ a),col="red")
reg <- lm(b ~ a)

I would like to use the predict function in order to compute the standard error for the predicted b value at 110.

z <- predict(reg, newdata=data.frame(year=110), se.fit=TRUE)

This is the output I get, but I think this is just giving me the standard errors for my 10 time points, but not the new 11th data point:

 z
 $fit
   1         2         3         4         5         6         7         8         9        10 
 24.456364 22.146061 19.835758 17.525455 15.215152 12.904848 10.594545   8.284242  5.973939  3.663636 

 $se.fit
       1         2         3         4         5         6         7         8         9        10 
 1.2616229 1.0700007 0.8998935 0.7657760 0.6889958 0.6889958 0.7657760 0.8998935 1.0700007 1.2616229 

 $df
[1] 8

 $residual.scale
[1] 2.146516

I'm not sure what to make of this, any help is appreciated!

like image 611
Neuro H Avatar asked Sep 26 '22 05:09

Neuro H


1 Answers

You should probably be a bit more careful with data inside and outside data.frames. Your newdata= parameter should be a data.frame with column names that match the original prodicters. Something like this would be better

a_b <- data.frame(
    a=c(60, 65, 70, 75, 80, 85, 90, 95, 100, 105),
    b=c(26, 24.7, 20, 16.1, 12.6, 10.6, 9.2, 7.6, 6.9, 6.9)
)

plot(b~a, a_b, col = "purple")

reg <- lm(b ~ a, a_b)
abline(reg,col="red")

z <- predict(reg, newdata=data.frame(a=110), se.fit=TRUE)
# $fit
#        1 
# 1.353333 
# 
# $se.fit
# [1] 1.466349
# 
# $df
# [1] 8
# 
# $residual.scale
# [1] 2.146516
like image 117
MrFlick Avatar answered Sep 29 '22 06:09

MrFlick