This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
## data is in honey.txt (short version), .xls file from reporter, and there are two data frames in honey.Rdata; one from 2000 on
load("honey.Rdata")
head(honey)
## year colonies pounds average fiveyear runmean
## 1 1970 123160 7636 62 NA NA
## 2 1971 115000 7245 63 NA NA
## 3 1972 103200 6708 65 NA 67.60175
## 4 1973 97900 9203 94 NA 71.80162
## 5 1974 99900 5395 54 68 72.80162
## 6 1975 104000 8632 83 72 74.80162
library(ggplot2)
## fiveyear is the mean of the preceding 5 years, to correspond to reporter's calculatuions
## runmean is a central five year mean, e.g. in 1972 the average of 1970,1,2,3,4
## various plots show smooths of average honey over time, all seem to indicate a drop after about 2002
honeyplot <- ggplot(data=honey,aes(year,average)) # nothing will be plotted yet
honeyplot+geom_point(aes(honey$year,honey$average))+scale_x_continuous(breaks = 1970:2014) +theme(axis.text.x = element_text(angle=45))
## this makes the x axis look like the excel plot that was sent
honeyplot + geom_line(aes(honey$year,honey$runmean),col = "green",size=1.5) + geom_point(aes(honey$year,honey$average))+scale_x_continuous(breaks=1970:2014) + theme(axis.text.x = element_text(angle=45))+geom_smooth(method="loess", span = .5) + geom_smooth(method="loess",span=.75,se=F) + scale_y_continuous(breaks=seq(0,140,by=10))
## Warning: Removed 4 rows containing missing values (geom_path).
# this puts two loess smooths, as well as the running mean
honey.lo <- loess(average ~ year, span = 0.5, data = honey) # loess smoother, span can be changed
## qqnorm of residuals looks okay
honey.lo2 <- loess(average ~ year, span = 0.5,
family = "symmetric", data = honey) # robust version; looks same
honey.ss <- smooth.spline(honey$year,honey$average) # again may smoothing parameters tried (spar = .5:.75 by .05)
library(sm)
## Package 'sm', version 2.2-5.4: type help(sm) for summary information
hcv(honey$year, honey$average, display = "lines", ngrid=32) #cross-validation in library sm
## [1] 4.665511
honey.sm <- sm.regression(honey$year, honey$average, h = 4.6655, se = T) # using 'best' h in sm
plot(honey$year, honey$average) #old-fashioned R plots
lines(honey.ss)
lines(honey.lo$x, honey.lo$fitted, col="red")
lines(honey.sm$eval.points,honey.sm$estimate,col="blue")
lines(honey$year,honey$runmean,col="green")
lines(smooth.spline(honey$year,honey$average,spar = 0.5)) # and so on (see plot manysmooth)
legend(1974,130, legend = c("RunningAverage", "loess", "splines"), col = c("green", "red", "black"), lty = c(2,1,1), merge=T)
## tried a few changepoint tests with cpt.mean, on original data and on data from 2006
library(changepoint)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.1.3
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Successfully loaded changepoint package version 1.1.5
## Created on 2014-06-25
## Substantial changes to the structure of the package have occured from version 1.0. Please see the package NEWS for details.
plot(cpt.mean(honey$average))
plot(cpt.mean(honey$average[honey$year > 2000])) # and so on -- not very convincing
## and tried smoothing just from 2000 onwards, using honeyshort
honeyshort <- honey[31:45,]
honeyshortplot <- ggplot(honeyshort,aes(x=year,y=average))+geom_point() + geom_smooth() # for exxample
honeyshortplot+geom_smooth()+scale_x_continuous(breaks = 2000:2014)+geom_line(aes(year,average))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## didn't succeed in really seeing how many points went into each 'running smooth' using the various methods