#Call Packages
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(ggfortify)
#Read csv file
growth.moo <- read.csv("C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/growth.csv")
#Check if imported correctly
glimpse(growth.moo)
## Rows: 48
## Columns: 3
## $ supplement <chr> "supergain", "supergain", "supergain", "supergain", "contro…
## $ diet <chr> "wheat", "wheat", "wheat", "wheat", "wheat", "wheat", "whea…
## $ gain <dbl> 17.37125, 16.81489, 18.08184, 15.78175, 17.70656, 18.22717,…
#Converts diet/supplement into factors
growth.moo$diet <- as.factor(growth.moo$diet)
growth.moo$supplement <- as.factor(growth.moo$supplement)
##See levels associated with a factor ($ grab column we want info)
levels(growth.moo$diet)
## [1] "barley" "oats" "wheat"
levels(growth.moo$supplement)
## [1] "agrimore" "control" "supergain" "supersupp"
#Relevel (creates new referecen lvl by reordering) supplement column
growth.moo <-
mutate(growth.moo,
supplement = relevel(supplement, ref="control"))
#Check if it works
levels(growth.moo$supplement)
## [1] "control" "agrimore" "supergain" "supersupp"
#Calculate mean and sd of gain for all 12 combinations
sumMoo <- growth.moo %>%
group_by(diet, supplement) %>%
summarise(meanGrow = mean(gain))
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by diet and supplement.
## ℹ Output is grouped by diet.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(diet, supplement))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
#Check if it works
sumMoo
## # A tibble: 12 × 3
## # Groups: diet [3]
## diet supplement meanGrow
## <fct> <fct> <dbl>
## 1 barley control 23.3
## 2 barley agrimore 26.3
## 3 barley supergain 22.5
## 4 barley supersupp 25.6
## 5 oats control 20.5
## 6 oats agrimore 23.3
## 7 oats supergain 19.7
## 8 oats supersupp 21.9
## 9 wheat control 17.4
## 10 wheat agrimore 19.6
## 11 wheat supergain 17.0
## 12 wheat supersupp 19.7
#Plot Effect of Supplement on Cow Weight Gain (depends on diet)
ggplot(sumMoo, aes(x = supplement, y = meanGrow)) +
geom_point() +
theme_bw()
#Plot Effect of Supplement on Cow Weight Gain depending on diet #(connecting supplement/growth according to diet)
ggplot(sumMoo, aes(x = supplement, y = meanGrow,
colour = diet, group = diet)) +
geom_point() +
geom_line() +
theme_bw()
#Expand combination of variable for interaction between main effect of #diet and main effect of supplement
model_cow <- lm(gain ~ diet * supplement, data = growth.moo)
#Building the 4 plots
autoplot(model_cow, smooth.colour = NA)
## Warning: `fortify(<lm>)` was deprecated in ggplot2 4.0.0.
## ℹ Please use `broom::augment(<lm>)` instead.
## ℹ The deprecated feature was likely used in the ggfortify package.
## Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## ℹ The deprecated feature was likely used in the ggfortify package.
## Please report the issue at <https://github.com/sinhrks/ggfortify/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 48 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 48 rows containing missing values or values outside the scale range
## (`geom_line()`).
#ANOVA
anova(model_cow)
## Analysis of Variance Table
##
## Response: gain
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 2 287.171 143.586 83.5201 2.999e-14 ***
## supplement 3 91.881 30.627 17.8150 2.952e-07 ***
## diet:supplement 6 3.406 0.568 0.3302 0.9166
## Residuals 36 61.890 1.719
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Summary table
summary(model_cow)
##
## Call:
## lm(formula = gain ~ diet * supplement, data = growth.moo)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.48756 -1.00368 -0.07452 1.03496 2.68069
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.2966499 0.6555863 35.536 < 2e-16 ***
## dietoats -2.8029851 0.9271390 -3.023 0.00459 **
## dietwheat -5.8911317 0.9271390 -6.354 2.34e-07 ***
## supplementagrimore 3.0518277 0.9271390 3.292 0.00224 **
## supplementsupergain -0.8305263 0.9271390 -0.896 0.37631
## supplementsupersupp 2.2786527 0.9271390 2.458 0.01893 *
## dietoats:supplementagrimore -0.2471088 1.3111726 -0.188 0.85157
## dietwheat:supplementagrimore -0.8182729 1.3111726 -0.624 0.53651
## dietoats:supplementsupergain -0.0001351 1.3111726 0.000 0.99992
## dietwheat:supplementsupergain 0.4374395 1.3111726 0.334 0.74060
## dietoats:supplementsupersupp -0.9120830 1.3111726 -0.696 0.49113
## dietwheat:supplementsupersupp -0.0158299 1.3111726 -0.012 0.99043
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.311 on 36 degrees of freedom
## Multiple R-squared: 0.8607, Adjusted R-squared: 0.8182
## F-statistic: 20.22 on 11 and 36 DF, p-value: 3.295e-12
#Calculate mean and sd of gain for all 12 combinations
#n() counts the rows in each group (includes rows w/missing values)
sumMoo <- growth.moo %>%
group_by(diet, supplement) %>%
summarise(
meanGrow = mean(gain),
seGrow = sd(gain)/sqrt(n())
)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by diet and supplement.
## ℹ Output is grouped by diet.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(diet, supplement))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
#Two-way ANOVA
ggplot(sumMoo, aes(x = supplement, y = meanGrow,
colour = diet, group = diet)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = meanGrow - seGrow,
ymax = meanGrow + seGrow), width = 0.1) +
theme_bw()
#Call packages
library(ggplot2)
library(dplyr)
library(lubridate)
library(ggfortify)
#Clear the decks
rm(list=ls())
#Read csv file
limp <- read.csv("C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/limpet.csv")
#Check if it works
glimpse(limp)
## Rows: 24
## Columns: 3
## $ DENSITY <int> 8, 8, 8, 8, 8, 8, 15, 15, 15, 15, 15, 15, 30, 30, 30, 30, 30, …
## $ SEASON <chr> "spring", "spring", "spring", "summer", "summer", "summer", "s…
## $ EGGS <dbl> 2.875, 2.625, 1.750, 2.125, 1.500, 1.875, 2.600, 1.866, 2.066,…
#Plot window (egg production by limpets in two seasons and at four #limpet densities)
ggplot(limp, aes(x = DENSITY, y = EGGS, colour = SEASON)) +
geom_point() +
scale_color_manual(values = c(spring="green", summer="red")) +
theme_bw()
#Construct ANCOVA
limp.mod <- lm(EGGS ~ DENSITY * SEASON, data = limp)
#See what's collected
names(limp.mod)
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "contrasts" "xlevels" "call" "terms"
## [13] "model"
#Plot the 4 autoplots
autoplot(limp.mod, smooth.colour = NA)
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
#ANOVA
anova(limp.mod)
## Analysis of Variance Table
##
## Response: EGGS
## Df Sum Sq Mean Sq F value Pr(>F)
## DENSITY 1 5.0241 5.0241 30.1971 2.226e-05 ***
## SEASON 1 3.2502 3.2502 19.5350 0.0002637 ***
## DENSITY:SEASON 1 0.0118 0.0118 0.0711 0.7925333
## Residuals 20 3.3275 0.1664
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Summary table
summary(limp.mod)
##
## Call:
## lm(formula = EGGS ~ DENSITY * SEASON, data = limp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.65468 -0.25021 -0.03318 0.28335 0.57532
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.664166 0.234118 11.380 3.45e-10 ***
## DENSITY -0.033650 0.008259 -4.074 0.000591 ***
## SEASONsummer -0.812282 0.331092 -2.453 0.023450 *
## DENSITY:SEASONsummer 0.003114 0.011680 0.267 0.792533
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4079 on 20 degrees of freedom
## Multiple R-squared: 0.7135, Adjusted R-squared: 0.6705
## F-statistic: 16.6 on 3 and 20 DF, p-value: 1.186e-05
#Restates model repeating use of lm()
#Provides range, interquartile range, median of residuals as quick ##additional check of residuals
#Coefficient table
##67% of variation in egg production + significant fit to the data
##F = 16.6, df = 3,20, p>0.001, residual SE = 0.4079 w/20 df
#Equation for line fitted: EGGS = Intercept(b) + slope(m) x DENSITY
## EGGSspring = 2.66 - 0.033 x DENSITY
##EGGSsummer = (2.66-0.812)+(-0.033 + 0.033) x DENSITY,
###EGGSsummer = 1.84 - 0.03 x DENSITY
#Summer reduces egg prod. by ~0.812 eggs
#Expanding grid, generates grid of numbers (factorial representation ##of any variables you provide to it, returns a data frame)
expand.grid(FIRST = c("A", "B"), SECOND = c(1, 2))
## FIRST SECOND
## 1 A 1
## 2 B 1
## 3 A 2
## 4 B 2
#Predict() generates fitted values from a model
predict(limp.mod)
## 1 2 3 4 5 6 7 8
## 2.3949692 2.3949692 2.3949692 1.6075953 1.6075953 1.6075953 2.1594217 2.1594217
## 9 10 11 12 13 14 15 16
## 2.1594217 1.3938428 1.3938428 1.3938428 1.6546769 1.6546769 1.6546769 0.9358016
## 17 18 19 20 21 22 23 24
## 0.9358016 0.9358016 1.1499321 1.1499321 1.1499321 0.4777604 0.4777604 0.4777604
#Make some new DENSITY values at which we request predictions
new.x <- expand.grid(DENSITY =
seq(from = 8, to = 45, length.out = 10))
#Check if it works
head(new.x)
## DENSITY
## 1 8.00000
## 2 12.11111
## 3 16.22222
## 4 20.33333
## 5 24.44444
## 6 28.55556
#Change SEASON into factor
limp$SEASON <- as.factor(limp$SEASON)
#Add SEASON to grid
new.x <- expand.grid(
DENSITY = seq(from = 8, to = 45, length.out = 10),
SEASON = levels(limp$SEASON))
#Check if it works
head(new.x)
## DENSITY SEASON
## 1 8.00000 spring
## 2 12.11111 spring
## 3 16.22222 spring
## 4 20.33333 spring
## 5 24.44444 spring
## 6 28.55556 spring
#Generate fits and confidence interval at new.x values
new.y <- predict(limp.mod, newdata=new.x,
interval = 'confidence')
#Check if it works
head(new.y)
## fit lwr upr
## 1 2.394969 2.019285 2.770654
## 2 2.256632 1.931230 2.582034
## 3 2.118294 1.834274 2.402315
## 4 1.979957 1.724062 2.235852
## 5 1.841619 1.595998 2.087241
## 6 1.703282 1.447918 1.958646
#Rename fit produced by predict to EGGS to match OG data
addThese <- data.frame(new.x, new.y)
addThese <- rename(addThese, EGGS = fit)
#Check if it works
head(addThese)
## DENSITY SEASON EGGS lwr upr
## 1 8.00000 spring 2.394969 2.019285 2.770654
## 2 12.11111 spring 2.256632 1.931230 2.582034
## 3 16.22222 spring 2.118294 1.834274 2.402315
## 4 20.33333 spring 1.979957 1.724062 2.235852
## 5 24.44444 spring 1.841619 1.595998 2.087241
## 6 28.55556 spring 1.703282 1.447918 1.958646
#New small data frame containing grid of seasons/densities, + ##predicted confidence values and 95% CI at each of the combinations
#new.x2 with DENSITY set to mean
new.x2 <-expand.grid(DENSITY=mean(limp$DENSITY),
SEASON = levels(limp$SEASON))
#predictions
predEgg <-predict(limp.mod, newdata = new.x2)
#housekeeping
EggAtMeanDens <- data.frame(new.x2, predEgg)
head(EggAtMeanDens)
## DENSITY SEASON predEgg
## 1 24.5 spring 1.83975
## 2 24.5 summer 1.10375
#raw data plot
ggplot(limp, aes(x = DENSITY, y = EGGS, colour = SEASON)) +
geom_point(size = 5) +
#add fits and CIs, don't need to specify Density/Egg
#they are inherited from above
geom_smooth(data = addThese,
aes(ymin = lwr, ymax = upr,
fill = SEASON), stat = 'identity') +
#adjust colours
scale_colour_manual(values = c(spring="green", summer="red")) +
scale_fill_manual(values = c(spring="green", summer="red")) +
#theme
theme_bw()
library(dplyr)
library(ggplot2)
library(lattice) # for some statistical plots
library(plotly) # for ggplotly, a wrapper for interactive ggplots
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(Hmisc) # for extra statistical functions e.g. correlation
##
## Attaching package: 'Hmisc'
## The following object is masked from 'package:plotly':
##
## subplot
## The following objects are masked from 'package:dplyr':
##
## src, summarize
## The following objects are masked from 'package:base':
##
## format.pval, units
library(tidyverse) # Loads R packages for data manipulation, visualization and data handling
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ purrr 1.2.2 ✔ tibble 3.3.1
## ✔ readr 2.2.0 ✔ tidyr 1.3.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ plotly::filter() masks dplyr::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ Hmisc::src() masks dplyr::src()
## ✖ Hmisc::summarize() masks dplyr::summarize()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Custom function to create a correlation plot.
# The function uses circles and colored sections to represent the correlation(s)
panel.corrgram.2 <- function(x, y, z, subscripts, at = pretty(z), scale = 0.8, ...)
{ #Loads grid package with tools, shapes, graph elements
require("grid", quietly = TRUE)
x <- as.numeric(x)[subscripts]
y <- as.numeric(y)[subscripts]
z <- as.numeric(z)[subscripts]
# Converts x, y, z to numeric values and selects observations identified by subscripts
zcol <- level.colors(z, at = at, ...) # Assigns color to z values
for (i in seq(along = z)) # Repeats instructions for all z values that i identifies
{
lims <- range(0, z[i]) # Find range between 0 and z
tval <- 2 * base::pi * # pi calculates angles
seq(from = lims[1], to = lims[2], by = 0.01) # Sequence of values from lower to upper limit, increase by .01
grid.circle(x = x[i], y = y[i], r = .5 * scale,
default.units = "native") # Creates circle at x and y, r = radius
grid.polygon(x = x[i] + .5 * scale * c(0, sin(tval)),
y = y[i] + .5 * scale * c(0, cos(tval)),
default.units = "native",
gp = gpar(fill = zcol[i]))
} # Colored polygon section inside circle
# Sin() and cos() calculate coordinates for shapes
# zcoli[i] determines the fill collor for the current value
}
# Run additional R script to load additional functions.
source("C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/scripts/HighstatLib.R")
# Reading each data set in txt form rather than usual CSV
Sparrows <- read.table(file = "C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/SparrowsElphick.txt", header = TRUE)
Sparrows <- Sparrows[sample(1:nrow(Sparrows)), ] # randomize the order
Godwits <- read.table(file= "C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/Godwits.txt", header=TRUE)
RiceField <- read.table(file="C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/ElphickBirdData.txt", header = TRUE)
Sparrows2 <- read.table(file = "C:/Users/samhe/OneDrive/Documents/Bio 4259/Bio4259_Labs/data/VegSamplesV1.txt", header = TRUE)
# Boxplots and dot plots for the detection of outliers for continuous data.
# boxplot for a single variable
boxplot(Sparrows$wingcrd, range=1.5, ylab = "Wing length (mm)")
# dot plot of a single variable using ggplot
ggplot(data=Sparrows,aes(x=wingcrd,y=order(wingcrd)))+
geom_point()+
xlab("Wing length (mm)")+
ylab("Order of the data")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
# dot plot of a single variable using ggplot with a ggplotly wrapper
ggplotly(ggplot(data=Sparrows,aes(x=tarsus,y=order(tarsus)))+
geom_point()+
xlab("Tarsus (mm)")+
ylab("Order of the data")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()))
# One way to report data with outlier
Sparrows %>% filter(tarsus==31.8)
## wingcrd flatwing tarsus head culmen nalospi wt bandstat initials Year Month
## 1 59 60 31.8 21.9 12.2 8.9 19.9 1 8 2002 6
## Day Location SpeciesCode Sex Age
## 1 19 5 1 4 1
# Dot plots for multiple variables.
# Use Sparrows.
# Combine variables into a matrix
Z <- cbind(Sparrows$wingcrd, Sparrows$tarsus, Sparrows$head,
Sparrows$culmen, Sparrows$nalospi, Sparrows$wt)
# Add column names
colnames(Z) <- c("wing length", "tarsus length", "head length",
"culmen length", "nalospi to bill tip", "weight")
# Reshape data into long format
df <- as.data.frame(Z) %>%
mutate(row_order = row_number()) %>%
pivot_longer(cols = -row_order, names_to = "variable", values_to = "value") %>%
# Keep the exact order of columns instead of alphabetical
mutate(variable = factor(variable, levels = colnames(Z)))
# Original lattice dotplot
dotplot(as.matrix(Z), groups = FALSE,
strip = strip.custom(bg = 'white', par.strip.text = list(cex = 0.8)),
scales = list(x = list(relation = "free"),
y = list(relation = "free"),
draw = FALSE),
col = 1, cex = 0.5, pch = 16,
xlab = "Value of the variable",
ylab = "Order of the data from text file")
# Create faceted ggplot equivalent
Dotplot <- ggplot(df, aes(x = value, y = row_order)) +
geom_point(size = 0.5, color = "darkred") +
facet_wrap(~ variable, scales = "free") +
labs(title = "Ggplotly Of Dotplot",
x = "Value of the variable",
y = "Order of the data from text file") +
theme_bw()
# Task: translate Figure 3 into a ggplot with a ggplotly wrapper
# Render interactive plot
ggplotly(Dotplot)
# Boxplots for the detection of heterogeneity.
# Godwit intake rates
# Creating factors for Sex and Migration period
Godwits$fSEX <- factor(Godwits$SEX, levels = c(0, 1, 2),
labels = c("Not", "Female", "Male"))
Godwits$fPERIOD <- factor(Godwits$PERIOD, levels = c(0, 1, 2),
labels = c("Summer", "Pre-migration", "Winter"))
# Original boxplot using lattice
bwplot(mgconsumed ~ fPERIOD | fSEX, data = Godwits,
strip = strip.custom(bg = 'white'), subset = SEX!=0,
cex = .5, layout = c(2, 1),
xlab = "Migration period", ylab = "Intake rate",
par.settings = list(
box.rectangle = list(col = 1),
box.umbrella = list(col = 1),
plot.symbol = list(cex = .5, col = 1)),
scales = list(x = list(relation = "same"),
y = list(relation = "same")))
# Task: Change the colour scheme (was blue/green)
# ggplot equivalent plot
ggplot(data=filter(Godwits,fSEX != "Not"),
aes(x=fPERIOD,y=mgconsumed,fill=fSEX))+
geom_boxplot()+
scale_fill_manual(values=c("red","skyblue"),name="Sex")+
xlab("Migration period")+
ylab("Intake rate")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
facet_grid(~fSEX)
# Task: Add a plot to show each sex within each period
# ggplotly equivalent with shown sex within each period
plotly_Godwits <- ggplot(data = filter(Godwits, fSEX != "Not"),
aes(x = fPERIOD, y = mgconsumed, fill = fSEX)) +
geom_boxplot() +
# Updated color scheme
scale_fill_manual(values = c("#1f77b4", "#ff7f0e"), name = "Sex") +
xlab("Migration period") +
ylab("Intake rate") +
theme_bw() + # Clean layout theme
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
facet_grid(~fSEX)
# Add the plotly wrapper
ggplotly(plotly_Godwits)
# Show the values associated with a boxplot
?boxplot.stats # get help on what is shown in a boxplot
## starting httpd help server ... done
godwits2 <- Godwits %>%
filter(fSEX=="Male",fPERIOD=="Summer")
boxplot.stats(godwits2$mgconsumed)
## $stats
## [1] 0.03 0.08 0.18 0.36 0.62
##
## $n
## [1] 65
##
## $conf
## [1] 0.125127 0.234873
##
## $out
## [1] 1.07 0.93
IQR(godwits2$mgconsumed)*1.58+median(godwits2$mgconsumed)
## [1] 0.6224
# proof of the extreme of the upper whisker
# Histograms for evaluating distributions and assessing heterogeneity.
# Create factor for Months
Sparrows$fMonth<-factor(Sparrows$Month,
levels = c(5, 6, 7, 8, 9, 10),
labels = c("May", "June", "July", "August",
"Sept.", "Oct."))
# Create variable to identify Months
Sparrows$I1 <- Sparrows$fMonth =="June" |
Sparrows$fMonth =="July" |
Sparrows$fMonth =="August"
# Histogram using all data
hist(Sparrows$wt[Sparrows$I1],
xlab = "Weight (g)", breaks = 30,
main = "", ylab = "Frequency")
# Histograms separated by month
histogram( ~ wt | fMonth, type = "count",
xlab = "Weight (g)",
ylab = "Frequency",
nint=30,layout=c(1,3),
strip.left = strip.custom(bg = 'white'),
strip = F,
col.line = "black", col = "white",
scales = list(x = list(relation = "same"),
y = list(relation = "same"),
draw = TRUE),
subset = fMonth =="June" | fMonth == "July" |fMonth == "August",
data = Sparrows)
# Task make the same plot faceted by month
# ggplot equivalent
Histo <- ggplot(data=filter(Sparrows,I1==TRUE),aes(x=wt))+
geom_histogram(bins=30,col="red")+
xlab("Weight (g)")+
ylab("Frequency")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
# plot faceted by month
facet_grid(~fMonth)
## <ggproto object: Class FacetGrid, Facet, gg>
## attach_axes: function
## attach_strips: function
## compute_layout: function
## draw_back: function
## draw_front: function
## draw_labels: function
## draw_panel_content: function
## draw_panels: function
## finish_data: function
## format_strip_labels: function
## init_gtable: function
## init_scales: function
## map_data: function
## params: list
## set_panel_size: function
## setup_data: function
## setup_panel_params: function
## setup_params: function
## shrink: TRUE
## train_scales: function
## vars: function
## super: <ggproto object: Class FacetGrid, Facet, gg>
# ggplotly of ggplot histogram
ggplotly(Histo)
# Plots to help identify excess of zeros.
# Use RiceField data.
# Creating a frequency plot
plot(table(round(RiceField$AREA * RiceField$AQBIRDS)),
type = "h",
xlim = c(0, 100),
xlab = "Observed values", ylab = "Frequency")
# Recreates the frequency plot as ggplot with a ggplotly wrapper.
# plotly allow the user to interact with the plot (zoom, hover to show values)
ggplotly(ggplot(data=RiceField,aes(x=AREA*AQBIRDS))+
geom_histogram(binwidth=1)+
xlab("Observed values")+
ylab("Frequency")+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()))
# A fun plot of joint species absence in rice fields
# Use RiceField data.
#These are all the bird species (variables in the df)
AllS <- c(
"TUSW", "GWFG", "WHGO", "CAGO", "MALL",
"GADW", "GWTE", "CITE", "UNTE", "AMWI", "NOPI",
"NOSH", "RIDU", "CANV", "BUFF", "WODU", "RUDU",
"EUWI", "UNDU", "PBGB", "SORA", "COOT", "COMO",
"AMBI", "BCNH", "GBHE", "SNEG", "GREG", "WFIB",
"SACR", "AMAV", "BNST", "BBPL", "KILL", "LBCU",
"GRYE", "LEYE", "LBDO", "SNIP", "DUNL", "WESA",
"LESA", "PEEP", "RUFF", "UNSH", "RBGU", "HEGU",
"CAGU", "GUSP")
#Determine species richness (note common and rare species)
Richness <- colSums(RiceField[,AllS] > 0, na.rm = TRUE)
#Remove all covariates, and just keep the species
Birds <- RiceField[,AllS]
#To reduce the of variables in the figure, we only used the
#20 species that occurred at more than 40 sites.
#As a result, N = 20. Else it becomes a mess.
Birds2 <- Birds[, Richness > 40]
N <- ncol(Birds2)
AllNames <- names(Birds2)
# create and fill a matrix with the number of fields
# with neither species i or j
A <- matrix(nrow = N, ncol = N) # initialize matrix
for (i in 1:N){
for (j in 1:N){
A[i,j] <- sum(RiceField[,AllS[i]]==0 & RiceField[,AllS[j]]==0, na.rm=TRUE)
}}
A1 <- A/2035 # convert # to proportion of fields with neither species
print(A1, digits = 2)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] 1.00 0.98 0.98 0.99 0.69 0.96 0.91 0.98 0.99 0.89 0.82 0.83 0.99
## [2,] 0.98 0.98 0.97 0.98 0.69 0.95 0.90 0.97 0.98 0.88 0.81 0.82 0.98
## [3,] 0.98 0.97 0.98 0.98 0.69 0.95 0.90 0.97 0.98 0.88 0.82 0.82 0.98
## [4,] 0.99 0.98 0.98 1.00 0.69 0.96 0.91 0.98 0.99 0.89 0.82 0.83 0.99
## [5,] 0.69 0.69 0.69 0.69 0.69 0.69 0.67 0.69 0.69 0.68 0.66 0.65 0.69
## [6,] 0.96 0.95 0.95 0.96 0.69 0.96 0.89 0.95 0.96 0.87 0.81 0.82 0.96
## [7,] 0.91 0.90 0.90 0.91 0.67 0.89 0.91 0.90 0.91 0.85 0.80 0.81 0.91
## [8,] 0.98 0.97 0.97 0.98 0.69 0.95 0.90 0.98 0.98 0.88 0.81 0.83 0.98
## [9,] 0.99 0.98 0.98 0.99 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [10,] 0.89 0.88 0.88 0.89 0.68 0.87 0.85 0.88 0.89 0.89 0.80 0.80 0.89
## [11,] 0.82 0.81 0.82 0.82 0.66 0.81 0.80 0.81 0.82 0.80 0.82 0.77 0.82
## [12,] 0.83 0.82 0.82 0.83 0.65 0.82 0.81 0.83 0.83 0.80 0.77 0.83 0.83
## [13,] 0.99 0.98 0.98 0.99 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [14,] 1.00 0.98 0.98 1.00 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [15,] 0.99 0.98 0.98 0.99 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [16,] 0.99 0.98 0.98 1.00 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [17,] 1.00 0.98 0.98 1.00 0.69 0.96 0.91 0.98 1.00 0.89 0.82 0.83 1.00
## [18,] 0.99 0.98 0.98 0.99 0.69 0.96 0.91 0.98 0.99 0.89 0.82 0.83 0.99
## [19,] 0.97 0.96 0.96 0.97 0.69 0.94 0.89 0.96 0.97 0.88 0.82 0.82 0.97
## [20,] 0.98 0.97 0.97 0.98 0.69 0.95 0.90 0.97 0.98 0.88 0.81 0.83 0.98
## [,14] [,15] [,16] [,17] [,18] [,19] [,20]
## [1,] 1.00 0.99 0.99 1.00 0.99 0.97 0.98
## [2,] 0.98 0.98 0.98 0.98 0.98 0.96 0.97
## [3,] 0.98 0.98 0.98 0.98 0.98 0.96 0.97
## [4,] 1.00 0.99 1.00 1.00 0.99 0.97 0.98
## [5,] 0.69 0.69 0.69 0.69 0.69 0.69 0.69
## [6,] 0.96 0.96 0.96 0.96 0.96 0.94 0.95
## [7,] 0.91 0.91 0.91 0.91 0.91 0.89 0.90
## [8,] 0.98 0.98 0.98 0.98 0.98 0.96 0.97
## [9,] 1.00 1.00 1.00 1.00 0.99 0.97 0.98
## [10,] 0.89 0.89 0.89 0.89 0.89 0.88 0.88
## [11,] 0.82 0.82 0.82 0.82 0.82 0.82 0.81
## [12,] 0.83 0.83 0.83 0.83 0.83 0.82 0.83
## [13,] 1.00 1.00 1.00 1.00 0.99 0.97 0.98
## [14,] 1.00 1.00 1.00 1.00 1.00 0.97 0.98
## [15,] 1.00 1.00 1.00 1.00 0.99 0.97 0.98
## [16,] 1.00 1.00 1.00 1.00 1.00 0.97 0.98
## [17,] 1.00 1.00 1.00 1.00 1.00 0.97 0.99
## [18,] 1.00 0.99 1.00 1.00 1.00 0.97 0.98
## [19,] 0.97 0.97 0.97 0.97 0.97 0.97 0.96
## [20,] 0.98 0.98 0.98 0.99 0.98 0.96 0.99
# adds species names to matrix
rownames(A1) <- AllNames
colnames(A1) <- AllNames
# create color coded matrix with levelplot
levelplot(A1,xlab=NULL,ylab=NULL,
at=do.breaks(c(0.5,1.01),101),
panel=panel.corrgram.2,
scales=list(x=list(rot=90)),
colorkey=list(space="top"),
col.regions=colorRampPalette(c("red","white","blue")))
# Plots to evaluate collinearity.
# Use Sparrows2 data.
# Determine VIF's
# Select covariates
Z<-Sparrows2[,c("Avgmaxht", "Avgdens", "ht.thatch",
"S.patens", "Distichlis", "S.alternifloraShort",
"S.alternifloraTall", "Juncus", "Bare", "Other",
"Phragmites", "Shrub", "Tallsedge", "Water")]
corvif(Z) # Part of Table 1
## Correlations of the variables
##
## Avgmaxht Avgdens ht.thatch S.patens
## Avgmaxht 1.00000000 -0.22841524 0.466218046 -0.17785017
## Avgdens -0.22841524 1.00000000 0.062575238 0.67411203
## ht.thatch 0.46621805 0.06257524 1.000000000 0.16053731
## S.patens -0.17785017 0.67411203 0.160537307 1.00000000
## Distichlis -0.02337749 0.13160006 0.022321660 -0.10290139
## S.alternifloraShort -0.44042742 -0.35875821 -0.401407857 -0.41441467
## S.alternifloraTall 0.60074164 -0.51803067 0.254760430 -0.55521017
## Juncus 0.17293819 0.32490496 -0.003660471 0.03727048
## Bare -0.23941028 -0.20344863 -0.155596279 -0.05769548
## Other -0.36157539 0.13420907 -0.194936964 0.06081853
## Phragmites 0.38995648 -0.06910767 0.330766492 0.08310766
## Shrub -0.07595686 -0.19996137 -0.141009710 -0.16031227
## Tallsedge 0.28552986 -0.07218760 -0.066039061 0.07970131
## Water -0.25038911 -0.12840907 -0.120468982 -0.17287729
## Distichlis S.alternifloraShort S.alternifloraTall
## Avgmaxht -0.02337749 -0.44042742 0.600741637
## Avgdens 0.13160006 -0.35875821 -0.518030668
## ht.thatch 0.02232166 -0.40140786 0.254760430
## S.patens -0.10290139 -0.41441467 -0.555210170
## Distichlis 1.00000000 -0.17327011 -0.219076843
## S.alternifloraShort -0.17327011 1.00000000 -0.156590051
## S.alternifloraTall -0.21907684 -0.15659005 1.000000000
## Juncus 0.04757495 -0.31812489 -0.231319555
## Bare -0.24631400 -0.03848335 -0.012422149
## Other -0.12816334 0.08430546 -0.201525783
## Phragmites 0.08813589 -0.32519571 0.027006156
## Shrub 0.21464963 0.16155126 -0.173082116
## Tallsedge -0.09316033 -0.15043833 -0.050009373
## Water -0.21243584 0.06676282 0.002440565
## Juncus Bare Other Phragmites
## Avgmaxht 0.172938189 -0.23941028 -0.36157539 0.38995648
## Avgdens 0.324904964 -0.20344863 0.13420907 -0.06910767
## ht.thatch -0.003660471 -0.15559628 -0.19493696 0.33076649
## S.patens 0.037270482 -0.05769548 0.06081853 0.08310766
## Distichlis 0.047574949 -0.24631400 -0.12816334 0.08813589
## S.alternifloraShort -0.318124889 -0.03848335 0.08430546 -0.32519571
## S.alternifloraTall -0.231319555 -0.01242215 -0.20152578 0.02700616
## Juncus 1.000000000 -0.14345423 -0.11858986 0.08711016
## Bare -0.143454233 1.00000000 0.18374555 -0.05895322
## Other -0.118589860 0.18374555 1.00000000 -0.12488041
## Phragmites 0.087110160 -0.05895322 -0.12488041 1.00000000
## Shrub 0.045112426 -0.09935402 0.02376765 0.09161061
## Tallsedge -0.070738224 0.14131312 0.26940954 0.09853599
## Water -0.124503346 0.12718566 -0.07270756 -0.11213772
## Shrub Tallsedge Water
## Avgmaxht -0.07595686 0.28552986 -0.250389111
## Avgdens -0.19996137 -0.07218760 -0.128409073
## ht.thatch -0.14100971 -0.06603906 -0.120468982
## S.patens -0.16031227 0.07970131 -0.172877290
## Distichlis 0.21464963 -0.09316033 -0.212435845
## S.alternifloraShort 0.16155126 -0.15043833 0.066762825
## S.alternifloraTall -0.17308212 -0.05000937 0.002440565
## Juncus 0.04511243 -0.07073822 -0.124503346
## Bare -0.09935402 0.14131312 0.127185661
## Other 0.02376765 0.26940954 -0.072707562
## Phragmites 0.09161061 0.09853599 -0.112137724
## Shrub 1.00000000 -0.04012283 -0.098725195
## Tallsedge -0.04012283 1.00000000 -0.059059880
## Water -0.09872519 -0.05905988 1.000000000
##
##
## Variance inflation factors
## Warning in summary.lm(object, ...): essentially perfect fit: summary may be
## unreliable
## GVIF
## Avgmaxht 6.120018
## Avgdens 3.206401
## ht.thatch 1.671224
## S.patens 159.350658
## Distichlis 53.754540
## S.alternifloraShort 121.463782
## S.alternifloraTall 159.382860
## Juncus 44.995377
## Bare 12.058665
## Other 5.817015
## Phragmites 3.749027
## Shrub 2.781804
## Tallsedge 4.409398
## Water 17.067777
# prepare data for plot
Z <- as.vector(as.matrix(Sparrows2[, c("Avgmaxht", "Avgdens",
"ht.thatch", "S.patens", "Distichlis",
"S.alternifloraShort", "S.alternifloraTall", "Juncus",
"Bare", "Other", "Phragmites", "Shrub", "Tallsedge", "Water")]))
#Setup the data in vector format for the xyplot
Y10 <- rep(Sparrows2$Banded, 14)
# prepare covariate names
MyNames <- names(Sparrows2[,c("Avgmaxht", "Avgdens", "ht.thatch",
"S.patens", "Distichlis", "S.alternifloraShort",
"S.alternifloraTall", "Juncus", "Bare", "Other",
"Phragmites", "Shrub", "Tallsedge", "Water")])
# repeat covariate names and create labelled factor for plot
ID10 <- rep(MyNames, each = length(Sparrows2$Banded))
ID11 <- factor(ID10, labels = c("% Juncus gerardii",
"% Shrub", "Height of thatch", "% Spartina patens",
"% Distichlis", "% Bare ground", "% Other vegetation",
"% Phragmites australis", "% Tall sedge", "% Water",
"% Spartina alterniflora (short)",
"% Spartina alterniflora (tall)",
"Maximum vegetation height",
"Vegetation stem density"),
levels = c("Juncus", "Shrub", "Avgmaxht", "S.patens",
"Distichlis", "Bare", "Other", "Phragmites",
"Tallsedge", "Water", "S.alternifloraShort",
"S.alternifloraTall", "ht.thatch", "Avgdens"))
# create scatterplot using xyplot
xyplot(Y10 ~ Z | ID11, col = 1,
strip = function(bg='white',...) strip.default(bg='white',...),
scales = list(alternating = T,
x = list(relation = "free"),
y = list(relation = "same")),
xlab = "Covariates",
par.strip.text = list(cex = 0.8),
ylab = "Banded",
panel=function(x, y, subscripts,...){
panel.grid(h =- 1, v = 2)
panel.points(x, y, col = 1, pch = 16)
if(ID10[subscripts][1] != "Tallsedge") {panel.loess(x,y,col=1,lwd=2)}
})
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0022278
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0022278
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0022278
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0022278
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0022278
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.0472
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0049
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0049
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0049
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0049
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.0049
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.07
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.012343
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.012343
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.012343
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.012343
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, : at
## -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## radius 0.012343
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## all data on boundary of neighborhood. make span bigger
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## pseudoinverse used at -0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## neighborhood radius 0.1111
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## reciprocal condition number 1
## Warning in simpleLoess(y, x, w, span, degree = degree, parametric = FALSE, :
## zero-width neighborhood. make span bigger
# get correlations
Sparrows2.df <- as.data.frame(Sparrows2)
cor(Sparrows2.df[,c("Avgmaxht", "Avgdens", "ht.thatch",
"S.patens", "Distichlis", "S.alternifloraShort",
"S.alternifloraTall", "Juncus", "Bare", "Other",
"Phragmites", "Shrub", "Tallsedge", "Water")])
## Avgmaxht Avgdens ht.thatch S.patens
## Avgmaxht 1.00000000 -0.22841524 0.466218046 -0.17785017
## Avgdens -0.22841524 1.00000000 0.062575238 0.67411203
## ht.thatch 0.46621805 0.06257524 1.000000000 0.16053731
## S.patens -0.17785017 0.67411203 0.160537307 1.00000000
## Distichlis -0.02337749 0.13160006 0.022321660 -0.10290139
## S.alternifloraShort -0.44042742 -0.35875821 -0.401407857 -0.41441467
## S.alternifloraTall 0.60074164 -0.51803067 0.254760430 -0.55521017
## Juncus 0.17293819 0.32490496 -0.003660471 0.03727048
## Bare -0.23941028 -0.20344863 -0.155596279 -0.05769548
## Other -0.36157539 0.13420907 -0.194936964 0.06081853
## Phragmites 0.38995648 -0.06910767 0.330766492 0.08310766
## Shrub -0.07595686 -0.19996137 -0.141009710 -0.16031227
## Tallsedge 0.28552986 -0.07218760 -0.066039061 0.07970131
## Water -0.25038911 -0.12840907 -0.120468982 -0.17287729
## Distichlis S.alternifloraShort S.alternifloraTall
## Avgmaxht -0.02337749 -0.44042742 0.600741637
## Avgdens 0.13160006 -0.35875821 -0.518030668
## ht.thatch 0.02232166 -0.40140786 0.254760430
## S.patens -0.10290139 -0.41441467 -0.555210170
## Distichlis 1.00000000 -0.17327011 -0.219076843
## S.alternifloraShort -0.17327011 1.00000000 -0.156590051
## S.alternifloraTall -0.21907684 -0.15659005 1.000000000
## Juncus 0.04757495 -0.31812489 -0.231319555
## Bare -0.24631400 -0.03848335 -0.012422149
## Other -0.12816334 0.08430546 -0.201525783
## Phragmites 0.08813589 -0.32519571 0.027006156
## Shrub 0.21464963 0.16155126 -0.173082116
## Tallsedge -0.09316033 -0.15043833 -0.050009373
## Water -0.21243584 0.06676282 0.002440565
## Juncus Bare Other Phragmites
## Avgmaxht 0.172938189 -0.23941028 -0.36157539 0.38995648
## Avgdens 0.324904964 -0.20344863 0.13420907 -0.06910767
## ht.thatch -0.003660471 -0.15559628 -0.19493696 0.33076649
## S.patens 0.037270482 -0.05769548 0.06081853 0.08310766
## Distichlis 0.047574949 -0.24631400 -0.12816334 0.08813589
## S.alternifloraShort -0.318124889 -0.03848335 0.08430546 -0.32519571
## S.alternifloraTall -0.231319555 -0.01242215 -0.20152578 0.02700616
## Juncus 1.000000000 -0.14345423 -0.11858986 0.08711016
## Bare -0.143454233 1.00000000 0.18374555 -0.05895322
## Other -0.118589860 0.18374555 1.00000000 -0.12488041
## Phragmites 0.087110160 -0.05895322 -0.12488041 1.00000000
## Shrub 0.045112426 -0.09935402 0.02376765 0.09161061
## Tallsedge -0.070738224 0.14131312 0.26940954 0.09853599
## Water -0.124503346 0.12718566 -0.07270756 -0.11213772
## Shrub Tallsedge Water
## Avgmaxht -0.07595686 0.28552986 -0.250389111
## Avgdens -0.19996137 -0.07218760 -0.128409073
## ht.thatch -0.14100971 -0.06603906 -0.120468982
## S.patens -0.16031227 0.07970131 -0.172877290
## Distichlis 0.21464963 -0.09316033 -0.212435845
## S.alternifloraShort 0.16155126 -0.15043833 0.066762825
## S.alternifloraTall -0.17308212 -0.05000937 0.002440565
## Juncus 0.04511243 -0.07073822 -0.124503346
## Bare -0.09935402 0.14131312 0.127185661
## Other 0.02376765 0.26940954 -0.072707562
## Phragmites 0.09161061 0.09853599 -0.112137724
## Shrub 1.00000000 -0.04012283 -0.098725195
## Tallsedge -0.04012283 1.00000000 -0.059059880
## Water -0.09872519 -0.05905988 1.000000000
# Multi-scatter plot to evaluate correlations among variables.
# requires the Sparrows data
# creates variable names
MyNames <- c("wing chord", "tarsus length", "head length",
"culmen length", "nalospi to bill tip", "weight")
# creates scatterplot matrix
pairs(Sparrows[,c(1, 3, 4, 5, 6, 7)],
lower.panel = panel.cor, # correlation coefficients
cex.labels=1.3, # label size
labels=MyNames) # apply labels
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter
## Warning in par(usr): argument 1 does not name a graphical parameter