PietroFleer
PietroFleer

Reputation: 23

Error in `$<-.data.frame`(`*tmp*`, "P_value", value = 9.66218350888067e-05) : replacement has 1 row, data has 0

Can some-one help me with my code, i have a code which is calculating a lot of logistic regression at the same time. i used this code also for a lm model and then it worked quite wel, however i tried to adapt it to a glm model but it does not work anymore.

Output_logistic <- data.frame()
glm_output = glm(test[,1] ~ test_2[,1], family = binomial ('logit'))

Output_2 <- data.frame(R_spuared = summary(glm_output)$r.squared)
Output_2$P_value <- summary(glm_output)$coefficients[2,4]
Output_2$Variabele <- paste(colnames(test))
Output_2$Variabele_1 <- paste(colnames(test_2))
Output_2$N_NA <- length(glm_output$na.action)
Output_2$df <- paste(glm_output$df.residual)

Output_logistic <- rbind(Output_logistic,Output_2)

running this code gives the next error:

Error in $<-.data.frame(*tmp*, "P_value", value = 9.66218350888067e-05) : replacement has 1 row, data has 0

does anybody know what i have to adapt so that the code will work?

Thanks in advance

Upvotes: 1

Views: 98

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545488

Your Output_2 is an empty data.frame (it has no rows) because summary(glm_output)$r.squared does not exist, because glm doesn’t report this value.

If you need the R-squared value you’ll have to calculate it yourself. But to fix the error you can simply change your code to construct the data-frame from the existing data in the summary:

output_2 = data.frame(
   P_value = summary(glm_output)$coefficients[2, 4],
   Variable = colnames(test),
   # … etc.
)

Upvotes: 1

Related Questions