回归数据集的子集

时间:2013-01-23 02:08:55

标签: r regression linear

我想做以下事情并需要一些帮助:

分别计算“年龄”[lm(高度〜年龄)]的“高度”的斜率和截距

(A)每个人

(B)性别

并创建一个包含结果(斜率和截距)的表。我可以使用“申请”吗?

在下一步中,我想做一个统计测试,以确定性别之间的斜率和截距是否有显着差异。我知道如何在R中进行测试,但也许有一种方法可以将斜率/截距计算和T检验结合起来。

示例数据:

example = data.frame(Age = c(1, 3, 6, 9, 12,
                             1, 3, 6, 9, 12,
                             1, 3, 6, 9, 12,
                             1, 3, 6, 9, 12), 
                Individual = c("Jack", "Jack", "Jack", "Jack", "Jack",
                               "Jill", "Jill", "Jill", "Jill", "Jill",
                               "Tony", "Tony", "Tony", "Tony", "Tony",
                               "Jen", "Jen", "Jen", "Jen","Jen"),
                    Gender = c("M", "M", "M", "M", "M",
                               "F", "F", "F", "F", "F",
                               "M", "M", "M", "M", "M",
                               "F", "F", "F", "F", "F"),
                    Height = c(38, 62, 92, 119, 165,
                               31, 59, 87, 118, 170,
                               45, 72, 93, 155, 171,
                               33, 61, 92, 115, 168))

1 个答案:

答案 0 :(得分:5)

对每个级别分别进行回归分析,然后在数据框中组合斜率和截距的一种方法是使用库ddply()中的函数plyr

library(plyr)

ddply(example,"Individual",function(x) coefficients(lm(Height~Age,x)))
  Individual (Intercept)      Age
1       Jack    26.29188 11.11421
2        Jen    22.10660 11.56345
3       Jill    18.33249 12.04315
4       Tony    33.02030 11.96447

ddply(example,"Gender",function(x) coefficients(lm(Height~Age,x)))
  Gender (Intercept)      Age
1      F    20.21954 11.80330
2      M    29.65609 11.53934