I am trying to use R to fit a linear model and make predictions. My model includes some constant side parameters that are not in the data frame. Here's a simplified version of what I'm doing:
dat <- data.frame(x=1:5,y=3*(1:5))
b <- 1
mdl <- lm(y~I(b*x),data=dat)
Unfortunately the model object now suffers from a dangerous scoping issue: lm()
does not save b
as part of mdl
, so when predict()
is called, it has to reach back into the environment where b
was defined. Thus, if subsequent code changes the value of b
, the predict value will change too:
y1 <- predict(mdl,newdata=data.frame(x=3)) # y1 == 9
b <- 5
y2 <- predict(mdl,newdata=data.frame(x=3)) # y2 == 45
How can I force predict()
to use the original b
value instead of the changed one? Alternatively, is there some way to control where predict()
looks for the variable, so I can ensure it gets the desired value? In practice I cannot include b
as part of the newdata
data frame, because in my application, b
is a vector of parameters that does not have the same size as the data frame of new observations.
Please note that I have greatly simplified this relative to my actual use case, so I need a robust general solution and not just ad-hoc hacking.