0

R for Beginners: Logistic Regression

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

Traversing Bits

Leave a Reply

Your email address will not be published. Required fields are marked *