Created
June 16, 2013 02:48
-
-
Save grantbrown/5790561 to your computer and use it in GitHub Desktop.
R code template for working on homework 3
This file contains 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
# Homework 3 R code template | |
# Before doing anything else, get a copy of HeartDiseaseDiabetes.csv | |
# This file is available on the github site, and an excel version is | |
# available on icon. Remember that to use the excel version, you need | |
# to open it in excel or libre office and save a copy as a csv. | |
# Once you have obtained a copy of the data set in csv format, take | |
# note of where you've saved it. We need to tell R to use that folder | |
# as a working directory. | |
# I'm working on a linux machine, so my path will look a lot like | |
# one used on a mac. If you don't know how to work with folder paths, | |
# email me and we'll work on getting you set up. | |
# This is where I'm keeping my section 2 materials: | |
setwd("/home/grantbrown/dev/171-162-Materials/Section2/") | |
# A mac path might look something like: | |
# setwd("/Users/yourusername/Documents/Homework3") | |
# A windows path might look something like: | |
# setwd("C:/Users/yourusername/Documents/Homework3") | |
# Now we need to read in the data. It's already nicely packaged into a | |
# csv file, so there's not much work to do here. I'm going to call | |
# my data mortalityData | |
mortalityData = read.csv("./HeartDiseaseDiabetes.csv", head = TRUE) | |
# Remember that the option "head = TRUE" tells R to look for variable | |
# names. | |
# In this homework, we're interested in the log of the variable | |
# HeartCrude and the variable DiabetesCrude. I'm going to extract | |
# these from the data set here, and refer to them as Y and X for clarity. | |
Y = log( mortalityData$HeartCrude) | |
X = mortalityData$DiabetesCrude | |
# To get a basic scatterplot, I just have to do: | |
plot(X, Y, | |
main = "Fancy Pants Scatterplot", | |
xlab = "Diabetes Crude Rate per 100k", | |
ylab = "log(Heart Crude Rate per 100k)" | |
) | |
# To get a basic regression, I just use the lm function | |
myFancyRegression = lm(Y~X) | |
# To add the regression line to my plot, I just do | |
abline(reg=myFancyRegression) | |
# To get a summary of the regression output, I can just type: | |
summary(myFancyRegression) | |
# If you want to show your regression output in something like | |
# a word file, just copy the text from the R window and then format | |
# it with a font like "Courier New", which is monospace (so everything | |
# lines up right) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment