Skip to contents

These methods account for special features of the predict methods of some popular model types: glm (from package stats), glmrob (from package robustbase), gam (from package mgcv), ranger (from package ranger), lmer, glmer (from package lme4).

They all execute model.default() with an adjusted default value of predict_function.

Usage

# S3 method for glm
model(
  x,
  ...,
  predict_function = function(object, ...) predict(object, ..., type = "response"),
  env = parent.frame()
)

# S3 method for glmrob
model(
  x,
  ...,
  predict_function = function(object, ...) predict(object, ..., type = "response"),
  env = parent.frame()
)

# S3 method for gam
model(
  x,
  ...,
  predict_function = function(object, ...) predict(object, ..., type = "response"),
  env = parent.frame()
)

# S3 method for ranger
model(
  x,
  ...,
  predict_function = function(object, ...) predict(object, ...)$predictions,
  env = parent.frame()
)

# S3 method for merMod
model(
  x,
  ...,
  predict_function = function(object, ..., type = "response", allow.new.levels = TRUE)
    predict(object, ..., allow.new.levels = allow.new.levels),
  env = parent.frame()
)

# S3 method for lmerMod
model(
  x,
  ...,
  predict_function = function(object, ..., type = "response", allow.new.levels = TRUE)
    predict(object, ..., allow.new.levels = allow.new.levels),
  env = parent.frame()
)

# S3 method for glmerMod
model(
  x,
  ...,
  predict_function = function(object, ..., type = "response", allow.new.levels = TRUE)
    predict(object, ..., allow.new.levels = allow.new.levels),
  env = parent.frame()
)

Arguments

x

A fitted model.

...

Passed to model.default().

predict_function

As in model.default.

env

An environment. Used for internal purposes.

Value

All these methods of model() return a model.

Examples

# Simulate data
d <- simuldat()

