Dear Adrian,
thanks again for your response. It works perfectly for a single year.
I wonder whether it can be stretched for the lazybones like myself.
Specifically, as I am running different analyses in different years, I wonder whether it can be modified accordingly as I have several configurators and outcomes (named consistently, of course).
As an example, here's a snippet of the script I'm working on that, at the moment, does not recognize the difference between the outcome and its negated version.
outcome <- c("Y")
negated_outcome <- c("~Y")
configurator_list <- c("A", "B", "C", "D", "E", "F", "G")
year_list <- seq(2018, 2020, by = 2)
necessity_results <- list()
sufficiency_results <- list()
necessity_results_NEG <- list()
sufficiency_results_NEG <- list()
necessity_list <- list()
sufficiency_list <- list()
necessity_list_NEG <- list()
sufficiency_list_NEG <- list()
# Here, I extract values from pof results for the necessity and sufficiency of configuratorsextract_pof_values <- function(pof_result, relation) {
if (relation == "necessity") {
return(round(c(
inclN = pof_result$incl.cov$inclN,
RoN = pof_result$incl.cov$RoN,
covN = pof_result$incl.cov$covN
), 3))
} else if (relation == "sufficiency") {
return(round(c(
inclS = pof_result$incl.cov$inclS,
PRI = pof_result$incl.cov$PRI,
covS = pof_result$incl.cov$covS,
covU = pof_result$incl.cov$covU
), 3))
}
}
# Then, with a for cycle, I do it for the years I am interested in
for (i in seq_along(year_list)) {
year <- year_list[i]
# Create the file name for the current year
current_file <- paste("yearSINGLE_", year, ".csv", sep = "")
current_data <- read.csv(current_file)
# Here I modified sprintf() so that it would accommodate both the configurator and the outcome
for (variable in configurator_list) {
pof_result_necessity <- pof(sprintf("%s <- %s", variable, outcome), data = current_data)
necessity_list[[length(necessity_list) + 1]] <- c(
Year = year,
Variable = variable,
extract_pof_values(pof_result_necessity, "necessity")
)
pof_result_sufficiency <- pof(sprintf("%s -> %s", variable, outcome), data = current_data)
sufficiency_list[[length(sufficiency_list) + 1]] <- c(
Year = year,
Variable = variable,
extract_pof_values(pof_result_sufficiency, "sufficiency")
)
pof_result_necessity_NEG <- pof(sprintf("%s <- %s", variable, negated_outcome), data = current_data)
necessity_list_NEG[[length(necessity_list_NEG) + 1]] <- c(
Year = year,
Variable = variable,
extract_pof_values(pof_result_necessity, "necessity")
)
pof_result_sufficiency_NEG <- pof(sprintf("%s -> %s", variable, negated_outcome), data = current_data)
sufficiency_list_NEG[[length(sufficiency_list_NEG) + 1]] <- c(
Year = year,
Variable = variable,
extract_pof_values(pof_result_sufficiency, "sufficiency")
)
}
}
# Then I convert everything to a dataframe and add the rows for each configurator/year and print it
necessity_df <- do.call(rbind, necessity_list)
necessity_df_NEG <- do.call(rbind, necessity_list_NEG)
sufficiency_df <- do.call(rbind, sufficiency_list)
sufficiency_df_NEG <- do.call(rbind, sufficiency_list_NEG)
necessity_results[[length(necessity_results) + 1]] <- necessity_df
necessity_results_NEG[[length(necessity_results_NEG) + 1]] <- necessity_df_NEG
sufficiency_results[[length(sufficiency_results) + 1]] <- sufficiency_df
sufficiency_results_NEG[[length(sufficiency_results_NEG) + 1]] <- sufficiency_df_NEG
print(necessity_df)
print(necessity_df_NEG)
print(sufficiency_df)
print(sufficiency_df_NEG)
I am attaching two files to test if you want.
As always thanks for your time,
Luigi