% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/uncertainty_RYM.R
\name{estimRYMRep}
\alias{estimRYMRep}
\title{RYM with "rep" effect}
\usage{
estimRYMRep(
  data,
  colY,
  colID,
  colRep,
  mix2genos,
  conf_level = 0.95,
  plotDiag = FALSE,
  title = ""
)
}
\arguments{
\item{data}{data frame}

\item{colY}{column name specifying the response variable}

\item{colID}{column name specifying the stand identifiers}

\item{colRep}{column name specifying the replicate}

\item{mix2genos}{named list with one component pe rmixture, each component being a vector of genotype names}

\item{conf_level}{confidence level for the uncertainty intervals}

\item{plotDiag}{if TRUE, diagnostics will be plotted}

\item{title}{title for the diagnostics plots}
}
\value{
data frame with the inference summaries of RYM per mixture
}
\description{
Computes the relative yield of mixtures (RYMs) per replicate (e.g., blocks), then fits a linear model correcting for this rep effect, and quantifies the uncertainty around it.
}
\examples{
## generate fake data
set.seed(1234)
nbGenos <- 25
genos <- sprintf("g\%02i", 1:nbGenos)
pairs <- t(combn(x=genos, m=2))
mixIDs <- sample(paste(pairs[,1], pairs[,2], sep="_"), size=75)
nbBlocks <- 3
blocks <- LETTERS[1:nbBlocks]
dat <- do.call(rbind, lapply(blocks, function(block){
  data.frame(ID=c(genos, mixIDs),
             block=block,
             stringsAsFactors=TRUE)
}))
listContr <- list(block="contr.sum")
X <- model.matrix(~ 1 + block, data=dat, contrasts=listContr)
Z_GMA <- mkZGMA(dat, "ID", "_")
Z_SMA <- mkZSMA(dat, "ID", "_", inc_SMA_ii="only_pur")
truth <- list("intercept"=100, "var_GMA"=10, "var_SMA"=4, "var_error"=1)
truth[["blockEffs"]] <- rnorm(n=nbBlocks - 1, mean=0, sd=2)
truth[["GMAs"]] <- rnorm(n=nbGenos, mean=0, sd=sqrt(truth$var_GMA))
truth[["SMAs"]] <- rnorm(n=nlevels(dat$ID), mean=0, sd=sqrt(truth$var_SMA))
truth[["errors"]] <- rnorm(n=nrow(dat), mean=0, sd=sqrt(truth$var_error))
y <- X \%*\% c(truth$intercept, truth$blockEffs) +
  Z_GMA \%*\% truth$GMAs +
  Z_SMA \%*\% truth$SMAs +
  truth$errors
dat$yield <- y[,1]
hist(dat$yield, breaks=20, las=1, main="Simulated data")
boxplot(yield ~ block, data=dat, las=1, main="Simulated data")

## compute the average yield per ID over blocks, and then compute the RYMs:
avg <- tapply(dat$yield, dat$ID, mean)
avg <- data.frame(ID=names(avg), yield=avg, stringsAsFactors=TRUE)
avg <- RYM(avg, colIDcomps="ID", colY="yield", sep="_")

## compute the RYMs per block, and then correct for the "block" effect:
mix2genos <- strsplit(levels(dat$ID), "_")
names(mix2genos) <- levels(dat$ID)
out <- estimRYMRep(dat, "yield", "ID", "block", mix2genos)
head(out)

## compare both approaches:
op <- par(mfrow=c(1,2))
hist(avg$RYM, las=1, main="Estimated RYM by averaging over blocks", xlab="RYM")
abline(v=1, lwd=2); abline(v=mean(avg$RYM, na.rm=TRUE), col="red", lwd=2)
hist(out$estim, las=1, main="Estimated RYM after correcting the 'block' effect", xlab="RYM")
abline(v=1, lwd=2); abline(v=mean(out$estim), col="red", lwd=2)
par(op)

out$avgRYM <- avg[rownames(out), "RYM"]
plot(out$avgRYM, out$estim, las=1, type="n",
     xlab="RYM averaged over blocks",
     ylab="RYM after correcting the 'block' effect",
     main="Estimated RYMs")
idx <- which(out$pv > 0.05)
points(out$avgRYM[idx], out$estim[idx], pch=19, col="black")
idx <- which(out$pv <= 0.05)
points(out$avgRYM[idx], out$estim[idx], pch=19, col="red")
abline(a=0, b=1, h=1, v=1, lty=2)
segments(x0=as.numeric(out$avgRYM), y0=out$cil,
         x1=as.numeric(out$avgRYM), y1=out$ciu,
         col="grey", lty=2, lwd=2)
}
\seealso{
\code{\link{estimRYRep}}
}
\author{
Timothee Flutre [aut], Arnaud Gauffreteau [ctb]
}
