Multiple imputations with MICE and Latent Profile Analysis with tidyLPA - r
I am trying to do a multiple imputation with the mice package and later use those results to do a latent profile analysis with the tidyLPA package. However, I am running into coding problems and I am not sure if it can be solved. I have seen examples on the internet that after the imputation people fit linear / logistic models and using the pool function to pool the R squared estimate but not performing a Latent Profile Analysis.
Averaging the results won't be a good idea as I have read in numerous posts it won't take into account the variability among the imputed datasets.
The code gives me an error before conducting Latent Profile analysis which is the following:
Error in df[, select_vars, drop = FALSE] : incorrect number of dimensions
I am attaching a small example here to check if anyone is having a solution / suggestions.
Thank you in advance.
library("mice")
library("tidyLPA")
data <- data.frame(ID = c(1, 1, 2, 2, 3, 3, 3, 4, 4, 4),
var1 = c(1, 2, 5, 10, NA, 5, 23, NA, NA, 1),
var2 = c(1, NA, NA, 1, NA, 0, 1, 3, 23, 4))
imputation <- mice(data, m = 5,
method = c("", "pmm", "pmm"),
maxit = 20)
LPA <- with(imputation, estimate_profiles(imputation, n_profiles = 2, variances = "equal",
covariances = "equal"))
Related
Pooled average marginal effects from survey-weighted and multiple-imputed data
I am working with survey data and their associated weights, in addition to missing data that I imputed using mice(). The model I'm eventually running contains complex interactions between variables for which I want the average marginal effect. This task seems trivial in STATA, but I'd rather stay in R since that's what I know best. It seems easy to retrieve AME's for each separate imputed dataset and average the estimates. However, I need to make use of pool() (from mice) to make sure I'm getting the correct standard errors. Here is a reproducible example: library(tidyverse) library(survey) library(mice) library(margins) df <- tibble(y = c(0, 5, 0, 4, 0, 1, 2, 3, 1, 12), region = c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3), weight = c(7213, 2142, 1331, 4342, 9843, 1231, 1235, 2131, 7548, 2348), x1 = c(1.14, 2.42, -0.34, 0.12, -0.9, -1.2, 0.67, 1.24, 0.25, -0.3), x2 = c(12, NA, 10, NA, NA, 12, 11, 8, 9, 9)) Using margins() on a simple (non-multiple) svyglm works without a hitch. Running svyglm on each imputation using which() and pooling the results also works well. m <- with(surv_obj, svyglm(y ~ x1 * x2)) pool(m) However, wrapping margins() into which() returns an error "Error in .svycheck(design) : argument "design" is missing, with no default" with(surv_obj, margins(svyglm(y ~ x1 * x2), design = surv_obj)) If I specify the design in the svyglm call, I get "Error in UseMethod("svyglm", design) : no applicable method for 'svyglm' applied to an object of class "svyimputationList"" with(surv_obj, margins(svyglm(y ~ x1 * x2, design = surv_obj), design = surv_obj)) If I drop the survey layer, and simply try to run the margins on each imputed set and then pool, I get a warning: "Warning in get.dfcom(object, dfcom) : Infinite sample size assumed.". m1 <- with(imputed_df, margins(lm(y ~ x1 * x2))) pool(m1) This worries me given that pool() may use sample size in its calculations. Does anyone know of any method to either (a) use which(), margins() and pool() to retrieve the pooled average marginal effects or (b) knows what elements of margins() I should pass to pool() (or pool.scalar()) to achieve the desired result?
Update following Vincent's comment Wanted to update this post following Vincent's comment and related package marginaleffects() which ended up fixing my issue. Hopefully, this will be helpful to others stuck on similar problems. I implemented the code in the vignette linked in Vincent's comment, adding a few steps that allow for survey weighting and modeling. It's worth noting that svydesign() will drop any observations missing on clustering/weighting variables, so marginaleffects() can't predict values back unto the original "dat" data and will throw up an error. Pooling my actual data still throws up an "infinite sample size assumed", which (as noted) should be fine but I'm still looking into fixes. library(tidyverse) library(survey) library(mice) library(marginaleffects) fit_reg <- function(dat) { svy <- svydesign(ids = ~ 1, cluster = ~ region, weight = ~weight, data = dat) mod <- svyglm(y ~ x1 + x2*factor(x3), design = svy) out <- marginaleffects(mod, newdata = dat) class(out) <- c("custom", class(out)) return(out) } tidy.custom <- function(x, ...) { out <- marginaleffects:::tidy.marginaleffects(x, ...) out$term <- paste(out$term, out$contrast) return(out) } df <- tibble(y = c(0, 5, 0, 4, 0, 1, 2, 3, 1, 12), region = c(1, 1, 1, 1, 1, 3, 3, 3, 3, 3), weight = c(7213, 2142, 1331, 4342, 9843, 1231, 1235, 2131, 7548, 2348), x1 = c(1.14, 2.42, -0.34, 0.12, -0.9, -1.2, 0.67, 1.24, 0.25, -0.3), x2 = c(12, NA, 10, NA, NA, 12, 11, 8, 9, 9), x3 = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2)) imputed_df <- mice(df, m = 2, seed = 123) dat_mice <- complete(imputed_df, "all") mod_imputation <- lapply(dat_mice, fit_reg) mod_imputation <- pool(mod_imputation) summary(mod_imputation)
Stratified Sampling a Dataset and Averaging a Variable within the Train Dataset
I'm currently trying to do a stratified split in R to create train and test datasets. A problem posed to me is the following split the data into a train and test sample such that 70% of the data is in the train sample. To ensure a similar distribution of price across the train and test samples, use createDataPartition from the caret package. Set groups to 100 and use a seed of 1031. What is the average house price in the train sample? The dataset is a set of houses with prices (along with other data points) For some reason, when I run the following code, the output I get is labeled as incorrect in the practice problem simulator. Can anyone spot an issue with my code? Any help is much appreciated since I'm trying to avoid learning this language incorrectly. dput(head(houses)) library(ISLR); library(caret); library(caTools) options(scipen=999) set.seed(1031) #STRATIFIED RANDOM SAMPLING with groups of 100, stratefied on price, 70% in train split = createDataPartition(y = houses$price,p = 0.7,list = F, groups = 100) train = houses[split,] test = houses[-split,] nrow(train) nrow(test) nrow(houses) mean(train$price) mean(test$price) Output > dput(head(houses)) structure(list(id = c(7129300520, 6414100192, 5631500400, 2487200875, 1954400510, 7237550310), price = c(221900, 538000, 180000, 604000, 510000, 1225000), bedrooms = c(3, 3, 2, 4, 3, 4), bathrooms = c(1, 2.25, 1, 3, 2, 4.5), sqft_living = c(1180, 2570, 770, 1960, 1680, 5420), sqft_lot = c(5650, 7242, 10000, 5000, 8080, 101930), floors = c(1, 2, 1, 1, 1, 1), waterfront = c(0, 0, 0, 0, 0, 0), view = c(0, 0, 0, 0, 0, 0), condition = c(3, 3, 3, 5, 3, 3), grade = c(7, 7, 6, 7, 8, 11), sqft_above = c(1180, 2170, 770, 1050, 1680, 3890), sqft_basement = c(0, 400, 0, 910, 0, 1530), yr_built = c(1955, 1951, 1933, 1965, 1987, 2001), yr_renovated = c(0, 1991, 0, 0, 0, 0), age = c(59, 63, 82, 49, 28, 13)), row.names = c(NA, -6L ), class = c("tbl_df", "tbl", "data.frame")) > > library(ISLR); library(caret); library(caTools) > options(scipen=999) > > set.seed(1031) > #STRATIFIED RANDOM SAMPLING with groups of 100, stratefied on price, 70% in train > split = createDataPartition(y = houses$price,p = 0.7,list = F, groups = 100) > > train = houses[split,] > test = houses[-split,] > > nrow(train) [1] 15172 > nrow(test) [1] 6441 > nrow(houses) [1] 21613 > > mean(train$price) [1] 540674.2 > mean(test$price) [1] 538707.6
I try to reproduce it manually using sample_frac form dplyr package and cut2 function from Hmisc package. The results are almost the same - still not same. It looks like there might be a problem with pseudo numbers generator or with some rounding. In my opinion your code looks to be a correct one. Is it possible that in previous steps you should remove some outliers or pre-process dataset in any way. library(caret) options(scipen=999) library(dplyr) library(ggplot2) # to use diamonds dataset library(Hmisc) diamonds$index = 1:nrow(diamonds) set.seed(1031) # I use diamonds dataset from ggplot2 package # g parameter (in cut2) - number of quantile groups split = diamonds %>% group_by(cut2(diamonds$price, g= 100)) %>% sample_frac(0.7) %>% pull(index) train = diamonds[split,] test = diamonds[-split,] > mean(train$price) [1] 3932.75 > mean(test$price) [1] 3932.917 set.seed(1031) #STRATIFIED RANDOM SAMPLING with groups of 100, stratefied on price, 70% in train split = createDataPartition(y = diamonds$price,p = 0.7,list = T, groups = 100) train = diamonds[split$Resample1,] test = diamonds[-split$Resample1,] > mean(train$price) [1] 3932.897 > mean(test$price) [1] 3932.572 This sampling procedure should result in mean that approximate to a population one.
Creating new variable based on specific rows of other two variables in a long formatted dataset
I have a long dataset of emotional responses and I need to create a variable based on specific rows of two other variables, within subjects. The following data frame includes data for two participants ("person") presented with 2 pictures (P1, P2, P3), each with 3 repetitions (R1, R2, R3) which is the "phase" variable. The variable response includes two things the rating for each presentation ( scale -30 to 30) and the emotion experienced per picture. person <- c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2) block <- c(4, 4, 4, 5, 5, 5, 8, 8, 4, 4, 4, 5, 5, 5, 8, 8) phase <- c("P1R1", "P1R2", "P1R3", "P2R1","P2R2","P2R3", "Post1", "Post2","P1R1", "P1R2", "P1R3", "P2R1","P2R2","P2R3", "Post1", "Post2") response <- c(30, 30, 30, -30, -30, -30, "Happy", "Sad", 28, 27, 25, -23, -24, -22, "Excited", "Scared") df <- data.frame(person, block, phase, emotion, response) I need to create a new column that will be based on the block number and give me the emotion per picture. I would like the new column to be called “postsurvey” and expect it to be as following: postsurvey <-c ("Happy", "Happy", "Happy","Sad","Sad", "Sad", NA, NA, "Excited", "Excited", "Excited", "Scared", "Scared", "Scared", NA, NA) df <- data.frame(person, block, phase, emotion, response, postsurvey) The code that I used is: df<-df %>% group_by(person, block) %>% mutate(postsurvey=if(block==4){response[phase=="Post1"]} else if (block==5){response[phase=="Post2"]} else {print("NA")}) I expect for each subject to receive for each block number the same response, but what I get is that the response is not grouped by the subjects, and is not repeated within the subject by a block number, as if there is a vector of emotions and a person gets emotions that are not his. *In my original data I have 4 pictures per subject with 10 repetitions, so the "else if" code repeated with more then two conditions.
How to determine Dunn Index using clValid package in R?
I am trying to replicate the results of a journal paper, where the authors have proposed a clustering algorithm and have computed the Dunn index for the resulting cluster using the clValid in R. I was able to replicate the cluster. However, I am unable to get the Dunn index. I have the adjacency matrix (adj) as shown below: adj <-matrix(c(0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0, 1,0,1,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0, 1,1,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0, 1,1,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0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, 1, 3, 3, 2, 1, 3, 1, 3, 1, 3, 3, 1, 1, 3, 3, 1, 3, 3, 1, 3, 3) I used the following code to compute the Dunn index: dist_dunn <- dist(adj, method = "euclidean") dunn_value <- dunn(distance = dist_dunn, clusters = membership) dunn_value The resulting output is 0.2132. However, the actual output reported in the journal paper is 0.111. Can someone help me with this and let me know where I am going wrong? Thanks in advance.
Testing for multicollinearity when there are factors
is it possible to check for multicollinearity in a model with Dummyvariables? Assume the following example treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2), labels = c("placebo", "treated")) improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)), levels = c(1, 2, 3), labels = c("none", "some", "marked")) numberofdrugs <- rpois(84, 5)+1 healthvalue <- rpois(84,5) y <- data.frame(healthvalue,numberofdrugs, treatment, improved) test <- lm(healthvalue~numberofdrugs+treatment+improved, y) What am I supposed to do, when I want to check if multicollinearity occurs in such a model?
You can calculate the VIF for your predictors to quantify the amount of multicollinearity: library(car) vif(test) GVIF Df GVIF^(1/(2*Df)) numberofdrugs 1.035653 1 1.017670 treatment 1.224984 1 1.106790 improved 1.193003 2 1.04510