Function to calculate the random effects from a plm object
(random effects model).
# S3 method for class 'plm'
ranef(object, effect = NULL, ...)A named numeric with the random effects per dimension (individual or time).
Function ranef calculates the random effects of a fitted random
effects model. For one-way models, the effects of the estimated
model are extracted (either individual or time effects). For
two-way models, extracting the individual effects is the default
(both, argument effect = NULL and effect = "individual" will
give individual effects). Time effects can be extracted by setting
effect = "time".
Not all random effect model types are supported (yet?).
fixef() to extract the fixed effects from a fixed
effects model (within model).
data("Grunfeld", package = "plm")
m1 <- plm(inv ~ value + capital, data = Grunfeld, model = "random")
ranef(m1) # individual random effects
#> 1 2 3 4 5 6
#> -9.5242955 157.8910235 -172.8958044 29.9119801 -54.6790089 34.3461316
#> 7 8 9 10
#> -7.8977584 0.6726376 -28.1393497 50.3144442
# compare to random effects by ML estimation via lme from package nlme
library(nlme)
#>
#> Attaching package: ‘nlme’
#> The following object is masked _by_ ‘.GlobalEnv’:
#>
#> Gasoline
m2 <- lme(inv ~ value + capital, random = ~1|firm, data = Grunfeld)
cbind("plm" = ranef(m1), "lme" = unname(ranef(m2)))
#> plm lme
#> 1 -9.5242955 -9.5862170
#> 2 157.8910235 157.9998348
#> 3 -172.8958044 -173.0410642
#> 4 29.9119801 29.9489570
#> 5 -54.6790089 -54.7281533
#> 6 34.3461316 34.3899639
#> 7 -7.8977584 -7.8986758
#> 8 0.6726376 0.6906331
#> 9 -28.1393497 -28.1556448
#> 10 50.3144442 50.3803664
# two-ways RE model, calculate individual and time random effects
data("Cigar", package = "plm")
tw <- plm(sales ~ pop + price, data = Cigar, model = "random", effect = "twoways")
ranef(tw) # individual random effects
#> 1 3 4 5 7 8
#> -16.2313983 -10.9407671 -8.4070839 -8.0035988 -3.5394647 26.4991483
#> 9 10 11 13 14 15
#> 39.7177104 3.8777211 -5.1844126 -19.6522390 2.6503121 16.3006653
#> 16 17 18 19 20 21
#> -11.6829305 -11.4349650 52.1389070 0.1959530 10.1000599 -0.7371054
#> 22 23 24 25 26 27
#> -2.5582962 6.0052270 -13.9084124 -15.9415817 7.2335306 -13.1071681
#> 28 29 30 31 32 33
#> -16.2253164 50.1149877 110.3969356 -1.5881308 -31.7198448 -2.6728683
#> 35 36 37 39 40 41
#> -19.5275399 1.7670879 -3.3623632 -8.4818063 11.7369530 -6.5745407
#> 42 43 44 45 46 47
#> -20.8494465 -5.4741136 -11.0335402 -55.6627609 16.3784126 7.7327316
#> 48 49 50 51
#> -25.6449876 -9.0717648 -14.3557950 10.7278996
ranef(tw, effect = "time") # time random effects
#> 63 64 65 66 67 68 69
#> -4.8730347 -7.3464635 -5.9340750 -5.1545167 -4.9379129 -5.9884844 -6.6989482
#> 70 71 72 73 74 75 76
#> -8.2130475 -5.6496310 -2.2865468 -0.9936552 1.3543836 3.0309237 5.7378570
#> 77 78 79 80 81 82 83
#> 5.6665356 6.4982251 5.3839356 6.2673107 6.9524675 6.8306273 5.2637423
#> 84 85 86 87 88 89 90
#> 3.0305218 2.9219323 2.3839785 1.3038537 0.2722306 -1.0413612 -1.9688282
#> 91 92
#> -2.3758022 0.5637823