Linear Regression and group by in R

前端 未结 10 1276
抹茶落季
抹茶落季 2020-11-22 02:27

I want to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 sta

10条回答
  •  长发绾君心
    2020-11-22 02:54

    ## make fake data
     ngroups <- 2
     group <- 1:ngroups
     nobs <- 100
     dta <- data.frame(group=rep(group,each=nobs),y=rnorm(nobs*ngroups),x=runif(nobs*ngroups))
     head(dta)
    #--------------------
      group          y         x
    1     1  0.6482007 0.5429575
    2     1 -0.4637118 0.7052843
    3     1 -0.5129840 0.7312955
    4     1 -0.6612649 0.9028034
    5     1 -0.5197448 0.1661308
    6     1  0.4240346 0.8944253
    #------------ 
    ## function to extract the results of one model
     foo <- function(z) {
       ## coef and se in a data frame
       mr <- data.frame(coef(summary(lm(y~x,data=z))))
       ## put row names (predictors/indep variables)
       mr$predictor <- rownames(mr)
       mr
     }
     ## see that it works
     foo(subset(dta,group==1))
    #=========
                  Estimate Std..Error   t.value  Pr...t..   predictor
    (Intercept)  0.2176477  0.1919140  1.134090 0.2595235 (Intercept)
    x           -0.3669890  0.3321875 -1.104765 0.2719666           x
    #----------
    ## one option: use command by
     res <- by(dta,dta$group,foo)
     res
    #=========
    dta$group: 1
                  Estimate Std..Error   t.value  Pr...t..   predictor
    (Intercept)  0.2176477  0.1919140  1.134090 0.2595235 (Intercept)
    x           -0.3669890  0.3321875 -1.104765 0.2719666           x
    ------------------------------------------------------------ 
    dta$group: 2
                   Estimate Std..Error    t.value  Pr...t..   predictor
    (Intercept) -0.04039422  0.1682335 -0.2401081 0.8107480 (Intercept)
    x            0.06286456  0.3020321  0.2081387 0.8355526           x
    
    ## using package plyr is better
     library(plyr)
     res <- ddply(dta,"group",foo)
     res
    #----------
      group    Estimate Std..Error    t.value  Pr...t..   predictor
    1     1  0.21764767  0.1919140  1.1340897 0.2595235 (Intercept)
    2     1 -0.36698898  0.3321875 -1.1047647 0.2719666           x
    3     2 -0.04039422  0.1682335 -0.2401081 0.8107480 (Intercept)
    4     2  0.06286456  0.3020321  0.2081387 0.8355526           x
    

提交回复
热议问题