Peter Ralph
8 March 2021 – Advanced Biological Statistics
Visualize:
Response equals smooth plus noise: \[ y_i \approx f(x_i), \] where
To be determined: What’s “smooth”? What’s “plus noise”?
lo_dur <- loess(Duration ~ Start_Latitude * Start_Longitude, data=bt, span=0.3)
pred_lats <- seq(min(bt$Start_Latitude), max(bt$Start_Latitude), length.out=101)
pred_lons <- seq(min(bt$Start_Longitude), max(bt$Start_Longitude), length.out=101)
pred_locs <- expand.grid(Start_Latitude=pred_lats,
Start_Longitude=pred_lons)
pred_duration <- predict(lo_dur, newdata=pred_locs)
Response equals smooth plus noise: \[ y_i = f(x_i) + \epsilon_i, \] where
and \(f(x)\) is spatially autocorrelated:
\[\begin{aligned} f(x_i) &\sim \MVN(\mu, \Sigma) \\ \cov[f(x_i), f(x_j)] &= \rho(\|x_i - x_j\|) . \end{aligned}\]
> help(Krig, package="fields")
Krig package:fields R Documentation
Kriging surface estimate
Description:
Fits a surface to irregularly spaced data. The Kriging model
assumes that the unknown function is a realization of a Gaussian
random spatial processes. The assumed model is additive Y = P(x) +
Z(X) + e, where P is a low order polynomial and Z is a mean zero,
Gaussian stochastic process with a covariance that is unknown up
to a scale constant. The main advantages of this function are the
flexibility in specifying the covariance as an R language function
and also the supporting functions plot, predict, predictSE,
surface for subsequent analysis. Krig also supports a correlation
model where the mean and marginal variances are supplied.
Usage:
Krig(x, Y, cov.function = "stationary.cov", lambda = NA, df
= NA, GCV = FALSE, Z = NULL, cost = 1, knots = NA,
weights = NULL, m = 2, nstep.cv = 200, scale.type =
"user", x.center = rep(0, ncol(x)), x.scale = rep(1,
ncol(x)), rho = NA, sigma2 = NA, method = "REML",
verbose = FALSE, mean.obj = NA, sd.obj = NA,
null.function = "Krig.null.function", wght.function =
NULL, offset = 0, na.rm = TRUE, cov.args = NULL,
chol.args = NULL, null.args = NULL, wght.args = NULL,
W = NULL, give.warnings = TRUE, ...)
gp( )
In brms, we can use gp( )
to add a “Gaussian process” predictor:
## Compiling Stan program...
## Start sampling
A bivariate predictor is specified by:
y ~ gp(x1, x2)
Question: what’s the difference to
y ~ gp(x1) + gp(x2)
?
Gaussian processes don’t scale well. This would take forever:
brsm <- brm(y ~ gp(t), data=yt)
yt$brms_gp <- predict(ytsm, newdata=yt)
Why? With \(n\) observations, the covariance matrix has \(n^2\) entries. Fitting the MVN requires inverting it.
Bézier curves are piecewise polynomial, i.e., splines:
Response equals smooth plus noise: \[ y_i = f(x_i) + \epsilon_i, \] where
and \(f(x)\) is a \(k\)-th order spline with knots \(u_1, \ldots, u_m\): \[\begin{aligned} f(x) &= a_{j0} + a_{j1} x + a_{j2} x^2 + \cdots + a_{jk} x^k \\ \text{for} &\qquad u_j \le x \le u_{j+1} . \end{aligned}\] and ‘smooth’ at the knots.
Using brms
, this is as easy as using s( )
as a predictor:
## Compiling Stan program...
## Start sampling
downtown <- subset(bt, Start_Latitude > 45.51 & Start_Latitude < 45.53
& Start_Longitude > -122.69 & Start_Longitude < -122.67
& (rbinom(nrow(bt), size=1, prob=0.1) == 1))
s_dur <- brm(Duration ~ t2(Start_Latitude, Start_Longitude),
data=downtown)
## Warning: Rows containing NAs were excluded from the model.
## Compiling Stan program...
## Start sampling
Built-in smoothing with ~ gp( )
and ~ s( )
and ~ t2( )
in brms is very promising.
But, it’s not magical (yet).
Recall that a Generalized Linear Model (GLM) has 1. a family describing the distribution of the response, \(Y\) 2. a linear predictor \[\begin{aligned} X \beta = \beta_0 + \beta_1 X_1 + \cdots + \beta_n X_n \end{aligned}\] 3. a link function connecting the two: \[\begin{aligned} h(\E[Y]) = X\beta . \end{aligned}\]
A Generalized Additive Model (GAM) replaces the linear predictor with \[\begin{aligned} \beta_0 + f_1(X_1) + \cdots + f_n(X_n), \end{aligned}\] where each \(f_i( )\) is some smooth function, e.g., a spline.