Fit and predict in one function

multi_model_2(old_data, new_data, yname, xname, modeltype, ...)

Arguments

old_data

The data set to which predicted values will be added.

new_data

The data set to use for predicting.

yname

The outcome variable

xname

The predictor variable(s)

modeltype

A character specifying the model type e.g lm for linear model

...

Other arguments to specific model types.

Examples

# fit a linear model and get predictions
multi_model_2(iris[1:50,],iris[50:99,],"Sepal.Length","Petal.Length","lm")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species predicted
#> 1           5.1         3.5          1.4         0.2  setosa  4.972378
#> 2           4.9         3.0          1.4         0.2  setosa  6.761943
#> 3           4.7         3.2          1.3         0.2  setosa  6.653485
#> 4           4.6         3.1          1.5         0.2  setosa  6.870402
#> 5           5.0         3.6          1.4         0.2  setosa  6.382339
#> 6           5.4         3.9          1.7         0.4  setosa  6.707714
#> 7           4.6         3.4          1.4         0.3  setosa  6.653485
#> 8           5.0         3.4          1.5         0.2  setosa  6.761943
#> 9           4.4         2.9          1.4         0.2  setosa  6.002734
#> 10          4.9         3.1          1.5         0.1  setosa  6.707714
#> 11          5.4         3.7          1.5         0.2  setosa  6.328109
#> 12          4.8         3.4          1.6         0.2  setosa  6.111192
#> 13          4.8         3.0          1.4         0.1  setosa  6.490797
#> 14          4.3         3.0          1.1         0.1  setosa  6.382339
#> 15          5.8         4.0          1.2         0.2  setosa  6.761943
#> 16          5.7         4.4          1.5         0.4  setosa  6.165422
#> 17          5.4         3.9          1.3         0.4  setosa  6.599256
#> 18          5.1         3.5          1.4         0.3  setosa  6.653485
#> 19          5.7         3.8          1.7         0.3  setosa  6.436568
#> 20          5.1         3.8          1.5         0.3  setosa  6.653485
#> 21          5.4         3.4          1.7         0.2  setosa  6.328109
#> 22          5.1         3.7          1.5         0.4  setosa  6.816173
#> 23          4.6         3.6          1.0         0.2  setosa  6.382339
#> 24          5.1         3.3          1.7         0.5  setosa  6.870402
#> 25          4.8         3.4          1.9         0.2  setosa  6.761943
#> 26          5.0         3.0          1.6         0.2  setosa  6.545026
#> 27          5.0         3.4          1.6         0.4  setosa  6.599256
#> 28          5.2         3.5          1.5         0.2  setosa  6.816173
#> 29          5.2         3.4          1.4         0.2  setosa  6.924631
#> 30          4.7         3.2          1.6         0.2  setosa  6.653485
#> 31          4.8         3.1          1.6         0.2  setosa  6.111192
#> 32          5.4         3.4          1.5         0.4  setosa  6.273880
#> 33          5.2         4.1          1.5         0.1  setosa  6.219651
#> 34          5.5         4.2          1.4         0.2  setosa  6.328109
#> 35          4.9         3.1          1.5         0.2  setosa  6.978860
#> 36          5.0         3.2          1.2         0.2  setosa  6.653485
#> 37          5.5         3.5          1.3         0.2  setosa  6.653485
#> 38          4.9         3.6          1.4         0.1  setosa  6.761943
#> 39          4.4         3.0          1.3         0.2  setosa  6.599256
#> 40          5.1         3.4          1.5         0.2  setosa  6.436568
#> 41          5.0         3.5          1.3         0.3  setosa  6.382339
#> 42          4.5         2.3          1.3         0.3  setosa  6.599256
#> 43          4.4         3.2          1.3         0.2  setosa  6.707714
#> 44          5.0         3.5          1.6         0.6  setosa  6.382339
#> 45          5.1         3.8          1.9         0.4  setosa  6.002734
#> 46          4.8         3.0          1.4         0.3  setosa  6.490797
#> 47          5.1         3.8          1.6         0.2  setosa  6.490797
#> 48          4.6         3.2          1.4         0.2  setosa  6.490797
#> 49          5.3         3.7          1.5         0.2  setosa  6.545026
#> 50          5.0         3.3          1.4         0.2  setosa  5.840046
# multilinear
multi_model_2(iris[1:50,],iris[50:99,],"Sepal.Length",
    "Petal.Length + Sepal.Width","lm")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species predicted
