kosukeimai / mediation

R package mediation
58 stars 29 forks source link

Error in rep(1, nrow(dataarg)) : invalid 'times' argument #16

Open sakelariev opened 5 years ago

sakelariev commented 5 years ago

Hi there,

thanks for the package. I managed to use it for a single mediator analysis, but I can't seem to get it working for two mediators, using the mediators function I constantly get this error - Error in rep(1, nrow(dataarg)) : invalid 'times' argument

This is my code : mediations(datasets = positive, treatment = c("factor_scores"), mediators = c("downward_comparison", "upward_comparison"), outcome = c("self_esteem"), families=c("gaussian","gaussian"), LowerY = NULL, UpperY = NULL, interaction=FALSE, conf.level=.90, sims=100, boot = FALSE, weights=NULL)

There are no NA values in the columns. What am I missing?

Thanks, Ivaylo

liyezhou commented 4 years ago

the parameter datasets has to be a named list, i.e.

df <- list(df = positive)

mediations(datasets = df, treatment = c("factor_scores"), mediators = c("downward_comparison", "upward_comparison"), outcome = c("self_esteem"), families=c("gaussian","gaussian"), LowerY = NULL, UpperY = NULL, interaction=FALSE, conf.level=.90, sims=100, boot = FALSE, weights=NULL)

ianaianawong commented 1 year ago

I had the same error message when I ran the analysis for 5 mediators.

After running multiple imputation using micepackage to handle missing data, I used the mediationsfunction to test the 5 parallel mediators. All the variables are continuous. I used the codes below (I simplified them to two mediators).

Here is my simplified dataset: https://drive.google.com/file/d/1p9sidXaOro7_ahe880H5pOA4bPNIqCed/view?usp=drive_link

## Create the dataset for multiple imputation
alldata4 <- alldata3[ ,c('ECR_1', 'ECR_2', 'ECR_3', 'ECR_4', 'ECR_5', 'ECR_6',
                         'ECR_7', 'ECR_8', 'ECR_9', 'ECR_10', 'ECR_11', 'ECR_12',  
                         'ECR_13', 'ECR_14', 'ECR_15', 'ECR_16', 'ECR_17', 'ECR_18', 
                         'ECR_19', 'ECR_20', 'ECR_21', 'ECR_22', 'ECR_23', 'ECR_24', 
                         'ECR_25', 'ECR_26', 'ECR_27', 'ECR_28', 'ECR_29', 'ECR_30', 
                         'ECR_31', 'ECR_32', 'ECR_33', 'ECR_34', 'ECR_35', 'ECR_36', 
                         'CEER_1', 'CEER_2', 'CEER_3', 'CEER_4','CEER_5','CEER_6',
                         'CEER_7','CEER_8','CEER_9','CEER_10','CEER_11','CEER_12',
                         'DERS_1', 'DERS_2', 'DERS_3', 'DERS_4','DERS_5','DERS_6',
                         'DERS_7','DERS_8','DERS_9','DERS_10','DERS_11','DERS_12',
                         'DERS_13', 'DERS_14', 'DERS_15', 'DERS_16',
                         'CTS2_1_R', 'CTS2_2_R', 'CTS2_3_R', 'CTS2_4_R', 'CTS2_5_R', 'CTS2_6_R', 
                         'CTS2_7_R', 'CTS2_8_R', 'CTS2_9_R', 'CTS2_10_R', 'CTS2_11_R', 'CTS2_12_R', 
                         'CTS2_13_R', 'CTS2_14_R', 'CTS2_15_R', 'CTS2_16_R', 'CTS2_17_R', 'CTS2_18_R', 
                         'CTS2_19_R', 'CTS2_20_R', 'CTS2_21_R', 'CTS2_22_R', 'CTS2_23_R', 'CTS2_24_R', 
                         'CTS2_25_R', 'CTS2_26_R', 'CTS2_27_R', 'CTS2_28_R', 'CTS2_29_R', 'CTS2_30_R', 
                         'CTS2_31_R', 'CTS2_32_R', 'CTS2_33_R', 'CTS2_34_R', 'CTS2_35_R', 'CTS2_36_R', 
                         'CTS2_37_R', 'CTS2_38_R', 'CTS2_39_R', 'CTS2_40_R', 'CTS2_41_R', 'CTS2_42_R', 
                         'CTS2_43_R', 'CTS2_44_R', 'CTS2_45_R', 'CTS2_46_R', 'CTS2_47_R', 'CTS2_48_R', 
                         'CTS2_49_R', 'CTS2_50_R', 'CTS2_51_R', 'CTS2_52_R', 'CTS2_53_R', 'CTS2_54_R')]

## multiple imputation
alldata4.mi <- mice::mice(alldata4, m = 5, method = 'pmm')

## stack the datasets
alldata4.mi.c <- data.frame(mice::complete(alldata4.mi, include = TRUE, action = "long"))

## total score of ECR - anxiety after multiple imputation 
alldata4.mi.c$ECR_anxiety_average_mi <- rowMeans(alldata4.mi.c[c("ECR_2", "ECR_4", "ECR_6", "ECR_8","ECR_10","ECR_12","ECR_14","ECR_16","ECR_18","ECR_20","ECR_22","ECR_24","ECR_26","ECR_28","ECR_30","ECR_32","ECR_34","ECR_36")])

## total score of CEER after multiple imputation 
alldata4.mi.c$CEER_total_mi <- rowSums(alldata4.mi.c[c("CEER_1", "CEER_2", "CEER_3", "CEER_4","CEER_5","CEER_6",
                                                       "CEER_7","CEER_8","CEER_9","CEER_10","CEER_11","CEER_12")])

## total score of DERS after multiple imputation 
alldata4.mi.c$DERS_total_mi <- rowSums(alldata4.mi.c[c("DERS_1", "DERS_2", "DERS_3", "DERS_4","DERS_5","DERS_6","DERS_7","DERS_8","DERS_9","DERS_10","DERS_11","DERS_12","DERS_13", "DERS_14", "DERS_15", "DERS_16")])

## total score of CTS2_psy_perp_total after multiple imputation 
alldata4.mi.c$CTS2_psy_perp_total_mi <- rowSums(alldata4.mi.c[c("CTS2_1_R", "CTS2_23_R", "CTS2_33_R", "CTS2_47_R","CTS2_15_R","CTS2_19_R","CTS2_45_R","CTS2_49_R")])

## change the stacked datasets (together with the newly created total scores of relevant variables) to as.mids object
alldata4.mi.c.2 <- mice::as.mids(alldata4.mi.c)

## using "mediations" to analyze the mediators
ECRdf <- list(ECRdf = alldata4.mi.c.2)

datasets <- list(ECRdf = ECRdf)

mediators <- c("CEER_total_mi", "DERS_total_mi")

outcome <- c("CTS2_psy_perp_total_mi")

treatment <- c("ECR_anxiety_average_mi")

x <- mediation::mediations(datasets, treatment, mediators, outcome,
                           families=c("gaussian","gaussian"), interaction=FALSE, boot=TRUE,
                           conf.level=.90, sims=50)

However, I got this error message: Error in rep(1, nrow(dataarg)) : invalid 'times' argument

Would you please advise what's wrong with my code?

I saw that the mediatefunction would need to use the data process by amelidiatefunction if my datasets are multiply imputed. I wonder does mediationshave the same requirement? Since I need to create a total score from the multiply imputed individual scores (as research papers recommended not to multiply impute total scores), I'm having some trouble using the amelidiatefunction. Specifically, I was not able to obtain individual imputed datasets that would contain the total scores I created based on the imputed datasets.

Would you please help?

Thank you for your time and help in advance.

liyezhou commented 1 year ago

Hi, can you provide a code sample that I can run on my local machine - otherwise I can't figure out what's wrong. E.g., upload an .RData containing the alldata3 object.

ianaianawong commented 1 year ago

Hi @liyezhou, yes of course.

Please use the link below to download the R script. https://drive.google.com/file/d/15IUd_PCQWfpAaPP1-EnrlCGKUZWCmUJ9/view?usp=drive_link

Thank you for your time and help in advance! I greatly appreciate it :)

ianaianawong commented 1 year ago

@liyezhou

Updates:

1) I've added the total scores to each imputed dataset. I used all imputed datasets in the datasetsinstead of the datasets stacked by the completefunction (which was changed to as.mids. However, I still got the same error message.

2) I've also tried to remove missing values in the datasets (yes, it is weird that the datasets still have missing values after multiple imputations. I'm investigating this issue). However, I still got the same error message.

Am I doing something wrong, or the mediation package has some bugs?

liyezhou commented 1 year ago

I believe the problem might lie with the datasets variable being a class rather than a simple data frame. So I extracted it like this

x <- mediation::mediations(list(a = datasets$ECRdf$ECRdf$data[, c(treatment, mediators, outcome)]), treatment, mediators, outcome,
                           families=c("gaussian","gaussian"), interaction=FALSE, boot=TRUE,
                           conf.level=.90, sims=50)

But there's a new error now. But maybe now you can figure it out yourself how to deal with that error – I've not used the package for a while now. Have a good go!

ianaianawong commented 1 year ago

Hi @liyezhou , thank you very much for your help and advice :)

ianaianawong commented 1 year ago

Hi @liyezhou, I just wanted to let you know that I've fixed the problem by making the beginning part of the names of the datasets the same as the beginning part of the treatment variables :) But thank you for your help anyway :)

I just ran into another problem using the mediations function. I wonder if you could please help take a look and share your wisdom? No pressure though!

I've posted my question here: https://github.com/kosukeimai/mediation/issues/32

lee889 commented 7 months ago

Hi,

Thanks for the package. I get the following error when running the function - Error in rep(1, nrow(p)) : invalid 'times' argument

This is my code : set.seed(1) selected <- sample(1:nrow(occ_final), nrow(occ_final) * 0.75) occ_train <- occ_final[selected, ] occ_test <- occ_final[-selected, ] occ_trainn <- coordinates(occ_train) occ_train1 <- occ_trainn[complete.cases(occ_trainn), ] occ_testt <- coordinates(occ_test) occ_test1 <- occ_testt[complete.cases(occ_testt), ] p <- extract(raster_layer, occ_train1) p_test <- extract(raster_layer, occ_test1) a <- extract(raster_layer, bg)
p_data <- as.data.frame(p) a_data <- as.data.frame(a) pa <- c(rep(1, nrow(p)), rep(0, nrow(a)))

There are no NA values in the columns. What am I missing?

Thanks, Yuan