R for Beginners: Logistic Regression

[This article was first published on R – Traversing Bits, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R is a great tool to test regression models. The following is an R code for a logistic regression model. The objective of this model is to find effects of collective coping and social movement intent on the choice of one of the two hashtags related to racial discrimination (#AliveWhileBlack and #BlackLivesMatter).

######## BlackLivesMatter & AliveWhileBlack Preliminary Statistics ########
data1 <- read.csv(“/Research/…/Data/FinalDataset.csv”, header = TRUE)
data <-as.data.frame(data1)
summary(data1)

##########Logit Model, Resource: http://www.ats.ucla.edu/stat/r/dae/logit.htm############

data$hahtag <- factor(data$Hashtag)#converting Hashtag into a categorical factor
#Regression Model
logit1 <- glm(Hashtag ~ COMSTRS+COMCOP+ADVSS+BOUND+IDENT+GRPCOM+INFODIS+COORD, data = data, family = “binomial”)
summary(logit1)

## Confidence intervals (CIs) using profiled log-likelihood
confint(logit1)

## CIs using standard errors
confint.default(logit1)

## odds ratios only
exp(coef(logit1))

## odds ratios and 95% CI
exp(cbind(OR = coef(logit1), confint(logit1)))

#Model fit
with(logit1, null.deviance – deviance)#Chi-square
with(logit1, df.null – df.residual) #degree of freedom
with(logit1, pchisq(null.deviance – deviance, df.null – df.residual, lower.tail = FALSE))#p-value

#Plot the model
plot(logit1)

###################################################################
#It looks like the last variable in the above model is problematic
#Regression Model2 |||| This is a better model
logit2 <- glm(Hashtag ~ COMSTRS+COMCOP+ADVSS+BOUND+IDENT+GRPCOM+INFODIS, data = data, family = “binomial”)
summary(logit2)
## Confidence intervals (CIs) using profiled log-likelihood
confint(logit2)
## CIs using standard errors
confint.default(logit2)
## odds ratios only
exp(coef(logit2))
## odds ratios and 95% CI
exp(cbind(OR = coef(logit1), confint(logit2)))
#Model fit
with(logit2, null.deviance – deviance)#Chi-square
with(logit2, df.null – df.residual) #degree of freedom
with(logit2, pchisq(null.deviance – deviance, df.null – df.residual, lower.tail = FALSE))#p-value

##################################################################

 
Chamil Rathnayake

The post R for Beginners: Logistic Regression appeared first on Traversing Bits.

To leave a comment for the author, please follow the link and comment on their blog: R – Traversing Bits.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)