[R] predict.lm within a function

From: Pamela McCaskie <pmccask_at_cyllene.uwa.edu.au>
Date: Thu 31 Aug 2006 - 22:33:41 EST


Hi
I'm trying to wrap predict.lm within a function, but I'm having problems passing arguments into it in this way.

Basically I want to create a lm object, then pass it into the predict.lm function and be able to tell predict.lm which variable I want to predict for, by passing the variable name as an argument of the wrapper function. This variable will always be a factor with 3 levels, so I want to predict the response within each of these levels evaluated at the mean value of all other variables in the model (as indicated below by replicating the means for the data 3 times in the data.frame part of predict.lm).

The simplest of all my attempts looks like this:

my.fun <- function(formula, mydata, predict_variable){

        my.lm <- lm(formula=formula, data=mydata)
        my.predict <- 

predict.lm(my.lm,(data.frame(predict_variable=y,rbind(mean(mydata, na.rm=T),mean(mydata, na.rm=T), mean(mydata,na.rm=T)), row.names=as.numeric(names(table(mydata$predict_variable))))))

        }

outside of the function, the following commands do exactly what I want:

my.lm <- lm(LDL ~ AGE + SEX + factor(SNP1_add), data=testdata) predict.lm(my.lm,(data.frame(SNP1_add=y,rbind(mean(testdata, na.rm=T),mean(testdata, na.rm=T), mean(testdata,na.rm=T)), row.names=as.numeric(names(table(testdata$SNP1_add))))))

where SNP1_add is the variable to predict for, but when I run

my.fun(formula=LDL ~ AGE + SEX + factor(SNP1_add), mydata=testdata, predict_variable="SNP1_add")

I get: Error in sort(unique.default(x), na.last = TRUE) :

        'x' must be atomic

I figure I should be using eval() or substitute() somewhere to but none of my attempts have been successful. Any help would be appreciated.

-- 
Pamela A McCaskie
BSc(Mathematical Sciences)(Hons)

Laboratory for Genetic Epidemiology
Western Australian Institute for Medical Research
University of Western Australia
Ground Floor, B Block
Hospital Avenue, Nedlands
Western Australia  6009
AUSTRALIA
Email:        pmccask@cyllene.uwa.edu.au
Phone:        +61 (0)8 9346 1612
Mob:          +61 (0)417 926 607

This e-mail and all attachments contain information which is confidential and/or subject to copyright. It may not be used or disclosed unless authorised by the sender.

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Thu Aug 31 22:50:00 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 01 Sep 2006 - 12:24:23 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.