#> 1           5.1         3.5          1.4         0.2  setosa  4.902999
#> 2           4.9         3.0          1.4         0.2  setosa  5.771541
#> 3           4.7         3.2          1.3         0.2  setosa  5.714857
#> 4           4.6         3.1          1.5         0.2  setosa  5.761483
#> 5           5.0         3.6          1.4         0.2  setosa  4.972473
#> 6           5.4         3.9          1.7         0.4  setosa  5.476232
#> 7           4.6         3.4          1.4         0.3  setosa  5.447890
#> 8           5.0         3.4          1.5         0.2  setosa  5.838282
#> 9           4.4         2.9          1.4         0.2  setosa  4.840821
#> 10          4.9         3.1          1.5         0.1  setosa  5.542974
#> 11          5.4         3.7          1.5         0.2  setosa  5.211097
#> 12          4.8         3.4          1.6         0.2  setosa  4.630538
#> 13          4.8         3.0          1.4         0.1  setosa  5.496348
#> 14          4.3         3.0          1.1         0.1  setosa  4.905731
#> 15          5.8         4.0          1.2         0.2  setosa  5.571316
#> 16          5.7         4.4          1.5         0.4  setosa  5.259555
#> 17          5.4         3.9          1.3         0.4  setosa  5.619773
#> 18          5.1         3.5          1.4         0.3  setosa  5.581374
#> 19          5.7         3.8          1.7         0.3  setosa  5.267781
#> 20          5.1         3.8          1.5         0.3  setosa  5.047441
#> 21          5.4         3.4          1.7         0.2  setosa  5.077614
#> 22          5.1         3.7          1.5         0.4  setosa  5.799883
#> 23          4.6         3.6          1.0         0.2  setosa  5.306181
#> 24          5.1         3.3          1.7         0.5  setosa  5.361033
#> 25          4.8         3.4          1.9         0.2  setosa  5.504574
#> 26          5.0         3.0          1.6         0.2  setosa  5.457948
#> 27          5.0         3.4          1.6         0.4  setosa  5.553032
#> 28          5.2         3.5          1.5         0.2  setosa  5.532916
#> 29          5.2         3.4          1.4         0.2  setosa  5.723083
#> 30          4.7         3.2          1.6         0.2  setosa  5.514632
#> 31          4.8         3.1          1.6         0.2  setosa  5.030988
#> 32          5.4         3.4          1.5         0.4  setosa  4.982530
#> 33          5.2         4.1          1.5         0.1  setosa  4.954189
#> 34          5.5         4.2          1.4         0.2  setosa  5.211097
#> 35          4.9         3.1          1.5         0.2  setosa  5.551200
#> 36          5.0         3.2          1.2         0.2  setosa  5.581374
#> 37          5.5         3.5          1.3         0.2  setosa  5.848340
#> 38          4.9         3.6          1.4         0.1  setosa  5.704799
#> 39          4.4         3.0          1.3         0.2  setosa  5.085840
#> 40          5.1         3.4          1.5         0.2  setosa  5.468006
#> 41          5.0         3.5          1.3         0.3  setosa  5.105956
#> 42          4.5         2.3          1.3         0.3  setosa  5.286065
#> 43          4.4         3.2          1.3         0.2  setosa  5.609716
#> 44          5.0         3.5          1.6         0.6  setosa  5.172698
#> 45          5.1         3.8          1.9         0.4  setosa  4.774079
#> 46          4.8         3.0          1.4         0.3  setosa  5.296123
#> 47          5.1         3.8          1.6         0.2  setosa  5.496348
#> 48          4.6         3.2          1.4         0.2  setosa  5.429606
#> 49          5.3         3.7          1.5         0.2  setosa  5.457948
#> 50          5.0         3.3          1.4         0.2  setosa  4.822537
# glm
multi_model_2(iris[1:50,],iris[50:99,],"Sepal.Length","Petal.Length","glm")
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species predicted
#> 1           5.1         3.5          1.4         0.2  setosa  4.972378
#> 2           4.9         3.0          1.4         0.2  setosa  6.761943
#> 3           4.7         3.2          1.3         0.2  setosa  6.653485
#> 4           4.6         3.1          1.5         0.2  setosa  6.870402
#> 5           5.0         3.6          1.4         0.2  setosa  6.382339
#> 6           5.4         3.9          1.7         0.4  setosa  6.707714
#> 7           4.6         3.4          1.4         0.3  setosa  6.653485
#> 8           5.0         3.4          1.5         0.2  setosa  6.761943
#> 9           4.4         2.9          1.4         0.2  setosa  6.002734
#> 10          4.9         3.1          1.5         0.1  setosa  6.707714
#> 11          5.4         3.7          1.5         0.2  setosa  6.328109
#> 12          4.8         3.4          1.6         0.2  setosa  6.111192
#> 13          4.8         3.0          1.4         0.1  setosa  6.490797
#> 14          4.3         3.0          1.1         0.1  setosa  6.382339
#> 15          5.8         4.0          1.2         0.2  setosa  6.761943
#> 16          5.7         4.4          1.5         0.4  setosa  6.165422
#> 17          5.4         3.9          1.3         0.4  setosa  6.599256
#> 18          5.1         3.5          1.4         0.3  setosa  6.653485
#> 19          5.7         3.8          1.7         0.3  setosa  6.436568
#> 20          5.1         3.8          1.5         0.3  setosa  6.653485
#> 21          5.4         3.4          1.7         0.2  setosa  6.328109
#> 22          5.1         3.7          1.5         0.4  setosa  6.816173
#> 23          4.6         3.6          1.0         0.2  setosa  6.382339
#> 24          5.1         3.3          1.7         0.5  setosa  6.870402
#> 25          4.8         3.4          1.9         0.2  setosa  6.761943
#> 26          5.0         3.0          1.6         0.2  setosa  6.545026
#> 27          5.0         3.4          1.6         0.4  setosa  6.599256
#> 28          5.2         3.5          1.5         0.2  setosa  6.816173
#> 29          5.2         3.4          1.4         0.2  setosa  6.924631
#> 30          4.7         3.2          1.6         0.2  setosa  6.653485
#> 31          4.8         3.1          1.6         0.2  setosa  6.111192
#> 32          5.4         3.4          1.5         0.4  setosa  6.273880
#> 33          5.2         4.1          1.5         0.1  setosa  6.219651
#> 34          5.5         4.2          1.4         0.2  setosa  6.328109
#> 35          4.9         3.1          1.5         0.2  setosa  6.978860
#> 36          5.0         3.2          1.2         0.2  setosa  6.653485
#> 37          5.5         3.5          1.3         0.2  setosa  6.653485
#> 38          4.9         3.6          1.4         0.1  setosa  6.761943
#> 39          4.4         3.0          1.3         0.2  setosa  6.599256
#> 40          5.1         3.4          1.5         0.2  setosa  6.436568
#> 41          5.0         3.5          1.3         0.3  setosa  6.382339
#> 42          4.5         2.3          1.3         0.3  setosa  6.599256
#> 43          4.4         3.2          1.3         0.2  setosa  6.707714
#> 44          5.0         3.5          1.6         0.6  setosa  6.382339
#> 45          5.1         3.8          1.9         0.4  setosa  6.002734
#> 46          4.8         3.0          1.4         0.3  setosa  6.490797
#> 47          5.1         3.8          1.6         0.2  setosa  6.490797
#> 48          4.6         3.2          1.4         0.2  setosa  6.490797
#> 49          5.3         3.7          1.5         0.2  setosa  6.545026
#> 50          5.0         3.3          1.4         0.2  setosa  5.840046