Choices with Functions (Making Choices)
The UHURU experiment
in Kenya has conducted a survey of Acacia drepanolobium among each of their
ungulate exclosure treatments. Data for the survey is available here
in a tab delimited ("\t"
) format. Each of the individuals surveyed were
measured for branch circumference (CIRC
) and canopy width (AXIS1
) and was
identified for the associated ant-symbiont species present (ANT
).
The following function takes a subset of the data for a given ANT
symbiont
and evaluates the linear regression (lm()
) for a given relationship, returning
the symbiont species
used for the subset and the r2
of the model.
report_rsquared <- function(data, species, formula){
subset <- dplyr::filter(data, ANT == species)
test <- lm(formula, data = subset)
rsquared <- round(summary(test)$r.squared, 3)
output <- data.frame(species = species, r2 = rsquared)
return(output)
}
- Execute the function using the UHURU data
and specifying
species = "CM"
andformula = "AXIS1~CIRC"
. - Modify the function so that it also determines
if()
thersquared
is significant based on a giventhreshold
. The modified function shouldreturn()
thespecies
,rsquared
and asignificance
value of"S"
for a relationship with anrsquared > threshold
or"NS"
for anrsquared < threshold
. - Execute your modified function for
species
of"CM"
,"CS"
, and"TP"
given athreshold = 0.667
.