| To: | s-news@lists.biostat.wustl.edu |
|---|---|
| Subject: | passing arguments to a function |
| From: | Pravin Jadhav <pravinj@gmail.com> |
| Date: | Mon, 19 Dec 2005 15:24:37 -0500 |
| Domainkey-signature: | a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type; b=PxCDdQPzJt1+/qNSlvSJDADJza+I4WZnGbN52w5ZZq2T9tKCxQaX2FS5lfC+pgd6lpIInM7Uc+1UWKslxK6+6udr07k34All4SSp+GoyJhAYYc0i55FCDnbW8Sv0NpKd5diku53YjIegyVki8oYVJbtqkyfnIBnZAivgconu5Pc= |
|
Dear all, For my previous query (obtain predictions for the missing records), Dr. Pinheiro pointed me to the "predict" function. Thanks! It is doing exactly what I needed. But I am having some trouble putting it inside a generic function. Here is an example (dummy): I want to describe response (resp) as a function of time (time) using a linear mixed effects model for several trials with subjects (id) as a grouping variable. Of the 3 trials, the following code evaluates ONLY the 1st trial. Then the lmeObject is used for prediction. Although, there is no missing data here, the actual dataset has some missing observations. This step works just fine. #CREATE DATA data<-data.frame(trial=rep(1:3,each=30),id=rep(rep(1:10,each=3),3),time=rep(c(1:3),30)) data$resp<-rep(rnorm(30,5,1),each=3)+rep(rnorm(30,0.5,0.2),each=3)*time+rnorm(90,0,1) #FIT DATA AND PREDICT fit1<-lme(resp~time,data="" na.action=""> pred1<-predict(fit1, newdata=data, level=0:1) Now, if I take this inside a generic funciton (only the relevant part is shown) to evaluate all the trials, there is an error. The first part, LME works fine but has problems with predict function. It turns out that the lmeObject, "fit", does retain the information about the linear two sided formula used to describe fixed effcts. If you take a look at the object "fit1" from the previous step and "fit2" from the next step, the difference is clear. "fit1" has explicit two sided formla, whereas, "fit2" retains only list(fullF) and thus causing problems in the predict step. fit.F<-function (data, byvar, fullF, ranF,...) { fit<-by(data, byvar, function(bydata, fullF, ranF,...) { fit<-lme(fixed=fullF, data="" random=ranF, method="ML", na.action=""> # pred.fit<-predict(fit,newdata=bydata, level=0:1) return(fit) # retrun(pred.fit) },fullF=fullF,ranF=ranF) } fit2<-fit.F(data, data$trial, resp~time,list(id=pdDiag(~1+time))) How do I tweak the function so that the predict step is also working? Sorry for a lengthy email (if you are still reading it). Any comments are greatly appreciated. Thanks, Pravin |
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Re: Problems with importData from Excel files (2), Jonathan Dakin |
|---|---|
| Next by Date: | pdf.colors question, Atkinson, Elizabeth J. |
| Previous by Thread: | Problems with importData from Excel files (2), Jonathan Dakin |
| Next by Thread: | Re: passing arguments to a function, Pravin Jadhav |
| Indexes: | [Date] [Thread] [Top] [All Lists] |