Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

R code #1

Merged
merged 2 commits into from

2 participants

@HarlanH

No description provided.

@Workman Workman merged commit 28e9786 into Workman:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 3, 2011
  1. @HarlanH

    simple by state data

    HarlanH authored
  2. @HarlanH

    work

    HarlanH authored
This page is out of date. Refresh to see the latest.
Showing with 113 additions and 0 deletions.
  1. +56 −0 TenureByCounty.R
  2. +57 −0 TenureByState.R
View
56 TenureByCounty.R
@@ -0,0 +1,56 @@
+options(stringsAsFactors=FALSE)
+
+library(plyr)
+library(ggplot2)
+library(reshape)
+library(maps)
+library(RCurl)
+library(stringr)
+
+dat.key <- '0AjGTI9tB9Lv_dHA2LTZ3Y2JUbDNOOEpmZG83SjVEbVE'
+
+dat.form = getForm("http://spreadsheets0.google.com/spreadsheet/pub",
+ hl ="en", key = dat.key,
+ single = "true", gid ="9",
+ output = "csv",
+ .opts = list(followlocation = TRUE, verbose = FALSE))
+
+dat <- read.csv(textConnection(dat.form))
+names(dat) <- tolower(names(dat))
+
+# remove rows without counties
+dat <- subset(dat, subset=str_detect(county, '\\\\'))
+dat$state_name <- str_extract(dat$county, '[A-Za-z ]+')
+
+ggplot(subset(dat, avg_govt_payments.2002>0 & tenure_2_or_less.2002>0 & tenure_2_or_less.2007>0),
+ aes(avg_govt_payments.2002/1000, log(tenure_2_or_less.2007/tenure_2_or_less.2002))) +
+ geom_point() + stat_smooth(method='lm', se=FALSE, size=2) +
+ facet_wrap(~ state_name) +
+ scale_x_continuous('2002 Average Government Subsidies ($K)', breaks=c(0,25,50,75)) +
+ scale_y_continuous('log(2007 New Farmers / 2002 New Farmers)') +
+ coord_cartesian(ylim=c(-2,2))
+
+dat.tenure <- (dat[,3:10])
+
+dat.tenure.2007 <- (dat.tenure[,grepl('2007', names(dat.tenure))])
+dat.tenure.2007$year <- 2007
+dat.tenure.2007$state_name <- dat$state_name
+dat.tenure.2007$county_name <- dat$geo
+names(dat.tenure.2007) <- str_replace(names(dat.tenure.2007), '.2007', '')
+
+dat.tenure.2002 <- (dat.tenure[,grepl('2002', names(dat.tenure))])
+dat.tenure.2002$year <- 2002
+dat.tenure.2002$state_name <- dat$state_name
+dat.tenure.2002$county_name <- dat$geo
+names(dat.tenure.2002) <- str_replace(names(dat.tenure.2002), '.2002', '')
+
+dat.tenure <- rbind(dat.tenure.2002, dat.tenure.2007)
+dat.tenure <- melt(dat.tenure, measure.vars=1:4, variable_name='tenure')
+dat.tenure$tenure_num <- as.numeric(str_extract(dat.tenure$tenure, '\\d+'))
+dat.tenure$new <- dat.tenure$tenure_num < 10
+
+dat.tenure.bystate <- ddply(dat.tenure, .(year, state_name),
+ summarise, prop_new_farmers=sum(value*new)/sum(value))
+dat.tenure.bystate$state_name <- tolower(dat.tenure.bystate$state_name)
+dat.tenure.bystate <- cast(dat.tenure.bystate, formula=state_name ~ year)
+names(dat.tenure.bystate) <- c('state', 'prop.new.2002', 'prop.new.2007')
View
57 TenureByState.R
@@ -0,0 +1,57 @@
+options(stringsAsFactors=FALSE)
+
+library(plyr)
+library(ggplot2)
+library(reshape)
+library(maps)
+library(RCurl)
+library(stringr)
+
+new.farmers.state.key <- '0AjGTI9tB9Lv_dHA2LTZ3Y2JUbDNOOEpmZG83SjVEbVE'
+farmers.tenure.state.key <- '0AjGTI9tB9Lv_dHA2LTZ3Y2JUbDNOOEpmZG83SjVEbVE'
+
+nfs.form = getForm("http://spreadsheets0.google.com/spreadsheet/pub",
+ hl ="en", key = new.farmers.state.key,
+ single = "true", gid ="1",
+ output = "csv",
+ .opts = list(followlocation = TRUE, verbose = TRUE))
+
+new.farmers.state <- read.csv(textConnection(nfs.form))
+names(new.farmers.state) <- tolower(names(new.farmers.state))
+new.farmers.state <- mutate(new.farmers.state,
+ state = tolower(state),
+ newnumber = as.numeric(newnumber),
+ newlending = as.numeric(str_replace_all(newlending, '\\D', '')),
+ totalnumber = as.numeric(totalnumber),
+ totallending = as.numeric(str_replace_all(totallending, '\\D', '')),
+ pcttonew_num = newnumber/totalnumber,
+ pcttonew_lending = newlending/totallending)
+new.farmers.state$pcttonew <- NULL
+
+fts.form = getForm("http://spreadsheets0.google.com/spreadsheet/pub",
+ hl ="en", key = farmers.tenure.state.key,
+ single = "true", gid ="5",
+ output = "csv",
+ .opts = list(followlocation = TRUE, verbose = FALSE))
+
+farmers.tenure.state <- read.csv(textConnection(fts.form))
+names(farmers.tenure.state) <- tolower(names(farmers.tenure.state))
+
+farmers.tenure.state$state <- tolower(farmers.tenure.state$state)
+farmers.tenure.state$total <- rowSums(as.matrix(farmers.tenure.state[,2:5]))
+
+dat.all <- join(new.farmers.state, farmers.tenure.state)
+
+
+ggplot(dat.all, aes(pcttonew_num, less.than.3.years/total)) + geom_point()
+
+# compare per-state lending to new farmers to total(increase) in new farmers
+dat.2 <- merge(dat.all, dat.tenure.bystate)
+
+ggplot(dat.2, aes(pcttonew_lending, prop.new.2007/prop.new.2002, label=state)) +
+ geom_text() + stat_smooth() +
+ xlab('% of Farm Service Agency Lending to New Farmers (2010)') +
+ ylab('Change of Proportion of New Farmers (2007 vs 2002)')
+
+
+ggplot(dat.2, aes(newlending, prop.new.2007/prop.new.2002)) + geom_point() + stat_smooth()
Something went wrong with that request. Please try again.