I need help with the fdr-values in my loop with logistic regressions

for (variable in independent_variables) {
formula <- as.formula(paste("Medikament ~ ", variable))
model <- glm(formula, data = Data_dep, family = binomial(link = "logit"))

coef_value <- coef(model)[2]

odds_ratio_value <- exp(coef_value)

n <- length(model$residuals)
R2cs <- 1 - exp((model$deviance - model$null.deviance) / n)
R2n <- R2cs / (1 - exp(-(model$null.deviance / n)))

cat("Variable:", variable, "\n")
cat("p-value:", summary(model)$coefficients[2, 4], "\n")
cat("Odds Ratio:", odds_ratio_value, "\n\n")
cat("R2:", R2n, "\n\n")

formula_additional <- as.formula(paste("meds~ ", variable, " + sex + age_at_survey + PC1 + PC2 + PC3 +
PC4 + PC5 + PC6 + PC7 + PC8 + PC9 + PC10 + PC11 + PC12 + PC13 + PC14 + PC15 + PC16 + PC17 + PC18 + PC19 +
model_additional <- glm(formula_additional, data = Data_dep, family = binomial(link = "logit"))

p_value_additional <- summary(model_additional)$coefficients[2, 4]

p_value_additional_corrected <- p.adjust(p_value_additional, method = "fdr")

cat("additional co-var- Raw p-value:", p_value_additional, "\n")
cat("additional co-var - Fdr-corrected p-value:", p_value_additional_corrected, "\n\n")

This is the loop I wrote. The problem is that the p-value from the logistig regression with additional covariances is the same as the fdr-corrected p-value for this glm. When I calculate it by hand, the values differ.
Can someone tell where the problem is?

you wrote a for loop, that makes it your responsibility to store the results in some way; it seems your method is to overwrite every subsequent value over the previous, this is not likely to be what you truly intend.

you have two options off the top of my head
a) prepare a results object to append or insert into and do that
b) switch from using a for loop to a map() type iteration as that will at least in the most basic respects automatically capture the final output of the implicit loop and store it in a results structure for you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.