Skip to content

Instantly share code, notes, and snippets.

@fgregg
Last active December 17, 2015 08:09
Show Gist options
  • Select an option

  • Save fgregg/5578318 to your computer and use it in GitHub Desktop.

Select an option

Save fgregg/5578318 to your computer and use it in GitHub Desktop.
# Read in the data
days <- read.csv('all_years.csv')
# Plot all the data
plot(total_count ~ temp_max, days, col=rgb(0,0,0, .3))
# Fit a cubic polynomial to the data
m1 <- lm(total_count ~ temp_max + I(temp_max^2) + I(temp_max^3), days)
# Extract the coefficents
b_0 <- coef(m1)[1]
b_1 <- coef(m1)[2]
b_2 <- coef(m1)[3]
b_3 <- coef(m1)[4]
# Plot the curve
curve(b_0 + b_1*x + b_2*x^2 + b_3*x^3,
min(days$temp_max),
max(days$temp_max),
add=TRUE,
col="red")
# Output as a png
pdf("crime_vs_temp.png")
plot(total_count ~ temp_max, days, col=rgb(0,0,0, .3))
curve(b_0 + b_1*x + b_2*x^2 + b_3*x^3,
min(days$temp_max),
max(days$temp_max),
add=TRUE,
col="red")
dev.off()
@fgregg
Copy link
Copy Markdown
Author

fgregg commented May 14, 2013

Image

The decline in crime at higher temperatures is much smaller.

Also notice that instead of one cloud of points it looks like there's two. This calls out for more investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment