Skip to contents

Compute predicted value of formula for linear (mixed) model for with lm or lmer

Usage

get_prediction(fit, formula)

# S4 method for lmerMod
get_prediction(fit, formula)

# S4 method for lm
get_prediction(fit, formula)

Arguments

fit

model fit with lm or lmer

formula

formula of fixed and random effects to predict

Value

Predicted values from formula using parameter estimates from fit linear (mixed) model

Details

Similar motivation as lme4:::predict.merMod(), but that function cannot use just a subset of the fixed effects: it either uses none or all. Note that the intercept is included in the formula by default. To exclude it from the prediction use ~ 0 + ... syntax

Examples


library(lme4)

# Linear model
fit <- lm(Reaction ~ Days, sleepstudy)

# prediction of intercept
get_prediction(fit, ~1)
#>        1        2        3        4        5        6        7        8 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>        9       10       11       12       13       14       15       16 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       17       18       19       20       21       22       23       24 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       25       26       27       28       29       30       31       32 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       33       34       35       36       37       38       39       40 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       41       42       43       44       45       46       47       48 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       49       50       51       52       53       54       55       56 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       57       58       59       60       61       62       63       64 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       65       66       67       68       69       70       71       72 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       73       74       75       76       77       78       79       80 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       81       82       83       84       85       86       87       88 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       89       90       91       92       93       94       95       96 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>       97       98       99      100      101      102      103      104 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      105      106      107      108      109      110      111      112 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      113      114      115      116      117      118      119      120 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      121      122      123      124      125      126      127      128 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      129      130      131      132      133      134      135      136 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      137      138      139      140      141      142      143      144 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      145      146      147      148      149      150      151      152 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      153      154      155      156      157      158      159      160 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      161      162      163      164      165      166      167      168 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      169      170      171      172      173      174      175      176 
#> 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 251.4051 
#>      177      178      179      180 
#> 251.4051 251.4051 251.4051 251.4051 

# prediction of Days without intercept
get_prediction(fit, ~ 0 + Days)
#>        1        2        3        4        5        6        7        8 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>        9       10       11       12       13       14       15       16 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       17       18       19       20       21       22       23       24 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>       25       26       27       28       29       30       31       32 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>       33       34       35       36       37       38       39       40 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>       41       42       43       44       45       46       47       48 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>       49       50       51       52       53       54       55       56 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       57       58       59       60       61       62       63       64 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>       65       66       67       68       69       70       71       72 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>       73       74       75       76       77       78       79       80 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>       81       82       83       84       85       86       87       88 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>       89       90       91       92       93       94       95       96 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       97       98       99      100      101      102      103      104 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>      105      106      107      108      109      110      111      112 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>      113      114      115      116      117      118      119      120 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>      121      122      123      124      125      126      127      128 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>      129      130      131      132      133      134      135      136 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>      137      138      139      140      141      142      143      144 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>      145      146      147      148      149      150      151      152 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>      153      154      155      156      157      158      159      160 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>      161      162      163      164      165      166      167      168 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>      169      170      171      172      173      174      175      176 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>      177      178      179      180 
#> 62.80372 73.27100 83.73829 94.20557 

# Linear mixed model

# fit model
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)

# predict Days, but exclude intercept
get_prediction(fm1, ~ 0 + Days)
#>        1        2        3        4        5        6        7        8 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>        9       10       11       12       13       14       15       16 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       17       18       19       20       21       22       23       24 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>       25       26       27       28       29       30       31       32 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>       33       34       35       36       37       38       39       40 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>       41       42       43       44       45       46       47       48 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>       49       50       51       52       53       54       55       56 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       57       58       59       60       61       62       63       64 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>       65       66       67       68       69       70       71       72 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>       73       74       75       76       77       78       79       80 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>       81       82       83       84       85       86       87       88 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>       89       90       91       92       93       94       95       96 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>       97       98       99      100      101      102      103      104 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>      105      106      107      108      109      110      111      112 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>      113      114      115      116      117      118      119      120 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>      121      122      123      124      125      126      127      128 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>      129      130      131      132      133      134      135      136 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>      137      138      139      140      141      142      143      144 
#> 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 
#>      145      146      147      148      149      150      151      152 
#> 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557  0.00000 10.46729 
#>      153      154      155      156      157      158      159      160 
#> 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 83.73829 94.20557 
#>      161      162      163      164      165      166      167      168 
#>  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 62.80372 73.27100 
#>      169      170      171      172      173      174      175      176 
#> 83.73829 94.20557  0.00000 10.46729 20.93457 31.40186 41.86914 52.33643 
#>      177      178      179      180 
#> 62.80372 73.27100 83.73829 94.20557 

# predict Days and (Days | Subject) random effect, but exclude intercept
get_prediction(fm1, ~ 0 + Days + (Days | Subject))
#>           1           2           3           4           5           6 
#>   2.2585510  21.9248127  41.5910744  61.2573361  80.9235979 100.5898596 
#>           7           8           9          10          11          12 
#> 120.2561213 139.9223830 159.5886448 179.2549065 -40.3987381 -38.5511327 
#>          13          14          15          16          17          18 
#> -36.7035274 -34.8559220 -33.0083167 -31.1607114 -29.3131060 -27.4655007 
#>          19          20          21          22          23          24 
#> -25.6178953 -23.7702900 -38.9604090 -33.9419795 -28.9235500 -23.9051205 
#>          25          26          27          28          29          30 
#> -18.8866910 -13.8682615  -8.8498320  -3.8314025   1.1870270   6.2054565 
#>          31          32          33          34          35          36 
#>  23.6906196  29.3435552  34.9964908  40.6494265  46.3023621  51.9552978 
#>          37          38          39          40          41          42 
#>  57.6082334  63.2611690  68.9141047  74.5670403  22.2603126  29.6576869 
#>          43          44          45          46          47          48 
#>  37.0550613  44.4524356  51.8498100  59.2471843  66.6445587  74.0419330 
#>          49          50          51          52          53          54 
#>  81.4393074  88.8366817   9.0395679  19.2346769  29.4297858  39.6248948 
#>          55          56          57          58          59          60 
#>  49.8200038  60.0151127  70.2102217  80.4053307  90.6004396 100.7955486 
#>          61          62          63          64          65          66 
#>  16.8405086  27.0841585  37.3278084  47.5714582  57.8151081  68.0587580 
#>          67          68          69          70          71          72 
#>  78.3024079  88.5460577  98.7897076 109.0333575  -7.2326151   4.3092525 
#>          73          74          75          76          77          78 
#>  15.8511201  27.3929877  38.9348553  50.4767229  62.0185906  73.5604582 
#>          79          80          81          82          83          84 
#>  85.1023258  96.6441934  -0.3336684  -0.6185476  -0.9034268  -1.1883061 
#>          85          86          87          88          89          90 
#>  -1.4731853  -1.7580645  -2.0429437  -2.3278229  -2.6127022  -2.8975814 
#>          91          92          93          94          95          96 
#>  34.8904868  53.9860379  73.0815891  92.1771402 111.2726913 130.3682424 
#>          97          98          99         100         101         102 
#> 149.4637935 168.5593447 187.6548958 206.7504469 -25.2102286 -13.5695105 
#>         103         104         105         106         107         108 
#>  -1.9287923   9.7119258  21.3526439  32.9933620  44.6340802  56.2747983 
#>         109         110         111         112         113         114 
#>  67.9155164  79.5562346 -13.0700342   4.0114696  21.0929734  38.1744772 
#>         115         116         117         118         119         120 
#>  55.2559809  72.3374847  89.4189885 106.5004922 123.5819960 140.6634998 
#>         121         122         123         124         125         126 
#>   4.5778642  12.0298881  19.4819119  26.9339358  34.3859597  41.8379836 
#>         127         128         129         130         131         132 
#>  49.2900074  56.7420313  64.1940552  71.6460791  20.8636782  34.8669653 
#>         133         134         135         136         137         138 
#>  48.8702523  62.8735394  76.8768265  90.8801136 104.8834007 118.8866878 
#>         139         140         141         142         143         144 
#> 132.8899748 146.8932619   3.2754656  14.6149665  25.9544673  37.2939681 
#>         145         146         147         148         149         150 
#>  48.6334689  59.9729698  71.3124706  82.6519714  93.9914723 105.3309731 
#>         151         152         153         154         155         156 
#> -25.6129993 -10.3232284   4.9665425  20.2563134  35.5460843  50.8358553 
#>         157         158         159         160         161         162 
#>  66.1256262  81.4153971  96.7051680 111.9949389   0.8070461  10.2861758 
#>         163         164         165         166         167         168 
#>  19.7653056  29.2444353  38.7235650  48.2026948  57.6818245  67.1609542 
#>         169         170         171         172         173         174 
#>  76.6400840  86.1192137  12.3145921  24.0659001  35.8172082  47.5685162 
#>         175         176         177         178         179         180 
#>  59.3198243  71.0711323  82.8224403  94.5737484 106.3250564 118.0763644