R extract regression coefficients from multiply regression via lapply command

前端 未结 2 1123
天命终不由人
天命终不由人 2021-01-03 13:01

I have a large dataset with several variables, one of which is a state variable, coded 1-50 for each state. I\'d like to run a regression of 28 variables on the remaining 2

2条回答
  •  情书的邮戳
    2021-01-03 13:41

    This is another example of the classic Split-Apply-Combine problem, which can be addressed using the plyr package by @hadley. In your problem, you want to

    1. Split data frame by state
    2. Apply regressions for each subset
    3. Combine coefficients into data frame.

    I will illustrate it with the Cars93 dataset available in MASS library. We are interested in figuring out the relationship between horsepower and enginesize based on origin of country.

    # LOAD LIBRARIES
    require(MASS); require(plyr)
    
    # SPLIT-APPLY-COMBINE
    regressions <- dlply(Cars93, .(Origin), lm, formula = Horsepower ~ EngineSize)
    coefs <- ldply(regressions, coef)
    
       Origin (Intercept) EngineSize
    1     USA    33.13666   37.29919
    2 non-USA    15.68747   55.39211
    

    EDIT. For your example, substitute PUF for Cars93, state for Origin and fm for the formula

提交回复
热议问题