[R] by() function doesn't work inside another function

From: Russell Ivory <Russell.Ivory_at_merrickbank.com>
Date: Thu, 10 Jul 2008 10:21:47 -0600


I have a longitudinal data set in long format and I want to run individual regressions. I do this by using the by() function as follows:  

temp <- by(tolerance.pp, tolerance.pp$id, function(x) summary(lm(tolerance ~ time, data=x)))  

This works fine. Coefficients for the first two subjects are shown below.  

Coefficients:

            Estimate Std. Error t value Pr(>|t|)

(Intercept) 1.9020 0.2519 7.549 0.00482 **

time 0.1190 0.1029 1.157 0.33105  

Coefficients:

            Estimate Std. Error t value Pr(>|t|)

(Intercept) 1.14400 0.13336 8.578 0.00333 **

time 0.17400 0.05444 3.196 0.04948 *    

Each subject has five rows of data, measured at periods 0,1,2,3,4. There are 16 subjects so 80 total rows and the output is 16 regression models.  

However, if I put this inside another function, the by() function no longer generates regressions by subject id. It does generate 16 models; but each model is identical which is a regression using all 80 rows at once. The function and coefficients for the first two models are shown below.    

test <- function(X,x,y,id)

{

temp <- by(X, id, function(z) summary(lm(y~x,data=z)))

temp

}  

attach(tolerance.pp)

test(tolerance.pp, time, tolerance, id)

>

Coefficients:

            Estimate Std. Error t value Pr(>|t|)

(Intercept) 1.35775 0.08952 15.167 < 2e-16 ***

x 0.13081 0.03655 3.579 0.000596 ***  

Coefficients:

            Estimate Std. Error t value Pr(>|t|)

(Intercept) 1.35775 0.08952 15.167 < 2e-16 ***

x 0.13081 0.03655 3.579 0.000596 ***    

Without attaching the data I've even tried  

test(tolerance.pp, tolerance.pp$time, tolerance.pp$tolerance, tolerance.pp$id)  

but get the same results. Any thoughts are greatly appreciated.    

Russell Ivory

Merrick Bank

South Jordan, UT

****************************************************************************This e-mail and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to whom it is addressed. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, be advised that you have received this e-mail in error, and that any use, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender at Merrick Bank and delete it from your computer. Although Merrick Bank attempts to sweep e-mail and attachments for viruses, it does not guarantee that either are virus-free and accepts no liability for any damage sustained as a result of viruses.

	[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org 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 10 Jul 2008 - 16:26:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jul 2008 - 16:31:36 GMT.

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

list of date sections of archive