Type: Package
Title: Perform a Partition of Variance of Reaction Norms
Version: 0.2.1
Date: 2025-01-23
BugReports: https://github.com/devillemereuil/reacnorm/issues
Description: Partitions the phenotypic variance of a plastic trait, studied through its reaction norm. The variance partition distinguishes between the variance arising from the average shape of the reaction norms (V_Plas) and the (additive) genetic variance . The latter is itself separated into an environment-blind component (V_G/V_A) and the component arising from plasticity (V_GxE/V_AxE). The package also provides a way to further partition V_Plas into aspects (slope/curvature) of the shape of the average reaction norm (pi-decomposition) and partition V_Add (gamma-decomposition) and V_AxE (iota-decomposition) into the impact of genetic variation in the reaction norm parameters. Reference: de Villemereuil & Chevin (2025) <doi:10.32942/X2NC8B>.
Depends: R (≥ 4.1.0)
Encoding: UTF-8
Imports: cubature (≥ 1.4), stringi, matrixStats
Suggests: R.rsp
VignetteBuilder: R.rsp
LazyData: true
License: GPL-3
NeedsCompilation: no
Packaged: 2025-01-23 10:51:23 UTC; pierre
Author: Pierre de Villemereuil [aut, cre]
Maintainer: Pierre de Villemereuil <pierre.de-villemereuil@mnhn.fr>
Repository: CRAN
Date/Publication: 2025-01-27 18:40:09 UTC

A simulated dataset on the reaction norms of dragons in a continuous environment

Description

A simulated dataset on the reaction norm of aggressiveness and thermal performance of dragons measured directly in a wild population, with heterogenous temperatures. Offspring from the same mothers are considered as half-sibs.

Usage

dragon_continuous

Format

A data frame with 1000 rows and 5 variables:

Individual

The individual on which the measures were performed at the different temperatures.

Family

The family to which each individual belongs to, i.e.\ which mother they are the offspring off. Offspring from the same mothers are considered as half-sibs.

Temp

The value of the recorded temperature at which the experiment was performed in the field. For convenience, the value was standardised so that the average is 0.

Aggressiveness

Aggressiveness of the dragon, measured using a very precise, continuous index, when presented with an armored knight to combat in the field.

Performance

Performance (sprint speed to rush toward a dummy princess) of each individual dragon evaluated directly on the field.


A simulated dataset on the reaction norms of dragons at a discretised environmental gradient

Description

A simulated dataset on the reaction norm of aggressiveness and thermal performance of dragons according to a series of experimental measures at a discretised gradient of temperature

Usage

dragon_discrete

Format

A data frame with 1000 rows and 5 variables:

Name_Env

A character string for the "name" of the experimental temperature at which the experiment was performed.

Temp

The value of the experimental temperature at which the experiment was performed. For convenience, the value was standardised so that the average is 0.

Individual

The individual on which the measures were performed at the different temperatures.

Aggressiveness

Aggressiveness of the dragon, measured using a very precise, continuous index, when presented with an armored knight to combat.

Performance

Performance (sprint speed to rush toward a dummy princess) of each individual dragon evaluated at each of the experimental temperatures.


Decompose the \mathbf{G}-matrix from a character-state model

Description

This function takes the \mathbf{G}-matrix estimated from a character-state model and returns the values for V_{\text{Add}}, V_{\text{A}}, V_{\text{A}\times\text{E}} and n_{\text{eff}}

Usage

rn_cs_gen(G_cs, wt = NULL)

Arguments

G_cs

(Additive) genetic variance-covariance matrix estimated from a character-state model (i.e. where environments are treated as a categorical variable). (numerical matrix)

wt

Weights to apply to the different environments, e.g. reflecting their frequencies in the wild. The weights must non-negative, and at least one must be non-zero. The vector wt must be the same length as the rows and columns of G_cs. By default, no weighting is applied. (numeric)

Details

V_{\text{Add}} is the (weighted) average of the diagonal elements of G_cs, V_{\text{A}} is the (weighted) average of all the elements of G_cs and V_{\text{A}\times\text{E}} is the difference between V_{\text{Add}} and V_{\text{A}}. Finally, the efficient number of dimensions n_{\text{eff}} is the ratio of the sum of the eigen values of G_cs over its maximum eigen value. Note that n_{\text{eff}} is returned for information, but is expected to be biased in practice due to an over-estimation of the maximum eigen value.

