Reputation: 79
I want to know how to do the following:
a <- data.frame(num = 1:10, numsqr = (1:10)^2)
b <- data.frame(num = 11:14, numsqr = 0)
fit <- lm(numsqr ~ num, data = a)
b$numsqr <- predict(fit, b)
print(b)
num numsqr
1 11 121
2 12 144
3 13 169
4 14 196
Right now I'm getting the following result
print(b)
num numsqr
1 11 99
2 12 110
3 13 121
4 14 132
How could I get my anticipated result??
Upvotes: 0
Views: 54
Reputation: 1149
If you assume that you have no idea whatsoever of the relationship of the data ( in this case you know that y= x^2 ), it will be very difficult to get the exact values through a linear regression
You can try converting the response variable to a logarithm for a slightly better resolution to incorporate the curvature
Upvotes: 0
Reputation: 32426
To get the squared variable in the formula, you can use I
or poly
(still linear in the coefficients), otherwise it is just fitting y ~ ax + b.
fit <- lm(numsqr ~ I(num^2), data=a)
fit <- lm(numsqr ~ poly(num, 2), data=a) # different model, same predictions
predict(fit, newdata=b)
# 1 2 3 4
# 121 144 169 196
Upvotes: 3