The data is split into strata according to the levels of the grouping factors and individual lm fits are obtained for each stratum.

lm_by(data, formula, id = NULL, ...)

lmBy(formula, data, id = NULL, ...)

Arguments

data

A dataframe

formula

A linear model formula object of the form y ~ x1 + ... + xn | g1 + ... + gm. In the formula object, y represents the response, x1, ..., xn the covariates, and the grouping factors specifying the partitioning of the data according to which different lm fits should be performed.

id

A formula describing variables from data which are to be available also in the output.

...

Additional arguments passed on to lm().

Value

A list of lm fits.

Author

Søren Højsgaard, sorenh@math.aau.dk

Examples


bb <- lmBy(1 / uptake ~ log(conc) | Treatment, data=CO2)
coef(bb)
#>            (Intercept)   log(conc)
#> nonchilled   0.1440697 -0.01825196
#> chilled      0.1669465 -0.01964886

fitted(bb)
#> $nonchilled
#>          1          2          3          4          5          6          7 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#>          8          9         10         11         12         13         14 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#>         15         16         17         18         19         20         21 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#>         43         44         45         46         47         48         49 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#>         50         51         52         53         54         55         56 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#>         57         58         59         60         61         62         63 
#> 0.06095250 0.04980222 0.04329220 0.03715092 0.03064091 0.02516341 0.01798962 
#> 
#> $chilled
#>         22         23         24         25         26         27         28 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#>         29         30         31         32         33         34         35 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#>         36         37         38         39         40         41         42 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#>         64         65         66         67         68         69         70 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#>         71         72         73         74         75         76         77 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#>         78         79         80         81         82         83         84 
#> 0.07746803 0.06546436 0.05845611 0.05184481 0.04483656 0.03893984 0.03121701 
#> 
residuals(bb)
#> $nonchilled
#>             1             2             3             4             5 
#>  0.0015474990 -0.0169074783 -0.0145565675 -0.0102692027 -0.0023122957 
#>             6             7             8             9            10 
#>  0.0003467923  0.0071993013  0.0125769107 -0.0131721785 -0.0163380218 
#>            11            12            13            14            15 
#> -0.0132274781 -0.0060103658 -0.0010088224  0.0045837478  0.0007758940 
#>            16            17            18            19            20 
#> -0.0189380176 -0.0184783039 -0.0133979540 -0.0073308843 -0.0023843685 
#>            21            43            44            45            46 
#>  0.0039884064  0.0333871216  0.0022811182 -0.0051242607 -0.0038175898 
#>            47            48            49            50            51 
#>  0.0017215519  0.0057007858  0.0101793985  0.0223808323 -0.0043476697 
#>            52            53            54            55            56 
#> -0.0106124611 -0.0057043822  0.0002232899  0.0069909291  0.0137564161 
#>            57            58            59            60            61 
#>  0.0275430742  0.0017441766 -0.0045325098 -0.0013086292  0.0044468117 
#>            62            63 
#>  0.0104237769  0.0179816074 
#> 
#> $chilled
#>           22           23           24           25           26           27 
#> -0.007045494 -0.023970588 -0.025452808 -0.022943079 -0.014067327 -0.010691257 
#>           28           29           30           31           32           33 
#> -0.005377213  0.030058852 -0.028834327 -0.029884680 -0.026071617 -0.018929822 
#>           34           35           36           37           38           39 
#> -0.012273178 -0.007632101 -0.011242864 -0.017845316 -0.032209389 -0.022433048 
#>           40           41           42           64           65           66 
#> -0.019129616 -0.013687319 -0.007062417  0.017770066  0.001649730 -0.003207489 
#>           67           68           69           70           71           72 
#>  0.001065240  0.006445494  0.006105200  0.014445094  0.052402100  0.022254935 
#>           73           74           75           76           77           78 
#>  0.022844705  0.025078264  0.035163443  0.034052856  0.038227438  0.016871593 
#>           79           80           81           82           83           84 
#> -0.009908808 -0.002590186  0.004021109  0.011029364  0.013970208  0.019034250 
#> 

summary(bb)
#> 
#> Call:
#> lm(formula = 1/uptake ~ log(conc), data = wd)
#> 
#> Residuals:
#>       Min        1Q    Median        3Q       Max 
#> -0.018938 -0.007001 -0.000393  0.005422  0.033387 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  0.144070   0.014634   9.845 3.03e-12 ***
#> log(conc)   -0.018252   0.002494  -7.318 6.79e-09 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.01213 on 40 degrees of freedom
#> Multiple R-squared:  0.5724,	Adjusted R-squared:  0.5618 
#> F-statistic: 53.55 on 1 and 40 DF,  p-value: 6.79e-09
#> 
#> 
#> Call:
#> lm(formula = 1/uptake ~ log(conc), data = wd)
#> 
#> Residuals:
#>       Min        1Q    Median        3Q       Max 
#> -0.032209 -0.016901 -0.004292  0.016265  0.052402 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  0.166947   0.025977   6.427 1.19e-07 ***
#> log(conc)   -0.019649   0.004427  -4.438 6.95e-05 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.02154 on 40 degrees of freedom
#> Multiple R-squared:   0.33,	Adjusted R-squared:  0.3132 
#> F-statistic:  19.7 on 1 and 40 DF,  p-value: 6.95e-05
#> 
coef(summary(bb))
#> $nonchilled
#>                Estimate  Std. Error   t value     Pr(>|t|)
#> (Intercept)  0.14406966 0.014634155  9.844754 3.026663e-12
#> log(conc)   -0.01825196 0.002494095 -7.318068 6.789702e-09
#> 
#> $chilled
#>                Estimate Std. Error   t value     Pr(>|t|)
#> (Intercept)  0.16694651 0.02597669  6.426782 1.187822e-07
#> log(conc)   -0.01964886 0.00442720 -4.438214 6.950357e-05
#> 
coef(summary(bb), simplify=TRUE)
#>      stratum   parameter    Estimate  Std. Error   t value     Pr(>|t|)
#> 1 nonchilled (Intercept)  0.14406966 0.014634155  9.844754 3.026663e-12
#> 2 nonchilled   log(conc) -0.01825196 0.002494095 -7.318068 6.789702e-09
#> 3    chilled (Intercept)  0.16694651 0.025976687  6.426782 1.187822e-07
#> 4    chilled   log(conc) -0.01964886 0.004427200 -4.438214 6.950357e-05