Created
April 1, 2011 01:03
-
-
Save Protonk/897569 to your computer and use it in GitHub Desktop.
Another plot for a reddit question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ###Simple regression #### | |
| #Read.csv just turns a csv into a data-frame and I assign it to the name "religion" w/ | |
| # the <- command. Your file path will be different, of course. I cleaned up the | |
| #data in google refine first, removing a wierd leading character in the | |
| #country column and the % sign in the religion column | |
| religion<-read.csv(file="~/Downloads/Religion.csv",as.is=TRUE) | |
| #assigns names to the columns | |
| names(religion)<- c("country", "hdi", "religion", "murder") | |
| #Simple linear regression of murder rates on religion, no controls. | |
| rel.lm<- lm(murder ~ religion, data=religion) | |
| #Controlling for HDI | |
| cont.lm <- lm(murder ~ religion + hdi, data=religion) | |
| #To get the results of either of these regressions, type | |
| # summary(cont.lm) at the console. | |
| ##Plots | |
| #these two lines plot the relationship between religiosity and murder, note that even | |
| #though there appears to be a very slightly positive slope there is NO way to reject the | |
| #possibility of no relationship whatsoever. | |
| with(religion, plot(murder ~ religion)) | |
| abline(a=coef(rel.lm)[[1]], b=coef(rel.lm)[[2]], col=3, lty=2) | |
| #we can also plot hdi vs. religion | |
| with(religion, plot(hdi, religion)) | |
| #I wouldn't model this with a linear relationship (partially because both the dependent and explanatory | |
| #variables are bounded between 0 and 1, but also because the relationship looks non-linear | |
| #but you can run a simple regression w/ this command | |
| lm(religion ~ hdi, data=religion) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment