[R] building an expression

michaell taylor mt at michaelltaylor.com
Wed May 7 17:01:03 CEST 2003


I am still attempting to apply regression results from one dataframe to
another.  A significant problem in this process is that the new
dataframe is quite large and has a complex missing data structure which
differs from the 'in-sample' data.frame.  Moreover, the equation
represents a time series simulation structure demanding that the
equation be applied iterative year by year.

I've seen various posts in the past about "tricks" to apply predict() to
new dataframe with a different missing data structure.  Many of these
require creating missing value indices from the data frame to be
predicting in.  In this particular case, the data so large as to make
this a undesirable route (16 x (53*14*5000)) when iterating over the 10
years of forecast.

I've toyed with constructing the equation from the model object.  This
route seemingly has the advantage of being more efficient when applied
to specific years of the data.frame and does nto require na.action
object creation.

I am close, but not quite there.  I am left with the eq object:
> eq
[1] "0.0188900087591286"         "d1$b * -0.0192141899003632"
[3] "d1$c * 0.158235007749465"  

which needs converting into a single eval() 'able.  seems like some sort
of do.call(expression, eq) sort of construction is the way to go, but
clearly I am not getting the syntax correct.

Any help in building the expression from eq would be appreciated.  More
generally, any suggestions on applying a model object to a new and very
large dataframe would be appreciated.

# simplified example

> d1 <- data.frame(a=rnorm(20),b=rnorm(20),c=rnorm(20))
> moda <- lm(d1$a~d1$b+d1$c)
> moda
lm(formula = d1$a ~ d1$b + d1$c)

(Intercept)         d1$b         d1$c  
    0.01889     -0.01921      0.15824  
> Vl <- labels(moda$coefficients)
> Vn <- as.vector(moda$coefficients)
> eq <- Vn[1]
> for (n in 2:length(Vn)){ eq <- c(eq,paste(Vl[n],'*',Vn[n]))}
> eq
[1] "0.0188900087591286"         "d1$b * -0.0192141899003632"
[3] "d1$c * 0.158235007749465"  

More information about the R-help mailing list