## Adding colour to the famous 'iris' dataset:
iriscol <- iris
set.seed(123) # Simulate colours, then fit colour frequencies:
iriscol$col <- sample(c("yellow", "purple", "blue"),replace = TRUE,
size = nrow(iriscol), prob=c(0.5,0.3,0.2))
colfit <- fitme(cbind(npos,nneg) ~ 1+(1|Species), family=multi(responses="col"),
data=iriscol, init=list(lambda=NA)) # note warning if no 'init'...
head(fitted(colfit))
# To only generate the binomial datasets:
binomialize(iriscol,responses="col")
## An example considering pseudo-data at one diploid locus for 50 individuals
set.seed(123)
genecopy1 <- sample(4,size=50,prob=c(1/2,1/4,1/8,1/8),replace=TRUE)
genecopy2 <- sample(4,size=50,prob=c(1/2,1/4,1/8,1/8),replace=TRUE)
alleles <- c("122","124","126","128")
genotypes <- data.frame(type1=alleles[genecopy1],type2=alleles[genecopy2])
## Columns "type1","type2" each contains an allele type => input is "types" (the default)
datalist <- binomialize(genotypes,responses=c("type1","type2"))
## two equivalent fits:
f1 <- HLfit(cbind(npos,nneg)~1,data=datalist, family=binomial())
f2 <- HLfit(cbind(npos,nneg)~1,data=genotypes, family=multi(responses=c("type1","type2")))
fitted(f2)
if (spaMM.getOption("example_maxtime")>1.7) {
##### Control of lambda estimation over different binomial submodels
genoInSpace <- data.frame(type1=alleles[genecopy1],type2=alleles[genecopy2],
x=runif(50),y=runif(50))
method <- "PQL" # for faster exampple
## Fitting distinct variances for all binomial responses:
multifit <- corrHLfit(cbind(npos,nneg)~1+Matern(1|x+y),data=genoInSpace,
family=multi(responses=c("type1","type2")),
ranFix=list(rho=1,nu=0.5), method=method)
length(unique(unlist(lapply(multifit, get_ranPars, which="lambda")))) # 3
multifit <- fitme(cbind(npos,nneg)~1+Matern(1|x+y),data=genoInSpace,
family=multi(responses=c("type1","type2")),
init=list(lambda=NaN), # forcing 'inner' estimation for fitme
fixed=list(rho=1,nu=0.5), method=method)
length(unique(unlist(lapply(multifit, get_ranPars, which="lambda")))) # 3
## Fitting the same variance for all binomial responses:
multifit <- fitme(cbind(npos,nneg)~1+Matern(1|x+y),data=genoInSpace,
family=multi(responses=c("type1","type2")),
init=list(lambda=NA), # forcing 'outer' estimation for fitme
fixed=list(rho=1,nu=0.5), method=method)
length(unique(unlist(lapply(multifit, get_ranPars, which="lambda")))) # 1
multifit <-
corrHLfit(cbind(npos,nneg)~1+Matern(1|x+y),data=genoInSpace,
family=multi(responses=c("type1","type2")),
init.corrHLfit=list(lambda=1), # forcing 'outer' estimation for corrHLfit
ranFix=list(rho=1,nu=0.5), method=method)
length(unique(unlist(lapply(multifit, get_ranPars, which="lambda")))) # 1
}
Run the code above in your browser using DataLab