Linear Regression

Summary Output

linREGSUM <- function(x=runif(10), y=runif(10)){
  linMOD <- lm(y~x)
  myOUT <- summary(linMOD)
  return(myOUT)
}

linREGSUM()
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.39368 -0.15105 -0.04351  0.19356  0.37839 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   0.7087     0.1798   3.943  0.00428 **
## x            -0.5642     0.3382  -1.668  0.13383   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2895 on 8 degrees of freedom
## Multiple R-squared:  0.2581, Adjusted R-squared:  0.1653 
## F-statistic: 2.783 on 1 and 8 DF,  p-value: 0.1338
##With dummy data

linREGSUM(x=rep(1,10), y=runif(10))
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.36216 -0.19064  0.05141  0.14800  0.35632 
## 
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.51035    0.08159   6.255 0.000149 ***
## x                 NA         NA      NA       NA    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.258 on 9 degrees of freedom

Linear Regression

Plot Output

linREGPLT <- function(x=runif(10), y=runif(10)){
  linMOD <- lm(y~x)
  plot(y=y,x=x,pch=21,bg="lightblue",cex=2, main="Linear Regression", xlab="Generations of Exposure", ylab="Fecundity")
abline(linMOD, add=TRUE)
}

linREGPLT()
## Warning in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...): "add"
## is not a graphical parameter

##With dummy data

linREGPLT(x=runif(10), y=runif(10))
## Warning in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...): "add"
## is not a graphical parameter

ANOVA

Summary Output

aovSUM <- function(x=as.factor(rep(c("Control","Toxic"),each=5)), y=c(rgamma(5,shape=5,scale=5),rgamma(5,shape=5,scale=10))){
  aovMOD <- aov(y~x)
  myOUT <- summary(aovMOD)
  return(myOUT)
}

aovSUM()
##             Df Sum Sq Mean Sq F value  Pr(>F)   
## x            1   2730  2730.1   17.73 0.00295 **
## Residuals    8   1232   153.9                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##With dummy data

aovSUM(x=as.factor(rep(c("Maine","New Jersey"),each=5)), y=c(rgamma(8,shape=5,scale=5),rgamma(2,shape=5,scale=10)))
##             Df Sum Sq Mean Sq F value Pr(>F)
## x            1  453.8   453.8   1.878  0.208
## Residuals    8 1932.5   241.6

ANOVA

Plot Output

aovPLT <- function(x=as.factor(rep(c("Control","Toxic"),each=5)), y=c(rgamma(5,shape=5,scale=5),rgamma(5,shape=5,scale=10))){
  aovMOD <- aov(y~x)
  boxplot(y~x,col=c("darkolivegreen1","darkgreen"), ylab="Fecundity")
}

aovPLT()

##With dummy data

aovPLT(x=as.factor(rep(c("Maine","New Jersey"),each=5)), y=c(rgamma(8,shape=5,scale=5),rgamma(2,shape=5,scale=10)))

Contingency Table

Summary Output

ContingencySUM <- function(dataMATRIX= rbind(c(90,98,89), c(70,60,20))){
  rownames(dataMATRIX) <- c("Control","Toxic")
  colnames(dataMATRIX) <-c("LowTOX",
                         "MedTOX",
                         "HighTOX")
  myOUT <- print(chisq.test(dataMATRIX))
  return(myOUT)
}

ContingencySUM()
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 19.248, df = 2, p-value = 6.612e-05
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 19.248, df = 2, p-value = 6.612e-05
##With dummy data

ContingencySUM(dataMATRIX = rbind(c(87,66, 77), c(66, 44, 23)))
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 11.331, df = 2, p-value = 0.003463
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 11.331, df = 2, p-value = 0.003463

Contingency Table

Plot Output

ContingencyPLT <- function(dataMATRIX= rbind(c(90,98,89), c(70,60,20))){
  rownames(dataMATRIX) <- c("Control","Toxic")
  colnames(dataMATRIX) <-c("LowTOX",
                         "MedTOX",
                         "HighTOX")
  barplot(height=dataMATRIX,
        beside=TRUE,
        col=c("darkolivegreen1","darkgreen"), ylim=c(0,max(colSums(dataMATRIX))), legend = rownames(dataMATRIX),args.legend=list(
      x=ncol(dataMATRIX) + 3,
      y=max(colSums(dataMATRIX)),
      bty = "n"
    ))
}

ContingencyPLT()

##With dummy data

ContingencySUM(dataMATRIX = rbind(c(87,66, 77), c(66, 44, 23)))
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 11.331, df = 2, p-value = 0.003463
## 
##  Pearson's Chi-squared test
## 
## data:  dataMATRIX
## X-squared = 11.331, df = 2, p-value = 0.003463

Logistic Regression

Summary Output

LogisticSUM <- function(xVar, yVar){
  xVar <- rgamma(n=20,shape=5,scale=5)
yVar <- rbinom(n=20,size=1,p=0.5)
dataFrame <- data.frame(xVar,yVar)
logRegMod <- glm(yVar ~ xVar,
                 data=dataFrame,
                 family=binomial(link="logit"))
summary(logRegMod)
}

LogisticSUM()
## 
## Call:
## glm(formula = yVar ~ xVar, family = binomial(link = "logit"), 
##     data = dataFrame)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.3455  -1.0861  -0.9348   1.2015   1.4415  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.80843    1.12595  -0.718    0.473
## xVar         0.02618    0.04419   0.592    0.554
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 27.526  on 19  degrees of freedom
## Residual deviance: 27.168  on 18  degrees of freedom
## AIC: 31.168
## 
## Number of Fisher Scoring iterations: 4
## With dummy data
LogisticSUM(c(rgamma(n=100,shape=7, scale=1)), c(rbinom(n=100,size=1,p=0.2)))
## 
## Call:
## glm(formula = yVar ~ xVar, family = binomial(link = "logit"), 
##     data = dataFrame)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.6448  -1.0186   0.4447   1.0142   1.4953  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)  
## (Intercept) -2.63367    1.81963  -1.447   0.1478  
## xVar         0.12817    0.07739   1.656   0.0977 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 26.92  on 19  degrees of freedom
## Residual deviance: 23.00  on 18  degrees of freedom
## AIC: 27
## 
## Number of Fisher Scoring iterations: 4

Logistic Regression

Plot Output

LogisticPLT <- function(xVar, yVar){
  xVar <- rgamma(n=20,shape=5,scale=5)
yVar <- rbinom(n=20,size=1,p=0.5)
dataFrame <- data.frame(xVar,yVar)
logRegMod <- glm(yVar ~ xVar,
                 data=dataFrame,
                 family=binomial(link="logit"))
plot(x=dataFrame$xVar, y=dataFrame$yVar,pch=21,bg="gray31",cex=2, xlab = "Treatment Days", ylab = "Survival", main = "Toxic Algal Survival")
curve(predict(logRegMod,data.frame(xVar=x),type="response"),add=TRUE,lwd=2)
}

LogisticPLT()

## With dummy data
LogisticPLT(c(rgamma(n=100,shape=7, scale=1)), c(rbinom(n=100,size=1,p=0.8)))