Regression on subset of data set

后端 未结 1 432
傲寒
傲寒 2021-01-07 01:20

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 individ

相关标签:
1条回答
  • 2021-01-07 01:51

    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
    
    0 讨论(0)
提交回复
热议问题