Making your dataset easier to import to R:
dat <-
structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a",
"b", "c"), class = "factor"), B = structure(c(1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L
), .Label = c("a", "b", "c", "d"), class = "factor"), C = c(0.47,
0.88, 2.32, 3.26, 0.93, 1.86, 3.22, 0.92, 0.45, 0.92, 2.31, 3.24,
0.91, 1.84, 3.27, 0.86, 0.47, 0.9, 2.33, 3.19, 0.92, 1.84, 3.25,
0.93, 0.45, 0.92, 2.33, 3.08, 0.93, 1.86, 3.25, 0.93, 0.47, 0.9,
2.26, 3.09)), .Names = c("A", "B", "C"), class = "data.frame", row.names = c(NA,
-36L))
Much of SAS by group processing maps to split-apply-combine methods (divide the data into parts, do something with each part, put those parts back together in some way). In this case, the results of models are objects (lists), and the natural way to "combine" multiple models is to put them in a list.
library("plyr")
models <- dlply(dat, .(B), function(DF) glm(C~A, data=DF))
models
is now a list, each element of which is the result of a glm
on a subset of dim
.
> models
$a
Call: glm(formula = C ~ A, data = DF)
Coefficients:
(Intercept) Ab Ac
6.167e-01 1.500e-01 6.799e-17
Degrees of Freedom: 8 Total (i.e. Null); 6 Residual
Null Deviance: 0.472
Residual Deviance: 0.427 AIC: 6.107
$b
Call: glm(formula = C ~ A, data = DF)
Coefficients:
(Intercept) Ab Ac
1.220000 0.306667 0.006667
Degrees of Freedom: 8 Total (i.e. Null); 6 Residual
Null Deviance: 1.99
Residual Deviance: 1.806 AIC: 19.09
$c
Call: glm(formula = C ~ A, data = DF)
Coefficients:
(Intercept) Ab Ac
2.616667 0.333333 -0.003333
Degrees of Freedom: 8 Total (i.e. Null); 6 Residual
Null Deviance: 1.958
Residual Deviance: 1.733 AIC: 18.72
$d
Call: glm(formula = C ~ A, data = DF)
Coefficients:
(Intercept) Ab Ac
2.4733 -0.8133 -0.1067
Degrees of Freedom: 8 Total (i.e. Null); 6 Residual
Null Deviance: 11.4
Residual Deviance: 10.23 AIC: 34.69
attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
B
1 a
2 b
3 c
4 d
Extraction of information from all models at once follows the same paradigm:
> ldply(models, coefficients)
B (Intercept) Ab Ac
1 a 0.6166667 0.1500000 6.798700e-17
2 b 1.2200000 0.3066667 6.666667e-03
3 c 2.6166667 0.3333333 -3.333333e-03
4 d 2.4733333 -0.8133333 -1.066667e-01