dnorm(x, mean = 0, sd = 1, log = FALSE)
pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
rnorm(n, mean = 0, sd = 1)
Applied Statistics – A Practical Course
2024-11-25
sin(x)
, log(x)
, plot(x, y)
, summary(x)
, anova(lm.object)
, mean(x)
, monod(S, vmax, ks)
, simulate_phytoplankton(N, P, T, Zoo, ...)
Functions in R
Parentheses and arguments
log(x)
par()
par <- c(a=5, b=3)
\(\rightarrow\) here, par
is a variable, c()
a function
Return value and/or side effect
sin(x)
, log(x)
, mean(x)
are functions with return valueprint(x)
, plot(x, y)
are functions with side effecthist(x)
is a function with both, side effect and return valuePredefined and user-defined functions
Usage
x, q | vector of quantiles. |
p | vector of probabilities. |
n | number of observations. If length(n) > 1, the length is taken to be … |
log.p | if TRUE, probabilities p are given as log(p). |
lower.tail | if TRUE (default), … |
Arguments
=
allows to specify arguments in arbitrary orderplot.default
Object orientation
plot
is a generic functionplot.default
is the basic function...
see ?par
for additional graphical parameters, e.g.:col |
color |
bg |
background color for two-color symbols |
pch |
symbol (plotting character) |
cex |
size of symbol (character extension) |
lty |
line type |
lwd |
line width |
\[ v = \frac{v_{max} \cdot S}{k_S + S} \]
par(mar=c(4,4,1,1))
par(mfrow=c(3, 1))
monod <- function(S, vmax, ks) {
vmax * S / (ks + S)
}
S <- 1:10
P <- seq(0, 20, 0.1)
kP <- 5; mumax <- 1.2;
## different ways to call the function
plot(S, monod(S, 2, 2)) # simple call
plot(P, monod(S=P, vmax=mumax, ks=kP)) # named arguments
plot(P, monod(P, mumax, kP)) # argument position
\[ I_t = 997 - 816 \cos(2 \pi t / 365) + 126 \sin(2 \pi t / 365) \]
Functions as a knowledge base
\[ c_{O_2, 100\%} = ... ? \]
o2sat <- function(t) {
K <- t + 273.15 # Celsius to Kelvin
exp(-139.34411 + (157570.1/K) - (66423080/K^2) +
(1.2438e+10/K^3) - (862194900000/K^4))
}
o2sat(20)
[1] 9.092426
A more precise formula is found in package marelac
consult ?gas_O2sat
for citations.
Variables in a function are local:
Lexical Scoping
Now outcomment:
and try again.
The logistic growth function describes saturated growth of a population abundance \(N_t\), dependent of an initial value \(N_0\), growth rate \(r\) and carrying capacity \(K\).
\[ N_t = \frac{K N_0 e^{rt}}{K + N_0 (e^{rt}-1)} \]
with
\(P\) | predation rate |
\(N\) | abundance of prey |
\(P_{max}\) | maximum predation rate |
\(k\) | a constant |
\(\alpha\) | attack rate |
\(H\) | handling time |
\(b\) | exponent \(>1\) |
More presentations
Manuals
More details in the official R manuals, especially in An Introduction to R
Videos
Many videos can be found on Youtube, at the Posit webpage and somewhere else.