I have generated 10 model summaries after fitting the same model on 10 different subsets of the dataset which is as follows
library(mice)
data("nhanes")
head(nhanes)
imp <- mice(nhanes, print = FALSE, m = 10, seed = 24415)
df <- complete(imp, "long")
model_fit <- lapply(1:10, function(i) {
model = lm(bmi ~ lspline(age, c(5, 10)) + hyp + chl,
data = subset(df, `.imp`==i))
})
From this I get different ggpredict
objects
ggpredict(model_fit[[1]], c("age[all]", "hyp"))
ggpredict(model_fit[[2]], c("age[all]", "hyp"))
ggpredict(model_fit[[3]], c("age[all]", "hyp"))
ggpredict(model_fit[[4]], c("age[all]", "hyp"))
ggpredict(model_fit[[5]], c("age[all]", "hyp"))
ggpredict(model_fit[[6]], c("age[all]", "hyp"))
ggpredict(model_fit[[7]], c("age[all]", "hyp"))
ggpredict(model_fit[[8]], c("age[all]", "hyp"))
ggpredict(model_fit[[9]], c("age[all]", "hyp"))
ggpredict(model_fit[[10]], c("age[all]", "hyp"))
I am looking for an efficient way to a) Estimate the average of all the ggpredict
objects by hp and age
I am trying to use future_map
function with ggpredict
. This is my code
library(lspline)
library(purrr)
library(furrr)
plan(multisession, workers =20)
test1 <- future_map(model_fit, ggpredict, c("age[all]", "hyp"), library(lspline), progress = TRUE)
I get a warning and an error message
Warning: could not access model information. Warning could not find any variables for the specified model component.
i In index = 1.
i With name: modelStruct
Caused by error in !is.null(model_Info) && model_info$is_trial
:
! invalid y
type in x && y
However if I switch to regular map function, there is no error or warning everything works fine, it just takes too long.
test1 <- map(model_list[[1]] , ggpredict, c("age[all]", "hyp"))
I like to know where I am going wrong or the right way to specify future_map
to work with ggpredict
. Thanks in advance.
library(lspline)
inside future_map()
may be causing the problem.
The following works:
(Sometimes it is necessary to explicitly load a package for each worker using packages = c("package_name")
as an extra argument.)
[Updated for the second model with lspline
.]
library(mice)
library(ggeffects)
library(tidyverse)
library(furrr)
imp <- mice(nhanes, print = FALSE, m = 10, seed = 24415)
df <- complete(imp, "long")
model_fit <- lapply(1:10, function(i) {
model = lm(bmi ~ age + hyp + chl,
data = subset(df, `.imp`==i))
})
plan(multisession, workers = 10)
future_map(model_fit, ggpredict, c("age[all]", "hyp"), progress = TRUE)
#> [[1]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.38 | 26.67, 30.08
#> 2 | 24.21 | 22.71, 25.71
#> 3 | 20.05 | 17.13, 22.96
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.82 | 28.07, 35.58
#> 2 | 27.66 | 24.88, 30.43
#> 3 | 23.49 | 20.63, 26.35
#>
#> Adjusted for:
#> * chl = 195.08
#>
#> [[2]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.35 | 26.83, 31.87
#> 2 | 26.01 | 23.56, 28.46
#> 3 | 22.67 | 17.89, 27.44
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.35 | 23.58, 35.13
#> 2 | 26.01 | 22.02, 30.00
#> 3 | 22.67 | 18.73, 26.61
#>
#> Adjusted for:
#> * chl = 192.88
#>
#> [[3]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 27.30 | 25.25, 29.35
#> 2 | 25.40 | 23.54, 27.26
#> 3 | 23.51 | 20.08, 26.94
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.87 | 24.24, 33.49
#> 2 | 26.97 | 23.43, 30.52
#> 3 | 25.08 | 21.50, 28.66
#>
#> Adjusted for:
#> * chl = 194.64
#>
#> [[4]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 27.79 | 25.56, 30.03
#> 2 | 24.66 | 22.52, 26.80
#> 3 | 21.52 | 17.66, 25.38
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.87 | 27.10, 36.63
#> 2 | 28.73 | 25.20, 32.26
#> 3 | 25.59 | 22.01, 29.17
#>
#> Adjusted for:
#> * chl = 199.32
#>
#> [[5]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.85 | 26.72, 30.99
#> 2 | 24.47 | 22.75, 26.20
#> 3 | 20.09 | 16.79, 23.40
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.72 | 27.13, 36.32
#> 2 | 27.34 | 23.62, 31.07
#> 3 | 22.96 | 18.94, 26.99
#>
#> Adjusted for:
#> * chl = 192.36
#>
#> [[6]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.45 | 27.44, 31.45
#> 2 | 25.67 | 23.80, 27.54
#> 3 | 21.89 | 18.49, 25.30
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.64 | 26.49, 34.80
#> 2 | 26.87 | 23.77, 29.96
#> 3 | 23.09 | 19.84, 26.34
#>
#> Adjusted for:
#> * chl = 192.56
#>
#> [[7]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.55 | 27.64, 31.46
#> 2 | 25.64 | 23.98, 27.30
#> 3 | 21.73 | 18.87, 24.58
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.48 | 27.60, 33.37
#> 2 | 26.57 | 24.31, 28.84
#> 3 | 22.66 | 19.79, 25.53
#>
#> Adjusted for:
#> * chl = 192.92
#>
#> [[8]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.04 | 26.02, 30.07
#> 2 | 25.29 | 23.62, 26.97
#> 3 | 22.55 | 19.48, 25.62
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.09 | 25.15, 33.04
#> 2 | 26.34 | 23.11, 29.58
#> 3 | 23.60 | 19.96, 27.23
#>
#> Adjusted for:
#> * chl = 187.20
#>
#> [[9]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.35 | 26.33, 30.37
#> 2 | 25.06 | 22.85, 27.26
#> 3 | 21.76 | 17.69, 25.84
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.93 | 26.65, 35.21
#> 2 | 27.63 | 24.85, 30.41
#> 3 | 24.34 | 21.49, 27.19
#>
#> Adjusted for:
#> * chl = 194.56
#>
#> [[10]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.54 | 26.74, 30.34
#> 2 | 25.15 | 23.50, 26.79
#> 3 | 21.75 | 18.64, 24.87
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 32.04 | 27.92, 36.15
#> 2 | 28.65 | 25.61, 31.68
#> 3 | 25.25 | 22.25, 28.26
#>
#> Adjusted for:
#> * chl = 199.08
Created on 2024-04-25 with reprex v2.1.0
library(lspline)
library(furrr)
library(mice)
library(ggeffects)
imp <- mice(nhanes, print = FALSE, m = 10, seed = 24415)
df <- complete(imp, "long")
model_fit <- lapply(1:10, function(i) {
model = lm(bmi ~ lspline(age, c(5, 10)) + hyp + chl,
data = subset(df, `.imp`==i))
})
plan(multisession, workers = 10)
future_map(model_fit, ggpredict, c("age[all]", "hyp"), progress = TRUE)
#> [[1]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.38 | 26.67, 30.08
#> 2 | 24.21 | 22.71, 25.71
#> 3 | 20.05 | 17.13, 22.96
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.82 | 28.07, 35.58
#> 2 | 27.66 | 24.88, 30.43
#> 3 | 23.49 | 20.63, 26.35
#>
#> Adjusted for:
#> * chl = 195.08
#>
#> [[2]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.35 | 26.83, 31.87
#> 2 | 26.01 | 23.56, 28.46
#> 3 | 22.67 | 17.89, 27.44
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.35 | 23.58, 35.13
#> 2 | 26.01 | 22.02, 30.00
#> 3 | 22.67 | 18.73, 26.61
#>
#> Adjusted for:
#> * chl = 192.88
#>
#> [[3]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 27.30 | 25.25, 29.35
#> 2 | 25.40 | 23.54, 27.26
#> 3 | 23.51 | 20.08, 26.94
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.87 | 24.24, 33.49
#> 2 | 26.97 | 23.43, 30.52
#> 3 | 25.08 | 21.50, 28.66
#>
#> Adjusted for:
#> * chl = 194.64
#>
#> [[4]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 27.79 | 25.56, 30.03
#> 2 | 24.66 | 22.52, 26.80
#> 3 | 21.52 | 17.66, 25.38
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.87 | 27.10, 36.63
#> 2 | 28.73 | 25.20, 32.26
#> 3 | 25.59 | 22.01, 29.17
#>
#> Adjusted for:
#> * chl = 199.32
#>
#> [[5]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.85 | 26.72, 30.99
#> 2 | 24.47 | 22.75, 26.20
#> 3 | 20.09 | 16.79, 23.40
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 31.72 | 27.13, 36.32
#> 2 | 27.34 | 23.62, 31.07
#> 3 | 22.96 | 18.94, 26.99
#>
#> Adjusted for:
#> * chl = 192.36
#>
#> [[6]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.45 | 27.44, 31.45
#> 2 | 25.67 | 23.80, 27.54
#> 3 | 21.89 | 18.49, 25.30
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.64 | 26.49, 34.80
#> 2 | 26.87 | 23.77, 29.96
#> 3 | 23.09 | 19.84, 26.34
#>
#> Adjusted for:
#> * chl = 192.56
#>
#> [[7]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.55 | 27.64, 31.46
#> 2 | 25.64 | 23.98, 27.30
#> 3 | 21.73 | 18.87, 24.58
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.48 | 27.60, 33.37
#> 2 | 26.57 | 24.31, 28.84
#> 3 | 22.66 | 19.79, 25.53
#>
#> Adjusted for:
#> * chl = 192.92
#>
#> [[8]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.04 | 26.02, 30.07
#> 2 | 25.29 | 23.62, 26.97
#> 3 | 22.55 | 19.48, 25.62
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 29.09 | 25.15, 33.04
#> 2 | 26.34 | 23.11, 29.58
#> 3 | 23.60 | 19.96, 27.23
#>
#> Adjusted for:
#> * chl = 187.20
#>
#> [[9]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.35 | 26.33, 30.37
#> 2 | 25.06 | 22.85, 27.26
#> 3 | 21.76 | 17.69, 25.84
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 30.93 | 26.65, 35.21
#> 2 | 27.63 | 24.85, 30.41
#> 3 | 24.34 | 21.49, 27.19
#>
#> Adjusted for:
#> * chl = 194.56
#>
#> [[10]]
#> # Predicted values of bmi
#>
#> hyp: 1
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 28.54 | 26.74, 30.34
#> 2 | 25.15 | 23.50, 26.79
#> 3 | 21.75 | 18.64, 24.87
#>
#> hyp: 2
#>
#> age | Predicted | 95% CI
#> ------------------------------
#> 1 | 32.04 | 27.92, 36.15
#> 2 | 28.65 | 25.61, 31.68
#> 3 | 25.25 | 22.25, 28.26
#>
#> Adjusted for:
#> * chl = 199.08
Created on 2024-04-25 with reprex v2.1.0