in R I am trying to predict a single value. However, I return all the fitted values when I run the code. I have copied the code directly from my course materials and I get the same result. Am I missing something fundamental?
DF1 <- read.table("linear.txt",header=T)
model <- lm(DF1$Payment~DF1$Claim)
newdata <-data.frame(Claim=4.5)
predict(model,newdata)
my result is
DF1 <- read.table("linear.txt",header=T)
> DF1
Claim Payment
1 2.1 2.18
2 2.4 2.06
3 2.5 2.54
4 3.2 2.61
5 3.6 3.67
6 3.8 3.25
7 4.1 4.02
8 4.2 3.71
9 4.5 4.38
10 5.0 4.45
> model <- lm(DF1$Payment~DF1$Claim)
> newdata <-data.frame(Claim=4.5)
> predict(model,newdata)
1 2 3 4 5 6
2.016478 2.281170 2.369401 2.987016 3.339938 3.516400
7 8 9 10
3.781092 3.869323 4.134015 4.575168
Warning message:
'newdata' had 1 row but variables found have 10 rows
>
when I try different sources of data I get the same situation
my desired out put would be
model$coefficients[1]+model$coefficients[2]*4.5
(Intercept)
4.134015
Thank you very much for your help
in R I am trying to predict a single value. However, I return all the fitted values when I run the code. I have copied the code directly from my course materials and I get the same result. Am I missing something fundamental?
DF1 <- read.table("linear.txt",header=T)
model <- lm(DF1$Payment~DF1$Claim)
newdata <-data.frame(Claim=4.5)
predict(model,newdata)
my result is
DF1 <- read.table("linear.txt",header=T)
> DF1
Claim Payment
1 2.1 2.18
2 2.4 2.06
3 2.5 2.54
4 3.2 2.61
5 3.6 3.67
6 3.8 3.25
7 4.1 4.02
8 4.2 3.71
9 4.5 4.38
10 5.0 4.45
> model <- lm(DF1$Payment~DF1$Claim)
> newdata <-data.frame(Claim=4.5)
> predict(model,newdata)
1 2 3 4 5 6
2.016478 2.281170 2.369401 2.987016 3.339938 3.516400
7 8 9 10
3.781092 3.869323 4.134015 4.575168
Warning message:
'newdata' had 1 row but variables found have 10 rows
>
when I try different sources of data I get the same situation
my desired out put would be
model$coefficients[1]+model$coefficients[2]*4.5
(Intercept)
4.134015
Thank you very much for your help
Share Improve this question asked Apr 1 at 1:09 calcalcalcal 152 bronze badges1 Answer
Reset to default 2Don't use $
in your formula when fitting the model, use the data
argument instead:
lm(Payment ~ Claim, data = DF1)
if you use DF1$Payment
etc., R doesn't know that it should substitute the variable in your new (prediction) data set ...
If you have indeed copied exactly what was in the course materials, you should let your instructor know that not specifying data
/using $
in formulas is generally bad practice, for this reason ... (even if you don't want to use predict
afterwards, using the data
argument makes the model call more readable)