# ranger fitted model (random forest):
if (require(ranger)){
  ranger_fitted <- ranger(Y ~ ., d)
  # Methods predict.ranger() returns a list:
  str(predict(ranger_fitted, data = d))
  
  # Method model.ranger() makes sure that a "model" object returns
  # a vector, as required:
  ranger_model <- model(ranger_fitted)
  predict(ranger_model)
}
#> List of 5
#>  $ predictions              : num [1:500] 2.92 6.26 -1.35 2.76 6.09 ...
#>  $ num.trees                : num 500
#>  $ num.independent.variables: num 11
#>  $ num.samples              : int 500
#>  $ treetype                 : chr "Regression"
#>  - attr(*, "class")= chr "ranger.prediction"
#>           1           2           3           4           5           6 
#>  2.91631098  6.26134305 -1.34780870  2.76100755  6.09257025  4.42211470 
#>           7           8           9          10          11          12 
#>  6.78545610  7.33146115  4.14053750  1.17097194 -2.84093221  3.39374205 
#>          13          14          15          16          17          18 
#>  0.33780223  4.58553928  5.58182871  8.12603752  3.72604377  4.56215423 
#>          19          20          21          22          23          24 
#>  4.96627884  6.30760876  4.94196994  4.38367527  7.10772055  3.97831499 
#>          25          26          27          28          29          30 
#>  5.86480146  8.44237233  2.98875420  3.21152098  5.87508518  4.50926405 
#>          31          32          33          34          35          36 
#>  4.65584514  3.68389230  2.12183612  3.41618665  2.11476106  2.57904001 
#>          37          38          39          40          41          42 
#>  3.29527777  3.66304204  2.69351695  3.08974745  5.69446596  5.73572447 
#>          43          44          45          46          47          48 
#>  4.73120763  7.92816020  2.56899105  4.41101371  3.04676948  5.08871687 
#>          49          50          51          52          53          54 
#>  1.78658629  6.30525996  1.28558710  4.49212530  5.31736587  6.27955658 
#>          55          56          57          58          59          60 
#>  5.13352741  3.86483362  2.12919455  5.41657007  7.52815584  5.69844757 
#>          61          62          63          64          65          66 
#>  4.43919367  5.01851187  6.44574883  1.91884013  1.76946875  3.26919777 
#>          67          68          69          70          71          72 
#>  6.51161773  4.77641486  8.46678002  6.60230456  6.24928789 -2.23269471 
#>          73          74          75          76          77          78 
#>  5.92310844  5.76268576  2.89829650  4.43484215  4.35922348  2.16435021 
#>          79          80          81          82          83          84 
#>  6.40823754  3.84899619  3.40405079  1.93731925  5.66865715  3.99114018 
#>          85          86          87          88          89          90 
#>  6.07044507  4.98774369  2.60238842  4.07240134  1.87292015  4.01765390 
#>          91          92          93          94          95          96 
#>  3.50254838  4.82708541  2.80101398  6.38136677  2.04378731  6.11222030 
#>          97          98          99         100         101         102 
#>  4.34613332 -1.69144285  2.83133485  7.54631918  1.83290046  1.27700285 
#>         103         104         105         106         107         108 
#>  7.00848829  4.29107640  5.54136269  4.69688307  3.02178322  8.98761683 
#>         109         110         111         112         113         114 
#>  2.06902910  1.37973583  4.62967358  2.30298310  7.17993543  4.23408099 
#>         115         116         117         118         119         120 
#>  4.94599346  2.86228994 -0.44324822  6.41408755  5.09070133  5.40694922 
#>         121         122         123         124         125         126 
#>  2.96586088  3.14094370  8.26596186 -0.20062942  5.54577576  0.96037158 
#>         127         128         129         130         131         132 
#>  5.52839672  6.72168986  3.23478807  3.88216140  1.52751880  3.45972785 
#>         133         134         135         136         137         138 
#>  6.59809893  3.33458081  8.29455687  4.49673712  3.87842837  6.83636309 
#>         139         140         141         142         143         144 
#>  1.08801775 -1.08917412  5.93583786  5.94901419  3.77277060  1.99488492 
#>         145         146         147         148         149         150 
#>  3.61117486  1.47451818  4.95003547  4.20954477  5.31712485  0.42640919 
#>         151         152         153         154         155         156 
#>  6.23509448  3.32537831  4.31329505  3.63117266 11.44642826  0.51997242 
#>         157         158         159         160         161         162 
#>  4.81176455  7.72329750  4.06251850  5.97439404  0.50064504  0.40197567 
#>         163         164         165         166         167         168 
#>  3.04359664 -0.43138400  1.78486781  7.66478534  0.22444857  1.24971914 
#>         169         170         171         172         173         174 
#>  1.08783464  4.98442039  6.12099881  2.64717103  5.86062651  3.68045282 
#>         175         176         177         178         179         180 
#>  1.42412017  7.26484374  4.03144279  4.62841225  5.32358453  1.12251471 
#>         181         182         183         184         185         186 
#>  6.71904749  2.11701290  2.37191763  5.99872002  3.24736589  5.13766190 
#>         187         188         189         190         191         192 
#>  3.14707656  4.04452210  5.53162629  3.00628350  3.49735253  0.75033643 
#>         193         194         195         196         197         198 
#>  6.60747359  6.09751195  2.76793444  4.75113178  0.44164274  5.59499761 
#>         199         200         201         202         203         204 
#>  2.34349542  3.78747390  4.54312663  4.54971143  1.03481154  7.22276430 
#>         205         206         207         208         209         210 
#>  6.58579971  5.12623672  2.83270088  4.18302873  2.30582237  4.11913245 
#>         211         212         213         214         215         216 
#>  7.39957406  8.96719837  4.59464952  1.58300805  1.64558215  2.86836308 
#>         217         218         219         220         221         222 
#>  4.23779549  4.71156141  1.95325141  5.47944108  2.10302701  6.67901401 
#>         223         224         225         226         227         228 
#>  1.93735897  3.32752710  0.70859749  7.39005732  2.56560830  6.02207548 
#>         229         230         231         232         233         234 
#>  9.39154723  6.86663707  4.41232455  8.58459585  3.92256379  6.64115797 
#>         235         236         237         238         239         240 
#>  6.32744418  2.68269303  5.00982899  2.70813360  1.64662670  5.57640601 
#>         241         242         243         244         245         246 
#>  4.13061151  7.37637345  0.14841804  4.59352065  2.20526565  4.34205936 
#>         247         248         249         250         251         252 
#>  2.21627003  5.60468915  2.95570230  5.43273092  5.06193653  0.58308015 
#>         253         254         255         256         257         258 
#>  5.35506452  3.50584520  6.99735693  6.25140785  5.53627706  3.48093558 
#>         259         260         261         262         263         264 
#>  3.89512774  3.37914568  2.19157366  3.77809251  2.47163367  8.70604724 
#>         265         266         267         268         269         270 
#>  3.97418655  4.31452349  6.39983471  5.26516658  6.20690055  4.11630281 
#>         271         272         273         274         275         276 
#>  6.23398524  3.58559906  4.50007914  7.61936407  4.43939341  3.15279348 
#>         277         278         279         280         281         282 
#>  4.20865971 -1.03206011  9.08279909  3.23320288  6.56471246  5.47482481 
#>         283         284         285         286         287         288 
#>  4.72032775  1.73601771  5.54436211  4.55897796  3.07213435 -0.21475534 
#>         289         290         291         292         293         294 
#>  6.20498485  5.65724415  4.27453569  7.33228653  1.13409235  2.31536166 
#>         295         296         297         298         299         300 
#>  4.63057719  3.70253915  8.45966174  1.23745477  4.90110013  0.61415672 
#>         301         302         303         304         305         306 
#>  2.68360540  3.28334324  0.75979428  3.67773141  7.27572970  7.22967797 
#>         307         308         309         310         311         312 
#>  4.54270622  6.04970120  4.81150978  8.68081333  7.34166850  4.04374686 
#>         313         314         315         316         317         318 
#>  4.07353908  3.67318554  2.01724514  5.72654292  4.02426731  5.84777468 
#>         319         320         321         322         323         324 
#>  3.61122027  5.41094020  7.94185502  5.06218191  8.16730182  4.87721427 
#>         325         326         327         328         329         330 
#>  2.90691496  5.90000920  6.21134980  6.11094641  4.25882061  4.63535827 
#>         331         332         333         334         335         336 
#>  3.80220969  3.40934887  1.01361685  5.40544932  2.91173868  2.25656858 
#>         337         338         339         340         341         342 
#>  6.02398431  2.65980381  2.59007977  6.42324254  6.68840896  3.74938730 
#>         343         344         345         346         347         348 
#>  3.26284761  2.12687481  1.56788377  6.14683082  4.22269089  1.02570759 
#>         349         350         351         352         353         354 
#>  5.38701645  3.67168964  6.71666944  6.66425253  4.37137560  3.34453796 
#>         355         356         357         358         359         360 
#>  5.25398081  1.26070193  7.27646535  4.49036848  1.51371191  4.23857828 
#>         361         362         363         364         365         366 
#> -0.69665834  1.98604236  2.42548387  5.68727523  2.82628530  6.31981781 
#>         367         368         369         370         371         372 
#>  1.69837590  6.81035611  7.57950732  3.75879235  5.68589178  2.03280450 
#>         373         374         375         376         377         378 
#>  3.48847810  7.09617989  3.31343059  8.09563852  2.09313166  5.06863326 
#>         379         380         381         382         383         384 
#>  5.93289137  2.70876708  5.92240837 -0.25745600  4.15732373  1.34024476 
#>         385         386         387         388         389         390 
#>  1.29422776  0.04056416  4.99953383  5.51428665  3.18066711  4.17121504 
#>         391         392         393         394         395         396 
#>  0.72469716  6.18808701  5.03562990  2.31041355  5.22205671  7.65438539 
#>         397         398         399         400         401         402 
#>  1.81286088 -0.22198274  4.78915715  8.00895631  2.66218748  0.91373529 
#>         403         404         405         406         407         408 
#>  2.17260899  2.74548157  6.65124691  3.72753964  1.37979317  5.62181869 
#>         409         410         411         412         413         414 
#>  5.42688868  2.50120895  1.37018777  1.71975225  7.49634109  2.53320041 
#>         415         416         417         418         419         420 
#>  4.09990751  5.83460296  3.72070428  6.59719572  2.39746710  5.51498683 
#>         421         422         423         424         425         426 
#>  4.30574046  2.22250821  5.03907441  5.29197922  5.37888705  4.41969614 
#>         427         428         429         430         431         432 
#>  1.57583400  7.62275972  3.67160690  5.70304311  3.93307354  6.65054615 
#>         433         434         435         436         437         438 
#>  3.84039255  3.15814219  3.15302074  3.24332759  2.82569270  5.92007441 
#>         439         440         441         442         443         444 
#>  3.75862471  3.38365969  0.34502271  2.68942939  0.01067636  3.04535935 
#>         445         446         447         448         449         450 
#>  3.13720785  3.37998571  4.80023514  2.15332031  0.79666048  5.66739420 
#>         451         452         453         454         455         456 
#>  4.26253637  4.45426566  3.91968503  7.31406769  3.52516962  3.54675270 
#>         457         458         459         460         461         462 
#>  6.49504534  6.06883986  6.00230067  5.18343276  2.46664108  1.43101271 
#>         463         464         465         466         467         468 
#>  2.96636915  2.32116336  3.15934747  3.31547923  2.10178055 -0.76393641 
#>         469         470         471         472         473         474 
#>  3.48492600  4.91009052  6.44697292  4.48537400  0.24221103  3.85614813 
#>         475         476         477         478         479         480 
#>  4.82795795  6.20075939  4.12066100  1.10844694  5.37623813  4.23414625 
#>         481         482         483         484         485         486 
#>  4.18021021  9.39753490  3.42141044  7.34665714  5.52553029  7.79488010 
#>         487         488         489         490         491         492 
#>  3.96068717  4.67981222  4.99659705  3.40434779  4.05707817  3.43648895 
#>         493         494         495         496         497         498 
#>  2.65624452  2.45385811  6.55673350  3.58396092  6.30268046  3.43461812 
#>         499         500 
#>  3.04277400  5.95528571