1
votes

The following link is my raw dataset.

enter link description here

I have created a linear regression model for it. And now, I would like to create a 95% confidence interval for the prediction that if "Weight=26".

My code was like the following:

          MileWeight.lm <-lm(Mydata$Mileage~Mydata$Weight, 
    data = Mydata)
attach(Mydata)
newdata1 <-(Weight=26)

predict(MileWeight.lm, weight= 26  , interval = "confidence")

The output was 7 rows with "fit lwr upr", with a warning

'newdata' had 1 row but variables found have 7 rows

Have I missed anything? Any assistance would be highly appreciated! Thanks!

Data in dput format.

Mydata <-
structure(list(Weight = c(8, 24.5, 27, 14.5, 28.5, 12.75, 21.25
), Mileage = c(7.69, 4.97, 4.56, 6.49, 4.34, 6.24, 4.45)), class = "data.frame", row.names = c(NA, 
-7L))
1
It's Weight not weight, R is case sensitive. And please don't attach data.Rui Barradas
@RuiBarradas Sorry I posted the wrong command for the "predict" command, I was actually using "newdata1" instead of "Weight = 26" . But after I set up a new linear regression model and used the command provided by the Random User, which was also the same as what I got from R Tutorial. It was not working. I used "attach" was because R Tutorial used that.Chen
newdata1 <-(Weight=26) does not do what you seem to think it does. It could be with double == but that would create a logical vector. The right way is newdata1 <- data.frame(Weight = 26). As for books using attach they can (and generally do) have other good things, that is not one of them.Rui Barradas
And the command provided by @RandomUser works, it outputs what is in the answer.Rui Barradas

1 Answers

2
votes
MileWeight.lm <- lm(Mileage ~ Weight, data = Mydata)
predict(MileWeight.lm, data.frame(Weight = 26), interval = "confidence")
#       fit      lwr      upr
#1 4.525936 3.922626 5.129246