I have a lm model with mortality data depending on daily temperature. To estimate a possible adaptation to climate change, I would like to reduce the slope of the curve by 10%. Therefore, I modified the slope-coefficients of the lm model by multiplying with 0.9.
However, the predict output of this modified model is unexpected. The slope is declined, but the curves don´t meet by x=0 but by the intercept of approx. 133. That is the next question, why ist this intercept not 0?
Here is a reproducible example:
x <- seq(0, 20, 0.1)
y <- seq(0, 20, 0.1)^2
mod <- lm(y ~ poly(x, 2))
mod$coefficients
(Intercept) poly(x, 2)1 poly(x, 2)2
133.6667 1645.2355 426.9008
mody <- mod
mody$coefficients[2] <- mody$coefficients[2]*0.9
mody$coefficients[3] <- mody$coefficients[3]*0.9
mody$coefficients
Intercept) poly(x, 2)1 poly(x, 2)2
133.6667 1480.7120 384.2108
plot(x, predict(mod), type="l")
lines(x, predict(mody), col="red")
I have tried to find the reason of the shifted output and I assume it is somewhere in the predict() function. To check the modified coefficients, I tried this, and it shows the expexted output.
curve(coef(mod)[1] + coef(mod)[2] * x + coef(mod)[3] * x^2, from=0, to=20, xlab="x", ylab="y")
curve(coef(mody)[1] + coef(mody)[2] * x + coef(mody)[3] * x^2, from=0, to=20,xlab="x", ylab="y", add = T)
Wy is the predict output different?
Why is the Intercept of the example not 0?
Or how can I "predict" the modified data "by hand" without using predict()?
Thanks for your help!
EDIT: The answer of DaveArmstrong solved the problem for the first example, by using raw=TRUE in poly(). However, with other data, it is still not working maybe due to negative coefficients in the model (?)
here an example of my original data :
x <- seq(15.0, 23.5, 0.1)
y <- c(0.992, 0.998, 1.012, 1.013, 1.015, 1.021, 1.028, 1.027, 1.023, 1.029, 1.032, 1.032, 1.029, 1.036, 1.035, 1.041, 1.043, 1.043, 1.037, 1.037, 1.039, 1.037, 1.041, 1.047, 1.047, 1.048, 1.045, 1.048, 1.044, 1.037, 1.046, 1.042, 1.037, 1.034, 1.032, 1.031, 1.030, 1.034,
1.044, 1.046, 1.036, 1.034, 1.049, 1.050, 1.037, 1.041, 1.046, 1.062, 1.077, 1.084, 1.091, 1.106, 1.114, 1.127, 1.120, 1.122, 1.130,
1.122, 1.135, 1.164, 1.187, 1.186, 1.195, 1.201, 1.197, 1.204, 1.201, 1.205, 1.203, 1.200, 1.205, 1.232, 1.218, 1.218, 1.249, 1.245,
1.253, 1.227, 1.232, 1.252, 1.258, 1.254, 1.248, 1.245, 1.261, 1.289)
org <- lm(y ~ poly(x, 2, raw=TRUE))
coef(org)
(Intercept) poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)2
2.240583377 -0.153426285 0.004822839
orgm <- org
orgm$coefficients[2] <- orgm$coefficients[2]*1.1 #reducing negative coefficient
orgm$coefficients[3] <- orgm$coefficients[3]*0.9
plot(predict(org), ylim=c(0,1.5), type="l")
lines(predict(orgm), col="red")
legend("topleft", legend=c("Original", "Modified"), col=c("black", "red"), lty=c(1,1))
In the resulting plot (plot), the modified model is somehow shifed to lower y-values and the slope also semms not correct. Why is this still unexpected?