R Markdown

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:

library(openair)
## Loading required package: maps
## 
##  # maps v3.1: updated 'world': all lakes moved to separate new #
##  # 'lakes' database. Type '?world' or 'news(package="maps")'.  #
library(plyr)
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:maps':
## 
##     ozone
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
ytata1 <- read.csv("yield_trend.csv")
ytata <- as.data.frame(ytata1)
View(ytata)
attach(ytata)

# Wet season

ymodel <- lm(formula = yield_wet ~ year, data = ytata)
summary(ymodel)
## 
## Call:
## lm(formula = yield_wet ~ year, data = ytata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.2072 -0.2318 -0.0732  0.3363  0.6632 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.594e+02  1.903e+01  -13.63 2.37e-13 ***
## year         1.311e-01  9.513e-03   13.78 1.85e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4066 on 26 degrees of freedom
## Multiple R-squared:  0.8795, Adjusted R-squared:  0.8749 
## F-statistic: 189.8 on 1 and 26 DF,  p-value: 1.849e-13
equation = function(x){
  lm_coef <- list(a = round(coef(x)[1], digits = 2),
                  b = round(coef(x)[2], digits = 2),
                  r2 = round(summary(x)$r.squared, digits = 2));
  lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2 ~ "=" ~ r2,lm_coef)
  as.character(as.expression(lm_eq));
}

ymodel1 <- ggplot(ytata, aes(x = year, y = yield_wet)) + geom_point() + geom_smooth(method = lm) + scale_x_continuous(name = "Year") + 
      scale_y_continuous(name = "Yield (ton/ha)") + ggtitle("(a)        Crop Yield Trend for Corn, Wet Season 1987 - 2014") + annotate("rect", xmin = 1999, xmax = 2011, ymin = 0.8, ymax = 1.2, fill = "white", colour = "red") + 
      annotate("text", x = 2005, y = 1, label = equation(ymodel), parse = TRUE)

# Dry season

ymodel0 <- lm(formula = yield_dry ~ year, data = ytata)
summary(ymodel0)
## 
## Call:
## lm(formula = yield_dry ~ year, data = ytata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.7255 -0.2774 -0.0311  0.1611  0.7548 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.837e+02  1.740e+01  -10.56 6.71e-11 ***
## year         9.339e-02  8.696e-03   10.74 4.71e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3717 on 26 degrees of freedom
## Multiple R-squared:  0.816,  Adjusted R-squared:  0.809 
## F-statistic: 115.3 on 1 and 26 DF,  p-value: 4.707e-11
equation = function(x){
  lm_coef <- list(a = round(coef(x)[1], digits = 2),
                  b = round(coef(x)[2], digits = 2),
                  r2 = round(summary(x)$r.squared, digits = 2));
  lm_eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(R)^2 ~ "=" ~ r2,lm_coef)
  as.character(as.expression(lm_eq));
}

p1 <- ymodel2 <- ggplot(ytata, aes(x = year, y = yield_dry)) + geom_point() + geom_smooth(method = lm) + scale_x_continuous(name = "Year") + 
      scale_y_continuous(name = "Yield (ton/ha)") + ggtitle("(b)       Crop Yield Trend for Corn, Dry Season 1987 - 2014") + annotate("rect", xmin = 1999, xmax = 2011, ymin = 0.8, ymax = 1.2, fill = "white", colour = "red") + 
      annotate("text", x = 2005, y = 1, label = equation(ymodel0), parse = TRUE)


grid.arrange(ymodel1, ymodel2, ncol = 2, nrow = 1)