Value

This function yields V_{\text{Add}}, V_{\text{A}}, V_{\text{A}\times\text{E}} and n_{\text{eff}} as a one-row data.frame (data.frame, all numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_vgen, rn_gen_decomp

Examples

G <- diag(10)

rn_cs_gen(G_cs = G)

Compute the \gamma-decomposition of additive genetic variance per environment

Description

This function computes the \gamma-decomposition of the additive genetic variance for each environment.

Usage

rn_gamma_env(theta, G_theta, env = NULL, shape = NULL, X = NULL,
             fixed = NULL, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

G_theta

Genetic variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the G_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

This function provides the \gamma-decomposition (according to each parameter of the reaction norm and their covariances) of the additive genetic variance in each environment.

It is very important that the parameters are in the same order in the argument of the shape function, in theta and in G_theta.

If X is provided the second column is returned as the value for the "environment" (based on the assumption that a polynomial function was used).

Value

This function yields a data.frame containing the input values for the environment, the additive genetic variance in each of them, with its corresponding \gamma-decomposition (data.frame, all numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_vgen, rn_gen_decomp

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing V_gen
rn_gamma_env(env     = vec_env,
              shape   = expr,
              theta   = theta,
              G_theta = G,
              fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# If a polynomial was used, it is possible to use the linear modeling rather having
# to compute integrals
theta <- c(a = 1.5, b = 0.5, c = -0.5)
X     <- cbind(1, vec_env, (vec_env - mean(vec_env))^2)
G     <- 0.1 * diag(3)
rn_gamma_env(theta   = theta,
             G_theta = G,
             X       = X)

Compute the decomposition of the additive genetic variance and their \gamma- and \iota-decomposition

Description

This function calculates the total additive genetic variance of the reaction norm (V_{\text{Add}}, and its \gamma-decomposition), the marginal additive genetic variance of the trait (V_{\text{A}}) and the additive genetic variance of plasticity (V_{\text{A}\times\text{E}}, and its \iota-decomposition).

Usage

rn_gen_decomp(theta, G_theta, env = NULL, shape = NULL, X = NULL, fixed = NULL,
              wt_env = NULL, compute_gamma = TRUE, compute_iota  = TRUE,
              add_vars = NULL, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

G_theta

Genetic variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the G_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

compute_gamma

Should the \gamma-decomposition be performed? Default is TRUE. (boolean)

compute_iota

Should the \iota-decomposition be performed? Default is TRUE. (boolean)

add_vars

Optional, provide additive genetic variances V_{\text{Add}}, V_{\text{A}} and V_{\text{A}\times\text{E}} (in that exact order!) computed from another model, e.g. a character-state model. These values will be used to scale the \gamma's and \iota's and be returned by the function for the respective additive genetic variances. (numeric vector of length 3)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The variance V_{\text{Add}} is the total additive genetic variance in the reaction norm. It can be decomposed according to each of the parameters in G_theta (and their pairwise covariance), using the \gamma-decomposition, if the compute_gamma is activated. It can also be decomposed into the marginal additive genetic variance of the trait (V_{\text{A}}, the additive genetic variance of the trait that would be computed if plasticity were ignored), and the additive genetic variance of plasticity itself (V_{\text{A}\times\text{E}}, i.e. the genetic-by-environment interaction variance). This last component can also be decomposed according to each of the parameters in G_theta (and their pairwise covariance), using the \iota-decomposition (for interaction).

It is very important that the parameters are in the same order in the argument of the shape function, in theta and in G_theta.

Value

This function yields the values for V_{\text{Add}}, V_{\text{A}} and V_{\text{A}\times\text{E}}, as well as the \gamma- and \iota-decomposition if requested, formatted as a 1-row data.frame (data.frame, all numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_vgen, rn_gen_decomp

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing V_gen
out <- 
  rn_gen_decomp(theta   = theta,
                G_theta = G,
                env     = vec_env,
                shape   = expr,
                fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# Checking that V_Add = V_A + V_AxE
out$V_Add == out$V_A + out$V_AxE
# Checking that the sum of \eqn{\gamma}'s is 1
out$Gamma_cmax + out$Gamma_xopt + out$Gamma_cmax_xopt
# Checking that the sum of \eqn{\iota}'s is 1
out$Iota_cmax + out$Iota_xopt + out$Iota_cmax_xopt

# Applying some weighting to the environmental values according to a normal distribution
rn_gen_decomp(theta   = theta,
              G_theta = G,
              env     = vec_env,
              shape   = expr,
              fixed   = c(3, 4),
              wt_env  = dnorm(vec_env))
              
# If a polynomial was used, it is possible to use the linear modeling rather having
# to compute integrals
theta <- c(a = 1.5, b = 0.5, c = -0.5)
X     <- cbind(1, vec_env, (vec_env - mean(vec_env))^2)
G     <- 0.1 * diag(3)
rn_gen_decomp(theta   = theta,
              G_theta = G,
              X        = X)

Compute the phenotypic mean on the observed scale

Description

This function calculates the phenotypic mean on the observed scale from the latent mean and variance.

Usage

rn_mean_by_env(theta, V_theta, env, shape, fixed = NULL, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

V_theta

Full variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

fixed

If some parameters of shape, included in theta are not included in the V_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

This function yields the phenotypic average (across genotypes) for each value of the environment. If the reaction norm is not linear in its parameters (the parameters in theta), then this average will generaly differ from the value yielded by shape evaluated at values in theta.

It is very important that the parameters are in the same order in theta (which, again, must be named) and in V_theta.

Value

This function yields the phenotypic mean for each value of the environmental vector provided (numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_vplas, rn_vgen

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing mean by environment
rn_mean_by_env(env     = vec_env,
               shape   = expr,
               theta   = theta,
               V_theta = G,
               fixed = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# This is (slightly) different from the function evaluated
# using the average values of the parameters in theta 
theta["cmax"] * exp(
         - exp(theta["rho"] * (vec_env - theta["xopt"]) - 6) -
             theta["sigmagaus"] * (vec_env - theta["xopt"])^2
     )

Compute the \varphi-decomposition of V_{\text{Plas}}

Description

This function calculates, for a linear (in its parameters) model, the plastic variance of the average (over genotypes) reaction norm shape V_{\text{Plas}} and its \varphi-decomposition (depending on the parameters rather than slope/curvature). This is especially useful when the reaction norm model was a polynomial.

Usage

rn_phi_decomp(theta, X, S = NULL, wt_env = NULL, correction = FALSE, v_plas = NA)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

X

The design matrix X of the reaction norm model. For a polynomial, X contains all the exponents of the environment. (numerical matrix)

S

The error variance-covariance matrix of the estimated fixed effects theta. Not mandatory, but it is strongly advised to correct for the error variance in the parameter estimates. The function throws a warning if it is NULL (numerical matrix).

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

correction

Should Bessel's correction (dividing the sum-of-squares by N-1 rather than N) be used (TRUE) or not (FALSE, default). The default is FALSE, because it is likely that other components such as the total phenotypic variance is computed over the number of individuals (generally large number) rather than the number of environments (generally small number). The best is to manually use Bessel's correction over the proper number of data points. (boolean)

v_plas

If already computed, an estimate for V_{\text{Plas}}. If NA, then V_{\text{Plas}} will be computed by the function. (numeric)

Details

The variance V_{\text{Plas}} is the variance arising from the shape of the reaction norm after avering over the genetic variance, i.e. the purely environmental part of the variance of plasticity in the reaction norm. Its \phi-decomposition is based on the parameters of the linear model \mathbf{X}\theta, rather than on slope and curvature, as for the \pi-decomposition.

It is very important that the parameters are in the same order in theta (which, again, must be named) and in G_theta.

Value

This function yields the \pi-decomposition of V_{\text{Plas}} as a one-row data.frame. If V_{\text{Plas}} was provided as an argument (e.g. as computed from a separate character-state model), it also outputs M^2, the ratio between V_{\text{Plas}} as computed from the linear model parameters and the provided value for V_{\text{Plas}} (data.frame, all numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_pi_decomp, rn_vplas

Examples

# Parameters
vec_env <- rnorm(20)
theta <- c(a = 1.5, b = 0.5, c = -0.5)
X     <- cbind(1, vec_env, (vec_env - mean(vec_env))^2)
S     <- (10^-4) * diag(3)

# Computing the phi-decomposition
rn_phi_decomp(theta = theta, X = X, S = S)

Compute the \pi-decomposition of V_{\text{Plas}}

Description

This function calculates the plastic variance of the average (over genotypes) reaction norm shape V_{\text{Plas}} and its \pi-decomposition into \pi_{\text{Sl}} and \pi_{\text{Cv}}.

Usage

rn_pi_decomp(theta, V_theta, env, shape, fixed = NULL, wt_env = NULL,
             correction = FALSE, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

V_theta

Full variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

fixed

If some parameters of shape, included in theta are not included in the V_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

correction

Should Bessel's correction (dividing the sum-of-squares by N-1 rather than N) be used (TRUE) or not (FALSE, default). The default is FALSE, because it is likely that other components such as the total phenotypic variance is computed over the number of individuals (generally large number) rather than the number of environments (generally small number). The best is to manually use Bessel's correction over the proper number of data points. (boolean)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The variance V_{\text{Plas}} is the variance arising from the shape of the reaction norm after averaging over the genetic variance, i.e. the purely environmental part of the variance of plasticity in the reaction norm. Its \pi-decomposition into a slope (\pi_{\text{Sl}}) and curvature (\pi_{\text{Cv}}) components allows to distinguish the importance of each of those components in V_{\text{Plas}}. Note that the \pi-decomposition requires that the environmental variable is normally distributed, or that the reaction norm shape is quadratic.

It is very important that the parameters are in the same order in theta (which, again, must be named) and in V_theta.

Value

This function yields the \pi-decomposition of V_{\text{Plas}} as a one-row data.frame (data.frame, all numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_mean_by_env, rn_vplas

Examples

# Some environment vector
vec_env <- rnorm(20)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing the pi-decomposition of V_Plas
rn_pi_decomp(theta   = theta,
             V_theta = G,
             env     = vec_env,
             shape   = expr,
             fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# Applying some weighting to the environmental values according to a normal distribution
# if the env variable is actually fixed
vec_env2 <- seq(-2, 2, length.out = 20)
rn_pi_decomp(theta   = theta,
              V_theta = G,
              env     = vec_env2,
              shape   = expr,
              fixed   = c(3, 4),
              wt_env  = dnorm(vec_env2))

Compute the total genetic variance V_{\text{Gen}}

Description

This function calculates the total genetic variance V_{\text{Gen}} arising from genetic variation in the reaction norm.

Usage

rn_vgen(theta, G_theta, env = NULL, shape = NULL, X = NULL, fixed = NULL, wt_env = NULL,
        average = TRUE, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

G_theta

Genetic variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the G_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

average

Should the values for each environment be given, or just their average (default)? (boolean)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The variance V_{\text{Gen}} is the total genetic variance in the reaction norm. If the reaction norm is linear in its parameters, then is it also equal to the total additive genetic variance in the reaction norm (V_{\text{Add}}). Otherwise, see the function rn_gen_decomp to compute V_{\text{Add}}.

It is very important that the parameters are in the same order in the argument of the shape function, in theta and in G_theta.

Value

This function yields the genetic variance arising from all the genetic variation in the reaction norm (numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_gen_decomp, rn_vplas, rn_vtot

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing V_gen
rn_vgen(theta   = theta,
        G_theta = G,
        env     = vec_env,
        shape   = expr,
        fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# Applying some weighting according to a normal distribution
rn_vgen(theta   = theta,
        G_theta = G,
        env     = vec_env,
        shape   = expr,
        fixed   = c(3, 4),
        wt_env  = dnorm(vec_env))

# Setting average to FALSE allows to obtain the value for each environment
rn_vgen(theta   = theta,
        G_theta = G,
        env     = vec_env,
        shape   = expr,
        fixed   = c(3, 4),
        average = FALSE)

# If a polynomial was used, it is possible to use the linear modeling rather having
# to compute integrals
theta <- c(a = 1.5, b = 0.5, c = -0.5)
X     <- cbind(1, vec_env, (vec_env - mean(vec_env))^2)
G     <- 0.1 * diag(3)
rn_vgen(theta   = theta,
        G_theta = G,
        X       = X)
# Should be very close to the computation with integration
rn_vgen(theta   = theta,
        G_theta = G,
        env     = vec_env,
        shape   = expression(a + b * x + c * x^2))

Compute the total phenotypic variance V_{\text{Tot}}

Description

This function calculates the total phenotypic variance V_{\text{Tot}} arising along and beyond the reaction norm, i.e. including the residual variance.

Usage

rn_vp_env(theta, V_theta, var_res, env = NULL, shape = NULL, X = NULL,
          fixed = NULL, wt_env = NULL, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

V_theta

Full variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

var_res

Residual variance beyond the reaction norm. It could be a scalar if this residual variance is assumed homogeneous or a vector the same length as env (or number of rows of X), if the residual variance is allowed to vary across the environment. (numeric scalar or vector)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the G_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The phenotypic variance at a given environment depends on the variation in the \theta parameters and the residual variance. It thus account for everything but the variance due to the average shape of the reaction norm (i.e. V_{\text{Plas}}).

Value

This function yields the phenotypic variance in each environment (numerical vector).

Author(s)

Pierre de Villemereuil

See Also

rn_vplas, rn_vgen

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)
# Another covariance matrix for cmax and xopt (e.g. permanent environment, or maternal effet)
M     <- matrix(c(0.05,      0,
                  0,      0.01),
                ncol = 2)
# Full variance-covariance matrix
P     <- G + M
# Residual variance
vr    <- 0.1
                
# Computing V_tot
rn_vp_env(theta   = theta,
          V_theta = P,
          var_res = vr,
          env     = vec_env,
          shape   = expr,
          fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in P

Compute the purely environmental plastic variance V_{\text{Plas}}

Description

This function calculates the purely environmental plastic variance arising from the average (across genotypes) shape of the reaction norm.

Usage

rn_vplas(theta, V_theta, env = NULL, shape = NULL, X = NULL, S = NULL,
         fixed = NULL, wt_env = NULL, correction = FALSE, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

V_theta

Full variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

S

The error variance-covariance matrix of the estimated fixed effects theta. Used only if the X argument is used. (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the V_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

correction

Should Bessel's correction (dividing the sum-of-squares by N-1 rather than N) be used (TRUE) or not (FALSE, default). The default is FALSE, because it is likely that other components such as the total phenotypic variance is computed over the number of individuals (generally large number) rather than the number of environments (generally small number). The best is to manually use Bessel's correction over the proper number of data points. (boolean)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The variance V_{\text{Plas}} is the variance arising from the shape of the reaction norm after avering over the genetic variance, i.e. the purely environmental part of the variance of plasticity in the reaction norm.

It is very important that the parameters are in the same order in theta (which, again, must be named) and in V_theta.

Value

This function yields V_{\text{Plas}} (numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_mean_by_env, rn_pi_decomp, rn_vgen, rn_vtot

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)

# Computing V_plas
rn_vplas(theta   = theta,
         V_theta = G,
         env     = vec_env,
         shape   = expr,
         fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in G

# Note the quite large difference when Bessel's correction is used
rn_vplas(theta   = theta,
         V_theta = G,
         env     = vec_env,
         shape   = expr,
         fixed   = c(3, 4),
         correction = TRUE)
         
# It is possible to weight the environment, e.g. according to a normal distribution
rn_vplas(theta   = theta,
         V_theta = G,
         env     = vec_env,
         shape   = expr,
         fixed   = c(3, 4),
         wt_env  = dnorm(vec_env),
         correction = TRUE)

# If a polynomial was used, it is possible to use the linear modeling rather having
# to compute integrals
theta <- c(a = 1.5, b = 0.5, c = -0.5)
X     <- cbind(1, vec_env, (vec_env - mean(vec_env))^2)
S     <- (10^-4) * diag(3)
rn_vplas(theta = theta, X = X, S = S)

Compute the total phenotypic variance V_{\text{Tot}}

Description

This function calculates the total phenotypic variance V_{\text{Tot}} arising along and beyond the reaction norm, i.e. including the residual variance.

Usage

rn_vtot(theta, V_theta, var_res, env = NULL, shape = NULL, X = NULL, S = NULL,
        fixed = NULL, wt_env = NULL, correction = FALSE, width = 10)

Arguments

theta

Average parameters of the shape function. It must be a named vector, with the names corresponding to the parameters in the shape expression. (numeric)

V_theta

Full variance-covariance matrix of the parameters. It can be of lesser dimensions than theta, see fixed parameter. (numerical matrix)

var_res

Residual variance beyond the reaction norm. It could be a scalar if this residual variance is assumed homogeneous or a vector the same length as env (or number of rows of X), if the residual variance is allowed to vary across the environment. (numeric scalar or vector)

env

Vector of environmental values (numeric).

shape

Expression providing the shape of the reaction where x is the environment. For example: expression(a + b * x + c * x^2).

X

If the model used was linear in the parameters, the design matrix X of the model (numeric, incompatible with the arguments env and shape).

S

The error variance-covariance matrix of the estimated fixed effects theta. Used only if the X argument is used. (numeric, incompatible with the arguments env and shape).

fixed

If some parameters of shape, included in theta are not included in the G_theta matrix, then those dimensions are considered as genetically "fixed". Hence, fixed should contain a vector of the index of those parameters. Otherwise (if all parameters vary genetically), and by default, fixed should be set as NA. (integer)

wt_env

Weights to apply to the env vector values, providing an information regarding their relative probability in the biological context. The weights must non-negative, and at least one must non-zero. The vector wt_env must be the same length as env. By default, no weighting is applied. (numeric)

correction

Should Bessel's correction (dividing the sum-of-squares by N-1 rather than N) be used (TRUE) or not (FALSE, default). The default is FALSE, because it is likely that other components such as the total phenotypic variance is computed over the number of individuals (generally large number) rather than the number of environments (generally small number). The best is to manually use Bessel's correction over the proper number of data points. (boolean)

width

Parameter for the integral computation. The integral is evaluated from mu - width * sqrt(var) to mu + width * sqrt(var). The default value is 10, which should be sensible for most models. (numeric)

Details

The variance V_{\text{Tot}} is the total phenotypic variance of the trait along and beyond the reaction norm. In the simplest case, it is equal to the sum V_{\text{Plas}} + V_{\text{Gen}} + V_{\text{Res}}, in which case this function is rather useless. But in cases where the parameters \theta in the reaction norm curve is fitted with more than the (additive) genetic effect, then computing V_{\text{Tot}} becomes non-trivial. This function allows to do it, e.g. by providing (i) a way to specify the full variation assumed in \theta, using the V_theta full variance-covariance argument, and (ii) a way to specify a varying residual variance by environment.

Value

This function yields the total phenotypic variance along and beyond the reaction norm (numeric).

Author(s)

Pierre de Villemereuil

See Also

rn_vplas, rn_vgen

Examples

# Some environment vector
vec_env <- seq(-2, 2)

# Shape function
expr <- expression(
     cmax * exp(
         - exp(rho * (x - xopt) - 6) -
             sigmagaus * (x - xopt)^2
     ))

# Theta
theta <- c(cmax = 1, xopt = 0.9, rho = 8, sigmagaus = 0.4)
# G, only for cmax and xopt
G     <- matrix(c(0.1,      0.01,
                  0.01,     0.05),
                ncol = 2)
# Another covariance matrix for cmax and xopt (e.g. permanent environment, or maternal effet)
M     <- matrix(c(0.05,      0,
                  0,      0.01),
                ncol = 2)
# Full variance-covariance matrix
P     <- G + M
# Residual variance
vr    <- 0.1
                
# Computing V_tot
rn_vtot(theta   = theta,
        V_theta = P,
        var_res = vr,
        env     = vec_env,
        shape   = expr,
        fixed   = c(3, 4))
# Note that fixed is set for the third and forth parameters than are not in P

# With varying residual variance along the environmental (here with environmental variance)
vr    <- 0.05 * (vec_env^2)
rn_vtot(theta   = theta,
        V_theta = P,
        var_res = vr,
        env     = vec_env,
        shape   = expr,
        fixed   = c(3, 4))
# Same result because the average of Vr is the same as the unique value above