GloubiGlouba
GloubiGlouba

Reputation: 1

How to extract all the coefficients with the variable name and modalities of a regression model in a loop?

I use the code below to get the results of repeated logistic regressions (with bonferroni correction). The problem is that when my variable x has more than two modalities, my results table only takes the Odd ratio for the association with the first modality for each model run. How could I solve this while excluding to extract the intercepts of each model? In addition, I need to obtain a new column so that for each coefficient this column indicates the variable and the modality of the type 'variable:modality'.

var = outcome chosen for the model

data = name of the table

correction = name of the correction to use
y <- data[,var] 
x <- data[, -which(names(data) == var)]

Repeated regression analysis for each variable x

n <- ncol(x) 
odds <- numeric(n) 
pvalues <- numeric(n)

for (i in 1:n) { 
fit <- glm(y ~ x[,i], family = binomial(link = "logit")) 
summary_fit <- summary(fit) 
odds[i] <- exp(summary_fit$coefficients[-c(1),2]) 
ci <- exp(confint(fit, level = 0.95)[2,]) 
pvalues[i] <- summary_fit$coefficients[-c(1),4] }

Bonferroni correction to p-values

pvalues_corrected <- p.adjust(pvalues, method = correction)

Table with results

results <- data.frame(variable = names(x), odd_ratio = odds, ci_low = data.frame(ci)[1,1], ci_high = data.frame(ci)[2,1], p_value = pvalues_corrected)

Upvotes: 0

Views: 234

Answers (0)

Related Questions