dimanche 1 mars 2015

Setting up GGplot for a logistic regression with one predictor and looping through multiple outcomes (or columns)


Vote count:

0




I am a novice at R and have a GG plot related question. Below is a dummy data frame with one column containing the predictor (xvar) and multiple columns of dichotomous outcomes (yvar1, yvar2, yvar3).



df <- data.frame("xvar"=c(0,100,200,300,400,500,600,1000),"yvar1"= c(0,0,0,0,0,0,1,1),"yvar2"=c(0,0,1,1,1,1,1,1),"yvar3"=c(0,0,1,1,0,1,1,1))


I have created a for loop to run a logistic regression for each yvar against the predictor xvar. I am able to successfully plot the regression for each yvar. Please ignore the regression warnings (this is a dummy dataset)



for (i in 2:4) {

logr.yvar <- glm(df[,names(df[i])] ~ xvar, data=df, family=binomial(link="logit"))
print(logr.yvar)

plot(df$xvar, df[,i])
curve(predict(logr.yvar, data.frame(xvar=x), type="response"), add=TRUE)

}


Instead of using the base plot function, I would like to switch to GGPlot2. I am currently able to generate ggplots for individual regressions:



ggplot(df, aes(x=xvar, y=yvar1)) + geom_point() +
stat_smooth(method="glm", family="binomial", se=TRUE)


How can I set up looping using GGplot2? Any solutions or link to existing code will be helpful.


Thanks.



asked 1 min ago







Setting up GGplot for a logistic regression with one predictor and looping through multiple outcomes (or columns)

Aucun commentaire:

Enregistrer un commentaire