# source(here::here("formatting", "functions", "rmd_functions.R")) # source(here::here("rmd_tools", "find_file.R")) require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
b
in the lab instructions.curve()
to plot your function.An example exploratory plotting exercise:
png("lab_05_q1.png", width = 1200, height = 800, res = 200) curve(exp_fun(x, 1.9, 0.1), add = FALSE, from = 0, to = 50, ann = FALSE, axes = FALSE, ylab = "f(x)"); box() curve(exp_fun(x, 1.2, 0.1), add = TRUE, from = 0, to = 100, col = 2) curve(exp_fun(x, 1.2, 0.3), add = TRUE, from = 0, to = 100, col = 3, lty = 2, lwd = 3) curve(exp_fun(x, 1.5, 0.03), add = TRUE, from = 0, to = 100, col = 4, lwd = 2) curve(exp_fun(x, 1.75, 0.003), add = TRUE, from = 0, to = 100, col = 66, lwd = 3, lty = 4) dev.off()
knitr::include_graphics(find_file("lab_05_q1.png"))
exp_fun = function(x, a, b) { return(a * exp(-b * x)) } # curve(exp_fun(x, 1.9, 0.1), add = FALSE, from = 0, to = 30, ann = FALSE, axes = FALSE); box() curve(exp_fun(x, 1.9, 0.1), add = FALSE, from = 0, to = 30, ylab = "f(x)") curve(exp_fun(x, 1.9, 0.3), add = TRUE, from = 0, to = 100, col = 1, lty = 2) curve(exp_fun(x, 1.2, 0.2), add = TRUE, from = 0, to = 100, col = 2, lty = 1) curve(exp_fun(x, 1.2, 0.4), add = TRUE, from = 0, to = 100, col = 2, lty = 2)
extitle: Exponential plots
extype: string
exsolution: nil
exname: lab 5
exsection: lab 5
exextra[essay,logical]: TRUE
exextra[essay_format,character]: editor
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 10
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 5
# source(here::here("formatting", "functions", "rmd_functions.R")) # source(here::here("rmd_tools", "find_file.R")) require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Consider the plot you made with the 4 exponential curves.
Observe how the curves vary as you change the two parameters' values.
a
sets the y-intercept.b
sets the [initial] steepness of the decay.exp_fun = function(x, a, b) { return(a * exp(-b * x)) } curve(exp_fun(x, 1.9, 0.1), add = FALSE, from = 0, to = 30, col = 1, ylab = "f(x)") curve(exp_fun(x, 1.9, 0.3), add = TRUE, from = 0, to = 100, col = 1, lty = 2) curve(exp_fun(x, 1.2, 0.2), add = TRUE, from = 0, to = 100, col = 2, lty = 1) curve(exp_fun(x, 1.2, 0.4), add = TRUE, from = 0, to = 100, col = 2, lty = 2) curve(exp_fun(x, 0.9, 0.4), add = TRUE, from = 0, to = 100, col = 3, lty = 2)
extitle: Exponential parameters
extype: string
exsolution: nil
exname: lab 5
exsection: lab 5
exextra[essay,logical]: TRUE
exextra[essay_format,character]: editor
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 15
expoints: 4
# source(here::here("formatting", "functions", "rmd_functions.R")) # source(here::here("rmd_tools", "find_file.R")) # dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) require(rmd.utils) exp_fun = function(x, a, b) { return(a * exp(-b * x))} ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))} # exp_fun = function(x, a, b) { return(a * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
curve()
to plot the Ricker function.An example exploratory plotting exercise:
# png("lab_05_q3.png", width = 1200, height = 800, res = 200) curve(ricker_fun(x, 25.1, .5), add = FALSE, from = 0, to = 10, ann = FALSE, ylab = "f(x)", ylim = c(0, 30)); box() curve(ricker_fun(x, 20.2, .3), add = TRUE, col = 2) curve(ricker_fun(x, 49.2, .66), add = TRUE, col = 3, lty = 2, lwd = 3) curve(ricker_fun(x, 10.5, 0.8), add = TRUE, col = 4, lwd = 2) # dev.off()
knitr::include_graphics(find_file("lab_05_q3.png"))
curve(ricker_fun(x, 25, 0.1), add = FALSE, from = 0, to = 50, ylab = "f(x)") curve(ricker_fun(x, 20, 0.1), add = TRUE, col = 1, lty = 2) curve(ricker_fun(x, 10, 0.1), add = TRUE, col = 1, lty = 2) curve(ricker_fun(x, 75, 0.3), add = TRUE, col = 2, lty = 1) curve(ricker_fun(x, 50, 0.3), add = TRUE, col = 2, lty = 2) curve(ricker_fun(x, 40, 0.3), add = TRUE, col = 2, lty = 2)
extitle: Ricker plots
extype: string
exsolution: nil
exname: lab 5
exsection: lab 5
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: FALSE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
curve()
to plot the Ricker function.An example exploratory plotting exercise:
curve(ricker_fun(x, 25.1, .5), add = FALSE, from = 0, to = 10, ann = FALSE, ylab = "f(x)", ylim = c(0, 30)); box() curve(ricker_fun(x, 20.2, .3), add = TRUE, col = 2) curve(ricker_fun(x, 49.2, .66), add = TRUE, col = 3, lty = 2, lwd = 3) curve(ricker_fun(x, 10.5, 0.8), add = TRUE, col = 4, lwd = 2) # curve(ricker_fun(x, 1.75, 0.003), add = TRUE, from = 0, to = 100, col = 66, lwd = 3, lty = 4)
curve(ricker_fun(x, 25, 0.1), add = FALSE, from = 0, to = 50, ylab = "f(x)") curve(ricker_fun(x, 20, 0.1), add = TRUE, col = 1, lty = 2) curve(ricker_fun(x, 10, 0.1), add = TRUE, col = 1, lty = 2) curve(ricker_fun(x, 75, 0.3), add = TRUE, col = 2, lty = 1) curve(ricker_fun(x, 50, 0.3), add = TRUE, col = 2, lty = 2) curve(ricker_fun(x, 40, 0.3), add = TRUE, col = 2, lty = 2)
extitle: Ricker params
extype: string
exsolution: nil
exname: lab 5
exsection: lab 5
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: FALSE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv"))
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
data.frame
called dat_dispersal
.dat_dispersal plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders")
extitle: salamander data scatterplot
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: editor
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 5
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) # Calculates the value of x for a linear function, given the coordinates # of a known point (x1, y1) and the slope of the line. line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
cat(readLines(here::here("formatting", "css", "styles.css")))
Recall the function we have used to visually estimate linear model parameters:
# Calculates the value of x for a linear function, given the coordinates # of a known point (x1, y1) and the slope of the line. line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
Instructions:
line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
For example:
plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders") curve(line_point_slope(x, 1400, 0.1, -0.0001), add = TRUE)
extitle: salamander linear model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 2
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
exp_fun = function(x, a, b) { return(a * exp(-b * x))}
plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders\nExponential model") curve(exp_fun(x, 0.75, 0.003), add = TRUE)
extitle: salamander linear model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))} ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))}
For example:
plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders\nRicker model") curve(ricker_fun(x, 0.007, 1/200), add = TRUE)
extitle: salamander ricker model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) # Calculates the value of x for a linear function, given the coordinates # of a known point (x1, y1) and the slope of the line. line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
cat(readLines(here::here("formatting", "css", "styles.css")))
Recall the function we have used to visually estimate linear model parameters:
# Calculates the value of x for a linear function, given the coordinates # of a known point (x1, y1) and the slope of the line. line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
Instructions:
resids_linear
.line_point_slope = function(x, x1, y1, slope) { get_y_intercept = function(x1, y1, slope) return(-(x1 * slope) + y1) linear = function(x, yint, slope) return(yint + x * slope) return(linear(x, get_y_intercept(x1, y1, slope), slope)) }
For example:
param_x1 = 1400 param_y1 = 0.1 param_slope = -0.0001 plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders\nLinear model") curve(line_point_slope(x, param_x1, param_y1, param_slope), add = TRUE) resids_linear = line_point_slope(dat_dispersal$dist.class, param_x1, param_y1, param_slope) - dat_dispersal$disp.rate.ftb hist(resids_linear, main = "residuals: linear model")
extitle: salamander linear model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
resids_exp
.exp_fun = function(x, a, b) { return(a * exp(-b * x))} plot(dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders\nExponential model") exp_a = 0.75 exp_b = 0.003 curve(exp_fun(x, exp_a, exp_b), add = TRUE) resids_exp = exp_fun(dat_dispersal$dist.class, exp_a, exp_b) - dat_dispersal$disp.rate.ftb hist(resids_exp)
extitle: salamander linear model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
require(rmd.utils) dat_dispersal = read.csv(find_file("salamander_dispersal.csv")) exp_fun = function(x, a, b) { return(a * exp(-b * x))} ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))}
cat(readLines(here::here("formatting", "css", "styles.css")))
Instructions:
Using the parameters you chose for your Ricker curve fit, calculate the residuals and store them in a vector resids_ricker
.
Using the scatterplot of the dispersal data, visually fit a ricker curve model.
ricker_fun = function(x, a, b) { return(a * x * exp(-b * x))}
For example:
plot( dat_dispersal$dist.class, dat_dispersal$disp.rate.ftb, xlab = "distance class", ylab = "standardized dispersal rate", main = "Marbled Salamander - first time breeders\nRicker model") rick_a = 0.007 rick_b = 1/200 resids_ricker = ricker_fun(dat_dispersal$dist.class, rick_a, rick_b) - dat_dispersal$disp.rate.ftb hist(resids_ricker) curve(ricker_fun(x, rick_a, rick_b), add = TRUE)
extitle: salamander ricker model
extype: string
exsolution: nil
exname: lab 4
exsection: lab 4
exextra[essay,logical]: TRUE
exextra[essay_format,character]: noinline
exextra[essay_required,logical]: TRUE
exextra[essay_fieldlines,numeric]: 5
exextra[essay_attachments,numeric]: 1
exextra[essay_attachmentsrequired,logical]: TRUE
expoints: 4
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.