gaynorr / AlphaSimR

R package for breeding program simulations
https://gaynorr.github.io/AlphaSimR/
Other
42 stars 18 forks source link

bv() fails on a population with one member when we have more than 1 trait #83

Closed gregorgorjanc closed 1 year ago

gregorgorjanc commented 1 year ago

Describe the bug bv() fails on a population with one member when we have more than 1 trait

Steps To Reproduce

#Create founder haplotypes
founderPop = quickHaplo(nInd=10, nChr=1, segSites=10)

#Set simulation parameters
SP = SimParam$new(founderPop)
SP$addTraitAD(10, meanDD=0.5)
SP$setVarE(h2=0.5)

#Create population
pop = newPop(founderPop, simParam=SP)
bv(pop, simParam=SP)
bv(pop[1])

#Create founder haplotypes
founderPop = quickHaplo(nInd=10, nChr=1, segSites=10)

#Set simulation parameters
SP = SimParam$new(founderPop)
SP$addTraitAD(10, mean = c(10, 5), var = c(1, 1), meanDD=c(0.5, 0.5))
SP$setVarE(h2=c(0.5, 0.5))

#Create population
pop = newPop(founderPop, simParam=SP)
bv(pop, simParam=SP)
bv(pop[1])

There must be some dimension/collapsing issues somewhere!

gregorgorjanc commented 1 year ago

Just to add R output along the code

> #Create founder haplotypes
> founderPop = quickHaplo(nInd=10, nChr=1, segSites=10)
> 
> #Set simulation parameters
> SP = SimParam$new(founderPop)
> SP$addTraitAD(10, meanDD=0.5)
> SP$setVarE(h2=0.5)
> 
> #Create population
> pop = newPop(founderPop, simParam=SP)
> bv(pop, simParam=SP)
          Trait1
 [1,] -1.1826482
 [2,] -0.3769351
 [3,] -1.4937266
 [4,]  2.1835105
 [5,]  0.3620446
 [6,]  0.6986256
 [7,] -0.4377998
 [8,]  0.4153918
 [9,]  0.3967900
[10,] -0.5652528
> bv(pop[1])
     Trait1
[1,]      0
> 
> #Create founder haplotypes
> founderPop = quickHaplo(nInd=10, nChr=1, segSites=10)
> 
> #Set simulation parameters
> SP = SimParam$new(founderPop)
> SP$addTraitAD(10, mean = c(10, 5), var = c(1, 1), meanDD=c(0.5, 0.5))
> SP$setVarE(h2=c(0.5, 0.5))
> 
> #Create population
> pop = newPop(founderPop, simParam=SP)
> bv(pop, simParam=SP)
          Trait1     Trait2
 [1,] -1.2161329 -0.1182307
 [2,] -0.2841492 -0.9980758
 [3,] -1.7141147 -0.2016634
 [4,] -0.8174502 -0.3535043
 [5,]  1.2568568  1.2453659
 [6,] -0.1030230  0.1303024
 [7,]  0.7027462 -1.4265577
 [8,]  1.5994379  1.1069079
 [9,]  0.4013167 -1.0718758
[10,]  0.1745123  1.6873316
> bv(pop[1])
Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent
gaynorr commented 1 year ago

It appears to be tied to the behavior of popVar, which is just a wrapper for arma::cov. I'll add a special rule to this function for cases where the number of individuals is 1.