Dalmuti71
Dalmuti71

Reputation: 1629

Regression on subset of data set

I'd like to do the following and need some help:

Calculate slope and intercept for "Height" over "Age" [lm(Height~Age)] separately for

(A) each individual

(B) gender

and create a table containing the results (slope and intercept). Can I use "apply" for this?

In a next step I would like to do a statistical test to determine if slope and intercept are significantly different between Gender. I know how to do the test in R but maybe there is a way to combine slope/intercept calculation and T-testing.

Example data:

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))

Upvotes: 2

Views: 1730

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98419

One way to do regression analysis separately for each level and then combine slopes and intercepts in data frame, is to use function ddply() from library 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

Upvotes: 6

Related Questions