Your code does not play because there are several errors in your code. Here's a fixed version that also shows your error:
set.seed(2157010)
x1 <- 1998:2011
x2 <- x1 + rnorm(length(x1))
y <- 3*x2 + rnorm(length(x1))
fit <- lm( y ~ x1 + x2)
coef(fit)
(Intercept) x1 x2
260.55645444 -0.04276353 2.91272272
fit$coefficients[2:3] <- c(5, 1)
coef(fit)
(Intercept) x1 x2
260.5565 5.0000 1.0000
So the problem was what you used fit$coef, although the component name in the lmoutput is valid coefficients. The shortened version works to get values, but not to install, because it created a new component with the name coef, and the function coefretrieved the values fit$coefficient.
source
share