Skip to contents

Convert a normal (Gaussian) trait to a count (Poisson) trait

Usage

asPoisson(x, meanLogShift = NULL)

Arguments

x

matrix, values for one or more traits (if not a matrix, we cast to a matrix).

meanLogShift

NULL, numeric or list, additional additive shift(s) on the latent log scale; when NULL a shift of 0 is assumed, when numeric shifts for all traits in x must be provided, and when list shifts for all traits in x must be provided with the possibility to pass a NULL list node to skip the conversion for a trait (see examples).

Value

matrix of values with some traits recoded as counts

Details

If the input trait is normal (Gaussian) then this function generates a count trait by sampling from the Poisson generalized linear model. As such, this function's output is stochastic.

Specifically, it generates y | x ~ Poisson(lambda) with lambda = exp(meanLogShift + x). If the supplied latent values x have mean mu and variance sigma2, then the marginal expected value of the counts is exp(meanLogShift + mu + sigma2/2), which is the same mean formula as in the log-normal case. Therefore, to target an expected count mean M, set meanLogShift = log(M) - mu - sigma2/2. The marginal variance differs from the log-normal case and is E(y) + E(y)^2 * (exp(sigma2) - 1), because Poisson sampling adds the extra E(y) term on top of the latent-scale heterogeneity. Consequently, latent variance can be used to correct the expected mean and to induce overdispersion, but it does not fully determine the observed variance. If x already contains an added Gaussian residual term, that latent variance contributes to the overdispersion as well.

The name meanLogShift is used to emphasize that this argument is an additional shift applied during transformation, not the primary way to set the latent trait mean. In a normal AlphaSimR workflow, the latent mean and variance are usually already set via SP$addTrait*(..., mean = ..., var = ...) in the founding population and SP$setVarE. Hence, meanLogShift should be left at its default unless an extra transformation-specific shift is needed. One example is to control the mean of the observed values as shown below. This value should be established at the start of simulation and kept constant for most use cases.

Examples

#Simulate a founder pop, set latent trait parameters, and create a population
founderPop = quickHaplo(nInd=10, nChr=1, segSites=10)
SP = SimParam$new(founderPop)
trtMeanLog = c(0, 0)
trtVarGLog = c(1, 2)
SP$addTraitA(nQtlPerChr = 10, mean = trtMeanLog, var = trtVarGLog,
             corA = matrix(data = c(1.0, 0.6,
                                    0.6, 1.0), ncol = 2))
trtVarELog = c(1, 1)
trtVarPLog = trtVarGLog + trtVarELog
SP$setVarE(varE = trtVarELog)
pop = newPop(founderPop)
#> Error in getSimParam(simParam): simParam was not supplied and there is no SimParam called SP in the global environment. Supply simParam or create SP with SimParam$new()
popLarge = randCross(pop, nCrosses = 1000)
#> Error in getSimParam(simParam): simParam was not supplied and there is no SimParam called SP in the global environment. Supply simParam or create SP with SimParam$new()
pheno(pop)
#> Error: object 'pop' not found

meanVarFun = function(x) list(mean = mean(x), var = var(x))

#Convert a single input trait
(countGv = asPoisson(gv(pop)[, 1]))
#> Error: object 'pop' not found
meanVarFun(countGv)
#> Error: object 'countGv' not found

#Demonstrate meanLogShift argument
#For Y|x ~ Poisson(exp(meanLogShift + x)), the expected count is
#E(Y)=exp(meanLogShift + mean(x) + var(x)/2). Trait 1 is centered on the
#latent scale, so we use -variance/2 to target expected mean 1.
(countGvMean1 = asPoisson(gv(pop)[, 1], meanLogShift = -trtVarGLog[1]/2))
#> Error: object 'pop' not found
meanVarFun(countGvMean1)
#> Error: object 'countGvMean1' not found

#If x already contains Gaussian residual variance, use the corresponding
#latent variance in the same correction.
(countPhenoMean1 = asPoisson(pheno(pop)[, 1], meanLogShift = -trtVarPLog[1]/2))
#> Error: object 'pop' not found
meanVarFun(countPhenoMean1)
#> Error: object 'countPhenoMean1' not found

