Spencer,
  Would "sapply" be better here?

R> by.df <- data.frame(A=rep(c("A1", "A2"), each=3),
R+                     B=rep(c("B1", "B2"), each=3),
R+                     x=1:6, y=rep(0:1, length=6))
R> t(sapply(split(by.df, do.call("paste", c(by.df[, 1:2], sep = ":"))),
R+          function(x) coef(lm(y ~ x, data = x))))
      (Intercept)             x
A1:B1   0.3333333 -1.517960e-16
A2:B2   0.6666667  3.282015e-16
R>

Sundar

Spencer Graves wrote:
Hi, Thomas, et al.:

Thanks for the reply. Unfortunately, "do.call" strips off the subset identifiers, which I want to use for further modeling:

 > do.call("rbind", byFits)
     (Intercept)              x
[1,]   0.3333333 -1.517960e-016
[2,]   0.6666667  3.282015e-016

The following does what I want using a "for" loop:

 > by.df <- data.frame(A=rep(c("A1", "A2"), each=3),
+  B=rep(c("B1", "B2"), each=3), x=1:6, y=rep(0:1, length=6))
 > by.lvls <- paste(as.character(by.df$A), as.character(by.df$B), sep=":")
 > A.B <- unique(by.lvls)
 > Fits <- data.frame(A.B = A.B, .Intercept.=rep(NA, length(A.B)),
+  x=rep(NA, length(A.B)))
 > Fits$A <- substring(A.B, 1, regexpr(":", A.B)-1)
 > Fits$B <- substring(A.B, regexpr(":", A.B)+1)
 > for(i in 1:length(A.B))
+  Fits[i, 2:3] <- coef(lm(y~x, by.df[by.lvls==A.B[i],]))
 > Fits
    A.B X.Intercept.             x  A  B
1 A1:B1    0.3333333 -1.517960e-16 A1 B1
2 A2:B2    0.6666667  3.282015e-16 A2 B2
 >

I wondered if there was something easier.

Thanks again for your reply.
Spencer Graves

Thomas Lumley wrote:

On Thu, 5 Jun 2003, Spencer Graves wrote:


Dear R-Help:

I want to (a) subset a data.frame by several columns, (b) fit a model
to each subset, and (c) store a vector of results from the fit in the
columns of a data.frame. In the past, I've used "for" loops do do this.
Is there a way to use "by"?


Consider the following example:

> byFits <- by(by.df, list(A=by.df$A, B=by.df$B),
+  function(data.)coef(lm(y~x, data.)))
> byFits
A: A1
B: B1
  (Intercept)             x
 3.333333e-01 -1.517960e-16
------------------------------------------------------------
A: A2
B: B1
NULL
------------------------------------------------------------
A: A1
B: B2
NULL
------------------------------------------------------------
A: A2
B: B2
 (Intercept)            x
6.666667e-01 3.282015e-16
>
>
#############################
Desired output:

data.frame(A=c("A1","A2"), B=c("B1", "B2"),
    .Intercept.=c(1/3, 2/3), x=c(-1.5e-16, 3.3e-16))

What's the simplest way to do this?



do.call("rbind", byFits)



-thomas



______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help


______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to