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,
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,