#Large population example to inspect means and variances
gvLarge = gv(popLarge)[, 1]
#> Error: object 'popLarge' not found
phenoLarge = pheno(popLarge)[, 1]
#> Error: object 'popLarge' not found
tmp = cbind(
  gv = gvLarge,
  countGv = c(asPoisson(gvLarge)),
  countGvMean1 = c(asPoisson(gvLarge, meanLogShift = -trtVarGLog[1]/2)),
  pheno = phenoLarge,
  countPhenoMean1 = c(asPoisson(phenoLarge, meanLogShift = -trtVarPLog[1]/2))
)
#> Error: object 'gvLarge' not found
(tmp2 = apply(X = tmp, MARGIN = 2, FUN = meanVarFun))
#> Error: object 'tmp' not found
hist(tmp[, "gv"], main = paste0("Mean: ", v = tmp2$gv$mean,
  ", Var: ", v = tmp2$gv$var))
#> Error: object 'tmp' not found
abline(v = tmp2$gv$mean, col = "red")
#> Error: object 'tmp2' not found
hist(tmp[, "countGv"], main = paste0("Mean: ", v = tmp2$countGv$mean,
  ", Var: ", v = tmp2$countGv$var))
#> Error: object 'tmp' not found
abline(v = tmp2$countGv$mean, col = "red")
#> Error: object 'tmp2' not found
hist(tmp[, "countGvMean1"], main = paste0("Mean: ", v = tmp2$countGvMean1$mean,
  ", Var: ", v = tmp2$countGvMean1$var))
#> Error: object 'tmp' not found
abline(v = tmp2$countGvMean1$mean, col = "red")
#> Error: object 'tmp2' not found
hist(tmp[, "pheno"], main = paste0("Mean: ", v = tmp2$pheno$mean,
", Var: ", v = tmp2$pheno$var))
#> Error: object 'tmp' not found
abline(v = tmp2$pheno$mean, col = "red")
#> Error: object 'tmp2' not found
hist(tmp[, "countPhenoMean1"], main = paste0("Mean: ", v = tmp2$countPhenoMean1$mean,
  ", Var: ", v = tmp2$countPhenoMean1$var))
#> Error: object 'tmp' not found
abline(v = tmp2$countPhenoMean1$mean, col = "red")
#> Error: object 'tmp2' not found

#Convert multiple input traits
try(asPoisson(pheno(pop), meanLogShift = 0))
#> Error in eval(expr, envir) : object 'pop' not found
asPoisson(gv(pop), meanLogShift = c(-trtVarGLog[1]/2, -trtVarGLog[2]/2))
#> Error: object 'pop' not found
asPoisson(pheno(pop), meanLogShift = list(-trtVarPLog[1]/2, NULL))
#> Error: object 'pop' not found

#Store the recoded trait manually
pheno(pop)
#> Error: object 'pop' not found
pop@pheno[, 1] = asPoisson(pheno(pop)[, 1])
#> Error: object 'pop' not found
pheno(pop)
#> Error: object 'pop' not found

#Apply and store the transformation automatically via SimParam$finalizePop()
finalizePopDefault = SP$finalizePop
SP$finalizePop = function(pop, simParam = SP, ...) {
  pop@pheno[, 1] = asPoisson(pheno(pop)[, 1])
  return(pop)
}
pop = newPop(founderPop)
#> Error in getSimParam(simParam): simParam was not supplied and there is no SimParam called SP in the global environment. Supply simParam or create SP with SimParam$new()
pheno(pop)
#> Error: object 'pop' not found

#Apply and store the transformation automatically via SimParam$finalizePheno()
SP$finalizePop = finalizePopDefault
SP$finalizePheno = function(pheno, pop, simParam = SP, ...) {
  pheno[, 1] = asPoisson(pheno[, 1])
  return(pheno)
}
pop = newPop(founderPop)
#> Error in getSimParam(simParam): simParam was not supplied and there is no SimParam called SP in the global environment. Supply simParam or create SP with SimParam$new()
pheno(pop)
#> Error: object 'pop' not found