• We are pleased to announce that the winner of our Feedback Prize Draw for the Winter 2024-25 session and winning £150 of gift vouchers is Zhao Liang Tay. Congratulations to Zhao Liang. If you fancy winning £150 worth of gift vouchers (from a major UK store) for the Summer 2025 exam sitting for just a few minutes of your time throughout the session, please see our website at https://www.acted.co.uk/further-info.html?pat=feedback#feedback-prize for more information on how you can make sure your name is included in the draw at the end of the session.
  • Please be advised that the SP1, SP5 and SP7 X1 deadline is the 14th July and not the 17th June as first stated. Please accept out apologies for any confusion caused.

R Query Chp 12 - Linear Regression

Naitik Shah

Keen member
Hi,

In Exercise 12.06 of PBOR where baby.weight is being used in Linear Regression, the code provided by ActEd & my code seems to be similar but I am not getting the answer for the same.

Code by ActEd:
baby <- read.table("baby weights.txt",header=TRUE)
baby
attach(baby)
model1 <- lm(weight~gestation)
coef(model1)[1]+coef(model1)[2]*33
#2.14kg
#Wrap the "gestation" parameter inside a data frame
newdata1 <-data.frame(gestation=33)
#Then we use the "predict" function
predict(model1,newdata1)
#2.14kg


My Code:
model1 <- lm(baby.weights$weight~baby.weights$gestation)
coef(model1)[1]+coef(model1)[2]*33
## 2.141429
newdata1 <- data.frame(baby.weights$gestation = 33)
The error in the above code: unexpected '=' in "newdata1 <- data.frame(baby.weights$gestation="

Now I tried the method of attaching the headers:
attach(baby.weights)
newdata1 <- data.frame(gestation=33)
predict(model1,newdata1)
## 1 2 3 4 5 6
## 1.528571 1.937143 2.345714 2.754286 3.162857 3.571429
Warning message:
'newdata' had 1 row but variables found have 6 rows


In summary, the issue that I have been encountering is that despite having the same code as that provided by ActEd, I am not getting the answer that I should be getting i.e. in this case 2.141429.

Request someone to help me out here.

Regards,
 
Hi,
So my query is basically that I am not getting a single value like the one which is being provided in the solutions but I am getting 6 values instead.

Best,
Naitik Shah.
 
The issue is the name you are using in the data.frame does not match the name used in the lm function.
Hence, the predict function has no idea what to do.

However, if you try to match it using:
newdata1 <- data.frame(baby.weights$gestation=33)
you'll still get a problem as $'s don't work in a data.frame and you end up with the same problem.

So either use:
weight <- baby.weights$weight
gestation <- baby.weights$gestation
lm(weight~gestation)

or

lm(weight~gestation, data=baby.weights)
predict(model1,newdata1)
or
attach(baby.weights)
lm(weight~gestation)

Then all of these will work with
newdata1 <- data.frame(gestation=33)
 
Back
Top