
get_design_matrix gets design matrix for gaussian, binomial, and poisson models
Source: R/pglmm-dense.R
get_design_matrix_pglmm.Rdget_design_matrix gets design matrix for gaussian, binomial, and poisson models
Arguments
- formula
A two-sided linear formula object describing the mixed effects of the model.
To specify that a random term should have phylogenetic covariance matrix along with non-phylogenetic one, add
__(two underscores) at the end of the group variable; e.g.,+ (1 | sp__)will construct two random terms, one with phylogenetic covariance matrix and another with non-phylogenetic (identity) matrix. In contrast,__in the nested terms (below) will only create a phylogenetic covariance matrix. Nested random terms have the general form(1|sp__@site__)which represents phylogenetically related species nested within correlated sites. This form can be used for bipartite questions. For example, species could be phylogenetically related pollinators and sites could be phylogenetically related plants, leading to the random effect(1|insects__@plants__). If more than one phylogeny is used, remember to add all to the argumentcov_ranef = list(insects = insect_phylo, plants = plant_phylo). Phylogenetic correlations can be dropped by removing the__underscores. Thus, the form(1|sp@site__)excludes the phylogenetic correlations among species, while the form(1|sp__@site)excludes the correlations among sites.Note that correlated random terms are not allowed. For example,
(x|g)will be the same as(0 + x|g)in thelme4::lmersyntax. However,(x1 + x2|g)won't work, so instead use(x1|g) + (x2|g), or(x1 + x2||g). As the random intercept and slope of(1|g) + (x|g)are independent, the model depends on wherexis zero: the variance among the levels ofgis that of the intercept plus \(x^2\) times that of the slope. With a 0/1 covariate (e.g. a control and a treatment), the levels then vary more in the treatment than in the control by construction. Center continuous covariates of random slopes (e.g.x - mean(x)) and code two-level factors as -1/+1 (equal variance in both groups) unless zero is a meaningful reference. Covariates of random slopes must be numeric columns ofdata.Grouping factors are columns of
data. Their interactions as inlme4, e.g.(1|sp:site), are fitted as the nested terms(1|sp@site), and(1|site/sp)as(1|site) + (1|sp@site).The formula can have offsets, e.g.
+ offset(log(effort))for counts with different sampling efforts or areas: terms of the linear predictor with a known coefficient of one. They are supported withmethod = "Laplace"(binomial, poisson and negative binomial models) andbayes = TRUE, and are included in the predicted values (predict(),fitted(),simulate(); withnewdata, the offset is calculated from it).- data
A
data.framecontaining the variables named in formula. Rows with missing values in the response, the fixed effects, the grouping variables or the covariates of random slopes are dropped before fitting; this is equivalent to removing those rows fromdatamanually before callingpglmm. As a result,pglmm(data_with_NAs)andpglmm(na.omit(data[, vars]))(withvarsthe variables of the model) produce identical estimates. Withrandom.effects, the rows with missing values in the response or the fixed effects are dropped, together with their values inrandom.effects.- random.effects
Optional pre-build list of random effects. If
NULL(the default), the functionprep_dat_pglmmwill prepare the random effects for you from the information informula,data, andcov_ranef.random.effectallows a list of pre-generated random effects terms to increase flexibility; for example, this makes it possible to construct models with both phylogenetic correlation and spatio-temporal autocorrelation. In preparingrandom.effect, make sure that the orders of rows and columns of covariance matrices in the list are the same as their corresponding group variables in the data. Also, this should be a list of lists, e.g.random.effects = list(re1 = list(matrix_a), re2 = list(1, sp = sp, covar = Vsp)). Each element is one random term:list(x, g, covar)(one value per level of the factorg, with covariance matrixcovar, times the covariatex, 1 for intercepts),list(x, g1, covar1, g2)(one value per combination ofg1andg2, with covariance matrixkronecker(I, covar1)), orlist(V)(one value per row of the data, with covariance matrixV). They are fitted with the sparse code (seesparse), with the inverses of these matrices, which must be positive definite; inlist(V), rows ofVthat are identical (e.g. replicated observations of the same species at the same site) are merged into one level, and ifVis still singular, the dense code is used (sparse = NULL, not withmethod = "Laplace"). Binomial and poisson models withrandom.effectsare fitted with the Laplace approximation by default (PQL before phyr 1.9.0); no observation-level term is added, asrandom.effectsgives all the random terms.- na.action
What to do with NAs?