I have a matrix of variables X
and fitted linear model Y ~ X1 + X2 + X3 + X4
. Let's say I want to predict Y
for a vector of means of my variables (X1_mean, X2_mean, X3_mean, X4_mean)
(and let's call it X_means
, I use function colMeans
). So, normally I'd do this in this fashion:
predict(model, X1 = X1_mean, X2 = X2_mean, X3 = X3_mean, X4 = X4_mean)
But is there a faster way to do that? For 4 variables typing (Xi = Xi_mean)
is not the end of the world, but for, let's say, 50 variables it will take a while. Can I somehow rewrite it so as to assign each mean to each variable? Something like colnames = means
.
Let's say your data looks like this:
set.seed(1)
df <- setNames(as.data.frame(matrix(rnorm(25), ncol=5)), c("Y", paste0("X", 1:4)))
df
#> Y X1 X2 X3 X4
#> 1 -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737
#> 2 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630
#> 3 -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498
#> 4 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170
#> 5 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575
And your model looks like this:
model1 <- lm(Y ~ X1 + X2 + X3 + X4, data = df)
Note that you can get a named list of the means of all your predictor columns like this:
lapply(df[-1], mean)
#> $X1
#> [1] 0.1351357
#>
#> $X2
#> [1] 0.03812297
#>
#> $X3
#> [1] 0.459567
#>
#> $X4
#> [1] 0.08123054
So to predict at the mean of each predictor in this case we could just do:
predict(model1, lapply(df[-1], mean))
#> 1
#> 0.1292699
Created on 2020-12-08 by the reprex package (v0.3.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.