### Worked example with mice: how_many_imputations() Source: https://errickson.net/howManyImputations/index.html Worked example using the mice package on the airquality dataset. It shows the full workflow: adding missingness, imputing with mice, fitting a model, and calling how_many_imputations(). The output shows the estimated number of imputations (72) and with cv = .01 (1767). ```r > library(howManyImputations) > data(airquality) > # Add some missingness > airquality[4:10, 3] <- rep(NA, 7) > airquality[1:5, 4] <- NA > airquality <- airquality[-c(5, 6)] > impdata1 <- mice(airquality, m = 5, maxit = 10, method = 'pmm', seed = 500) > modelFit1 <- with(impdata1, lm(Temp ~ Ozone + Solar.R + Wind)) > how_many_imputations(modelFit1) [1] 72 > how_many_imputations(modelFit1, cv = .01) [1] 1767 ``` -------------------------------- ### Using jomo and mitools: how_many_imputations() Source: https://errickson.net/howManyImputations/index.html Example using the jomo package for imputation and mitools to convert the output to a list, then fitting a model and calling how_many_imputations(). The output shows the estimated number of imputations (77). ```r > library(jomo) > library(mitools) # for the `imputationList` function > jomodata <- jomo1(airquality, nburn = 100, nbetween = 100, nimp = 5) > impdata2 <- imputationList(split(jomodata, jomodata$Imputation)) > modelFit2 <- with(impdata2, lm(Temp ~ Ozone + Solar.R + Wind)) > how_many_imputations(modelFit2) [1] 77 ``` -------------------------------- ### Calculate required imputations example Source: https://errickson.net/howManyImputations/reference/how_many_imputations.html Demonstrates calculating the required number of imputations using a linear model fit on imputed airquality data. ```R data(airquality) # Add some missingness airquality[4:10, 3] <- rep(NA, 7) airquality[1:5, 4] <- NA airquality <- airquality[-c(5, 6)] impdata1 <- mice::mice(airquality, m = 5, maxit = 10, method = 'pmm', seed = 500) #> #> iter imp variable #> 1 1 Ozone Solar.R Wind Temp #> 1 2 Ozone Solar.R Wind Temp #> 1 3 Ozone Solar.R Wind Temp #> 1 4 Ozone Solar.R Wind Temp #> 1 5 Ozone Solar.R Wind Temp #> 2 1 Ozone Solar.R Wind Temp #> 2 2 Ozone Solar.R Wind Temp #> 2 3 Ozone Solar.R Wind Temp #> 2 4 Ozone Solar.R Wind Temp #> 2 5 Ozone Solar.R Wind Temp #> 3 1 Ozone Solar.R Wind Temp #> 3 2 Ozone Solar.R Wind Temp #> 3 3 Ozone Solar.R Wind Temp #> 3 4 Ozone Solar.R Wind Temp #> 3 5 Ozone Solar.R Wind Temp #> 4 1 Ozone Solar.R Wind Temp #> 4 2 Ozone Solar.R Wind Temp #> 4 3 Ozone Solar.R Wind Temp #> 4 4 Ozone Solar.R Wind Temp #> 4 5 Ozone Solar.R Wind Temp #> 5 1 Ozone Solar.R Wind Temp #> 5 2 Ozone Solar.R Wind Temp #> 5 3 Ozone Solar.R Wind Temp #> 5 4 Ozone Solar.R Wind Temp #> 5 5 Ozone Solar.R Wind Temp #> 6 1 Ozone Solar.R Wind Temp #> 6 2 Ozone Solar.R Wind Temp #> 6 3 Ozone Solar.R Wind Temp #> 6 4 Ozone Solar.R Wind Temp #> 6 5 Ozone Solar.R Wind Temp #> 7 1 Ozone Solar.R Wind Temp #> 7 2 Ozone Solar.R Wind Temp #> 7 3 Ozone Solar.R Wind Temp #> 7 4 Ozone Solar.R Wind Temp #> 7 5 Ozone Solar.R Wind Temp #> 8 1 Ozone Solar.R Wind Temp #> 8 2 Ozone Solar.R Wind Temp #> 8 3 Ozone Solar.R Wind Temp #> 8 4 Ozone Solar.R Wind Temp #> 8 5 Ozone Solar.R Wind Temp #> 9 1 Ozone Solar.R Wind Temp #> 9 2 Ozone Solar.R Wind Temp #> 9 3 Ozone Solar.R Wind Temp #> 9 4 Ozone Solar.R Wind Temp #> 9 5 Ozone Solar.R Wind Temp #> 10 1 Ozone Solar.R Wind Temp #> 10 2 Ozone Solar.R Wind Temp #> 10 3 Ozone Solar.R Wind Temp #> 10 4 Ozone Solar.R Wind Temp #> 10 5 Ozone Solar.R Wind Temp modelFit1 <- with(impdata1, lm(Temp ~ Ozone + Solar.R + Wind)) how_many_imputations(modelFit1) #> [1] 57 how_many_imputations(modelFit1, cv = .01) #> [1] 1394 ``` -------------------------------- ### Using Amelia: how_many_imputations() Source: https://errickson.net/howManyImputations/index.html Example using the Amelia package for imputation, converting the output to a format that mice can understand, fitting a model, and calling how_many_imputations(). The output shows the estimated number of imputations (128). ```r > library(Amelia) > data(freetrade) > a.out <- amelia(freetrade, m = 20, ts = "year", cs = "country") > modelFit3 <- with(imputationList(a.out$imputations), lm(tariff ~ polity + pop + gdp.pc + year + country)) > how_many_imputations(modelFit3) [1] 128 ``` -------------------------------- ### Using how_many_imputations with jomo and mitools Source: https://errickson.net/howManyImputations/reference/how_many_imputations.html Loads jomo and mitools, creates 5 imputations of the airquality dataset with jomo1, converts them to an imputationList, fits a linear model, and calls how_many_imputations. The output shows the function returns 45. ```R library(jomo) library(mitools) # for the `imputationList` function jomodata <- jomo::jomo1(airquality, nburn = 100, nbetween = 100, nimp = 5) #> Found 4 continuous outcomes and no categorical. Using function jomo1con. #> ..........First imputation registered. #> ..........Imputation number 2 registered #> ..........Imputation number 3 registered #> ..........Imputation number 4 registered #> ..........Imputation number 5 registered #> The posterior mean of the fixed effects estimates is: #> X1 #> Ozone 41.993606 #> Solar.R 185.024961 #> Wind 9.911321 #> Temp 78.190098 #> #> The posterior covariance matrix is: #> Ozone Solar.R Wind Temp #> Ozone 1094.22277 1005.712450 -65.909864 211.69824 #> Solar.R 1005.71245 8446.240353 -1.810144 256.93195 #> Wind -65.90986 -1.810144 12.341253 -14.50923 #> Temp 211.69824 256.931946 -14.509226 89.29333 impdata2 <- mitools::imputationList(split(jomodata, jomodata$Imputation)) modelfit2 <- with(impdata2, lm(Temp ~ Ozone + Solar.R + Wind)) how_many_imputations(modelfit2) #> [1] 45 ``` -------------------------------- ### Using how_many_imputations with Amelia Source: https://errickson.net/howManyImputations/reference/how_many_imputations.html Loads Amelia, imputes the freetrade dataset 20 times, fits a linear model, and calls how_many_imputations. The output shows the function returns 112. ```R library(Amelia) #> Loading required package: Rcpp #> ## #> ## Amelia II: Multiple Imputation #> ## (Version 1.8.1, built: 2022-11-18) #> ## Copyright (C) 2005-2024 James Honaker, Gary King and Matthew Blackwell #> ## Refer to http://gking.harvard.edu/amelia/ for more information #> ## data(freetrade) a.out <- amelia(freetrade, m = 20, ts = "year", cs = "country") #> -- Imputation 1 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #> #> -- Imputation 2 -- #> #> 1 2 3 4 5 6 7 8 9 10 #> #> -- Imputation 3 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 #> #> -- Imputation 4 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #> #> -- Imputation 5 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 #> #> -- Imputation 6 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #> #> -- Imputation 7 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #> #> -- Imputation 8 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #> #> -- Imputation 9 -- #> #> 1 2 3 4 5 6 7 8 9 10 #> #> -- Imputation 10 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #> #> -- Imputation 11 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 #> #> -- Imputation 12 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #> #> -- Imputation 13 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #> #> -- Imputation 14 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 #> #> -- Imputation 15 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #> #> -- Imputation 16 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #> #> -- Imputation 17 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #> #> -- Imputation 18 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #> #> -- Imputation 19 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #> #> -- Imputation 20 -- #> #> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 modelFit3 <- with(a.out, lm(tariff ~ polity + pop + gdp.pc + year + country)) how_many_imputations(modelFit3) #> [1] 112 ``` -------------------------------- ### Basic usage with mice: how_many_imputations() Source: https://errickson.net/howManyImputations/index.html Basic usage of how_many_imputations() with a model fit on multiply imputed data from the mice package. The function estimates the total number of imputations needed. The optional cv and alpha arguments control the conservativeness of the estimate. ```r library(mice) imputed_data <- mice(data_with_missing, ...) mi_model_fit <- with(imputed_data, lm(y ~ x)) how_many_imputations(mi_model_fit) how_many_imputations(mi_model_fit, cv = .1, alpha = .01) ``` -------------------------------- ### how_many_imputations function usage Source: https://errickson.net/howManyImputations/reference/how_many_imputations.html Basic syntax for calling the function with a model object, specifying the desired precision of standard errors (cv) and the significance level (alpha). ```R how_many_imputations(model, cv = 0.05, alpha = 0.05) howManyImputations(model, cv = 0.05, alpha = 0.05) ``` -------------------------------- ### how_many_imputations(model, cv = 0.05, alpha = 0.05) Source: https://errickson.net/howManyImputations/reference/how_many_imputations.html Calculates the number of imputations needed to achieve a specified coefficient of variation (cv) for standard error estimates. This function is the primary API and can be used with either a mira object, mipo object, or an object convertible to mira via as.mira(). ```APIDOC ## how_many_imputations(model, cv = 0.05, alpha = 0.05) ### Description Calculates the number of imputations required to obtain a desired precision of standard errors, as described by von Hippel (2020). The function uses the Fraction of Missing Information (FMI) from an initial set of imputations to estimate the total needed. ### Method R function ### Function Signature ```r how_many_imputations(model, cv = 0.05, alpha = 0.05) howManyImputations(model, cv = 0.05, alpha = 0.05) ``` ### Parameters - **model** (mira, mipo, or convertible to mira) - Required - Either a `mira` object (created by running a model on a data set imputed with `mice::mice()`), a `mipo` object (created by running `pool()` on a `mira` object), or any object which can be converted to `mira` via `as.mira()`. - **cv** (numeric) - Optional - Desired precision of standard errors. Default is 0.05. If the data were re-imputed, the estimated standard errors would differ by no more than this amount. - **alpha** (numeric) - Optional - Significance level for choice of "conservative" FMI. Default is 0.05. ### Value The number of required imputations to obtain the `cv` level of precision. ### Details The function implements the quadratic rule from von Hippel (2020) to calculate the number of imputations needed. The procedure requires an initial estimate of FMI from a limited number of imputations (typically 20). The function returns the total number of imputations needed, which may be larger than the initial batch. ### Example ```r library(howManyImputations) data(airquality) # Add some missingness airquality[4:10, 3] <- rep(NA, 7) airquality[1:5, 4] <- NA airquality <- airquality[-c(5, 6)] impdata1 <- mice::mice(airquality, m = 5, maxit = 10, method = 'pmm', seed = 500) modelFit1 <- with(impdata1, lm(Temp ~ Ozone + Solar.R + Wind)) how_many_imputations(modelFit1) # [1] 57 how_many_imputations(modelFit1, cv = .01) # [1] 1394 ``` ### References von Hippel, Paul T. "How Many Imputations Do You Need? A Two-stage Calculation Using a Quadratic Rule." Sociological Methods & Research 49.3 (2020): 699-718. ``` -------------------------------- ### BibTeX citation for von Hippel (2020) Source: https://errickson.net/howManyImputations/authors.html BibTeX entry for the von Hippel (2020) article, as provided in the package's inst/CITATION file. Use this to cite the methodology behind the package. ```bibtex @Article{, title = {How Many Imputations Do You Need? A Two-stage Calculation Using a Quadratic Rule.}, author = {Paul T {von Hippel}}, journal = {Sociological Methods & Research}, year = {2020}, volume = {49}, number = {33}, pages = {699--718}, url = {https://journals.sagepub.com/doi/full/10.1177/0049124117747303}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.