diff --git a/R/aj_westcott.R b/R/aj_westcott.R index e746f6d..324a43b 100644 --- a/R/aj_westcott.R +++ b/R/aj_westcott.R @@ -1,8 +1,8 @@ -#' Adjust values with a grid of checks +#' Adjust values following the method of Westcott #' #' This function adjust the observed values of an experiment planted following #' the method described by Westcott (1981) with a grid of checks. -#' @param trait The trait to analyze. +#' @param trait The trait to adjust. #' @param geno The genotypes. #' @param ch1 Name of check 1. #' @param ch2 Name of check 2. @@ -30,6 +30,9 @@ #' proportion that the checks vary around the field. For values lower than 1 #' the values are adjusted based on that proportion over the checks variation. #' If \code{p = 0} then there is no adjustment. +#' +#' If the layout does not correspond with the Westcott method, then the observed values +#' are adjusted with the values of the checks planted nearby and a warning is issued. #' @return It returns the adjusted values. #' @references #' Westcott, B. (1981). Two methods for early generation yield assessment in winter wheat. @@ -40,7 +43,7 @@ aj.w <- function(trait, geno, ch1, ch2, row, col, nr = 5, ncb = 10, method = 2, p = 0.5, ind = TRUE, data) { - # Error messages + # Error and warning messages out <- ck.pos(row, col, data = data) @@ -50,19 +53,20 @@ aj.w <- function(trait, geno, ch1, ch2, row, col, nr = 5, ncb = 10, method = 2, out <- ck.w(trait, geno, ch1, ch2, row, col, ncb, data) if (out$c1 == 1) - stop("There are plots in the columns of checks with other genotypes planted.") + warning("There are plots in the columns of checks with other genotypes planted. + Adjusted values are obtained with the values of the checks nearby.") if (out$c2 == 1) - stop("The last column in the field does not have checks.") + warning("There are plots in the columns of genotypes with checks planted. + Adjusted values are obtained with the values of the checks nearby.") if (out$c3 == 1) - stop("There are plots in the columns of genotypes with checks planted.") + warning("There are columns of checks without alternating checks. + Adjusted values are obtained with the values of the checks nearby.") if (out$c4 == 1) - stop("There are columns of checks without alternating checks.") - - if (out$c5 == 1) - stop("There are plots with genotypes without a check plot to the left or to the right.") + warning("There are plots with genotypes without a check plot to the left or to the right. + Adjusted values are obtained with the values of the checks nearby.") # Save column names @@ -95,157 +99,182 @@ aj.w <- function(trait, geno, ch1, ch2, row, col, nr = 5, ncb = 10, method = 2, data[data[, geno] %in% c(ch1, ch2) & is.na(data[, trait.aj]), trait.aj] <- 0 - # Create columns for check centered values + # Run Westcott adjustment or modify adjustment - data[, ch1] <- NA - data[, ch2] <- NA - - # Create columns for prior and posterior check centered values - - ch1.pri.1 <- paste(ch1, 'pri.1', sep = '.') - data[, ch1.pri.1] <- NA - ch1.pri.2 <- paste(ch1, 'pri.2', sep = '.') - data[, ch1.pri.2] <- NA - ch1.pos.1 <- paste(ch1, 'pos.1', sep = '.') - data[, ch1.pos.1] <- NA - ch1.pos.2 <- paste(ch1, 'pos.2', sep = '.') - data[, ch1.pos.2] <- NA - ch2.pri.1 <- paste(ch2, 'pri.1', sep = '.') - data[, ch2.pri.1] <- NA - ch2.pri.2 <- paste(ch2, 'pri.2', sep = '.') - data[, ch2.pri.2] <- NA - ch2.pos.1 <- paste(ch2, 'pos.1', sep = '.') - data[, ch2.pos.1] <- NA - ch2.pos.2 <- paste(ch2, 'pos.2', sep = '.') - data[, ch2.pos.2] <- NA - - # Create columns for weigths - - ch1.w <- paste(ch1, 'w', sep = '.') - data[, ch1.w] <- NA - ch2.w <- paste(ch2, 'w', sep = '.') - data[, ch2.w] <- NA - - # Arrange check values and weights - - for(i in 1:dim(data)[1]) { - - geno.row <- data[i, row] - geno.col <- data[i, col] - columns <- (geno.col - ncb):(geno.col + ncb) + if (out$c1 == 0 & out$c2 == 0 & out$c3 == 0 & out$c4 == 0){ - cond1 <- data[, col] %in% columns - cond2 <- data[, geno] %in% c(ch1, ch2) - - temp <- data[data[, row] == geno.row & cond1 & cond2, c(geno, trait.aj, col)] - - if (dim(temp)[1] == 2) { + # Create columns for check centered values + + data[, ch1] <- NA + data[, ch2] <- NA + + # Create columns for prior and posterior check centered values + + ch1.pri.1 <- paste(ch1, 'pri.1', sep = '.') + data[, ch1.pri.1] <- NA + ch1.pri.2 <- paste(ch1, 'pri.2', sep = '.') + data[, ch1.pri.2] <- NA + ch1.pos.1 <- paste(ch1, 'pos.1', sep = '.') + data[, ch1.pos.1] <- NA + ch1.pos.2 <- paste(ch1, 'pos.2', sep = '.') + data[, ch1.pos.2] <- NA + ch2.pri.1 <- paste(ch2, 'pri.1', sep = '.') + data[, ch2.pri.1] <- NA + ch2.pri.2 <- paste(ch2, 'pri.2', sep = '.') + data[, ch2.pri.2] <- NA + ch2.pos.1 <- paste(ch2, 'pos.1', sep = '.') + data[, ch2.pos.1] <- NA + ch2.pos.2 <- paste(ch2, 'pos.2', sep = '.') + data[, ch2.pos.2] <- NA + + # Create columns for weigths + + ch1.w <- paste(ch1, 'w', sep = '.') + data[, ch1.w] <- NA + ch2.w <- paste(ch2, 'w', sep = '.') + data[, ch2.w] <- NA + + # Arrange check values and weights + + for(i in 1:dim(data)[1]) { - # Checks on the row + geno.row <- data[i, row] + geno.col <- data[i, col] + columns <- (geno.col - ncb):(geno.col + ncb) - data[i, ch1] <- temp[temp[, geno] == ch1, trait.aj] - data[i, ch2] <- temp[temp[, geno] == ch2, trait.aj] + cond1 <- data[, col] %in% columns + cond2 <- data[, geno] %in% c(ch1, ch2) - # Checks on row -2 + temp <- data[data[, row] == geno.row & cond1 & cond2, c(geno, trait.aj, col)] - temp.pri <- data[data[, row] == geno.row - 2 & cond1 & cond2, c(geno, trait.aj, col)] + if (dim(temp)[1] == 2) { + + # Checks on the row + + data[i, ch1] <- temp[temp[, geno] == ch1, trait.aj] + data[i, ch2] <- temp[temp[, geno] == ch2, trait.aj] + + # Checks on row -2 + + temp.pri <- data[data[, row] == geno.row - 2 & cond1 & cond2, c(geno, trait.aj, col)] + + if (dim(temp.pri)[1] == 2) { + data[i, ch1.pri.2] <- temp.pri[temp.pri[, geno] == ch1, trait.aj] + data[i, ch2.pri.2] <- temp.pri[temp.pri[, geno] == ch2, trait.aj] + } + + # Checks on row -1 + + temp.pri <- data[data[, row] == geno.row - 1 & cond1 & cond2, c(geno, trait.aj, col)] + + if (dim(temp.pri)[1] == 2) { + data[i, ch1.pri.1] <- temp.pri[temp.pri[, geno] == ch2, trait.aj] + data[i, ch2.pri.1] <- temp.pri[temp.pri[, geno] == ch1, trait.aj] + } + + # Checks on row +1 + + temp.pos <- data[data[, row] == geno.row + 1 & cond1 & cond2, c(geno, trait.aj, col)] + + if (dim(temp.pos)[1] == 2) { + data[i, ch1.pos.1] <- temp.pos[temp.pos[, geno] == ch2, trait.aj] + data[i, ch2.pos.1] <- temp.pos[temp.pos[, geno] == ch1, trait.aj] + } + + # Checks on row +2 + + temp.pos <- data[data[, row] == geno.row + 2 & cond1 & cond2, c(geno, trait.aj, col)] + + if (dim(temp.pos)[1] == 2) { + data[i, ch1.pos.2] <- temp.pos[temp.pos[, geno] == ch1, trait.aj] + data[i, ch2.pos.2] <- temp.pos[temp.pos[, geno] == ch2, trait.aj] + } + + # Weights for closest checks - if (dim(temp.pri)[1] == 2) { - data[i, ch1.pri.2] <- temp.pri[temp.pri[, geno] == ch1, trait.aj] - data[i, ch2.pri.2] <- temp.pri[temp.pri[, geno] == ch2, trait.aj] + data[i, ch1.w] <- ncb + 1 - abs(temp[temp[, geno] == ch1, col] - geno.col) + data[i, ch2.w] <- ncb + 1 - abs(temp[temp[, geno] == ch2, col] - geno.col) } + } + + # Adjust values with method 1 + + if (method == 1) { + chs <- c(ch1, ch2, ch1.pri.1, ch2.pri.1, ch1.pos.1, ch2.pos.1) + if (nr == 5) + chs <- c(chs, ch1.pri.2, ch2.pri.2, ch1.pos.2, ch2.pos.2) + af <- apply(data[, chs], 1, mean, na.rm = TRUE) + } + + # Adjust values with method 2 + + if (method == 2) { - # Checks on row -1 - - temp.pri <- data[data[, row] == geno.row - 1 & cond1 & cond2, c(geno, trait.aj, col)] - - if (dim(temp.pri)[1] == 2) { - data[i, ch1.pri.1] <- temp.pri[temp.pri[, geno] == ch2, trait.aj] - data[i, ch2.pri.1] <- temp.pri[temp.pri[, geno] == ch1, trait.aj] + if (nr == 3) { + ch1.m.pri <- apply(data[, c(ch1, ch1.pri.1)], 1, mean, na.rm = TRUE) + ch2.m.pri <- apply(data[, c(ch2, ch2.pri.1)], 1, mean, na.rm = TRUE) + ch1.m.pos <- apply(data[, c(ch1, ch1.pos.1)], 1, mean, na.rm = TRUE) + ch2.m.pos <- apply(data[, c(ch2, ch2.pos.1)], 1, mean, na.rm = TRUE) } - # Checks on row +1 - - temp.pos <- data[data[, row] == geno.row + 1 & cond1 & cond2, c(geno, trait.aj, col)] + if (nr == 5) { + foo <- function(x) x * c(1.5, 2, 1) + ch1.w.pri <- t(apply(!is.na(data[, c(ch1, ch1.pri.1, ch1.pri.2)]), 1, foo)) + ch2.w.pri <- t(apply(!is.na(data[, c(ch2, ch2.pri.1, ch2.pri.2)]), 1, foo)) + ch1.w.pos <- t(apply(!is.na(data[, c(ch1, ch1.pos.1, ch1.pos.2)]), 1, foo)) + ch2.w.pos <- t(apply(!is.na(data[, c(ch2, ch2.pos.1, ch2.pos.2)]), 1, foo)) + + ch1.m.pri <- data[, c(ch1, ch1.pri.1, ch1.pri.2)] * ch1.w.pri + ch2.m.pri <- data[, c(ch2, ch2.pri.1, ch2.pri.2)] * ch2.w.pri + ch1.m.pos <- data[, c(ch1, ch1.pos.1, ch1.pos.2)] * ch1.w.pos + ch2.m.pos <- data[, c(ch2, ch2.pos.1, ch2.pos.2)] * ch2.w.pos + + ch1.m.pri <- apply(ch1.m.pri, 1, sum, na.rm = TRUE) + ch2.m.pri <- apply(ch2.m.pri, 1, sum, na.rm = TRUE) + ch1.m.pos <- apply(ch1.m.pos, 1, sum, na.rm = TRUE) + ch2.m.pos <- apply(ch2.m.pos, 1, sum, na.rm = TRUE) - if (dim(temp.pos)[1] == 2) { - data[i, ch1.pos.1] <- temp.pos[temp.pos[, geno] == ch2, trait.aj] - data[i, ch2.pos.1] <- temp.pos[temp.pos[, geno] == ch1, trait.aj] + ch1.w.pri <- apply(ch1.w.pri, 1, sum, na.rm = TRUE) + ch2.w.pri <- apply(ch2.w.pri, 1, sum, na.rm = TRUE) + ch1.w.pos <- apply(ch1.w.pos, 1, sum, na.rm = TRUE) + ch2.w.pos <- apply(ch2.w.pos, 1, sum, na.rm = TRUE) + + ch1.m.pri <- ch1.m.pri / ch1.w.pri + ch2.m.pri <- ch2.m.pri / ch2.w.pri + ch1.m.pos <- ch1.m.pos / ch1.w.pos + ch2.m.pos <- ch2.m.pos / ch2.w.pos } - # Checks on row +2 - - temp.pos <- data[data[, row] == geno.row + 2 & cond1 & cond2, c(geno, trait.aj, col)] - - if (dim(temp.pos)[1] == 2) { - data[i, ch1.pos.2] <- temp.pos[temp.pos[, geno] == ch1, trait.aj] - data[i, ch2.pos.2] <- temp.pos[temp.pos[, geno] == ch2, trait.aj] - } + l.pri <- (ch1.m.pri * data[, ch1.w] + ch2.m.pri * data[, ch2.w]) / (data[, ch1.w] + data[, ch2.w]) + l.pos <- (ch1.m.pos * data[, ch1.w] + ch2.m.pos * data[, ch2.w]) / (data[, ch1.w] + data[, ch2.w]) - # Weights for closest checks - - data[i, ch1.w] <- ncb + 1 - abs(temp[temp[, geno] == ch1, col] - geno.col) - data[i, ch2.w] <- ncb + 1 - abs(temp[temp[, geno] == ch2, col] - geno.col) + af <- apply(cbind(l.pri, l.pos), 1, mean) } - } - # Adjust values with method 1 + # Make adjustment - if (method == 1) { - chs <- c(ch1, ch2, ch1.pri.1, ch2.pri.1, ch1.pos.1, ch2.pos.1) - if (nr == 5) - chs <- c(chs, ch1.pri.2, ch2.pri.2, ch1.pos.2, ch2.pos.2) - af <- apply(data[, chs], 1, mean, na.rm = TRUE) - } + data[, trait.aj] <- data[, trait.aj] / (1 + af) - # Adjust values with method 2 - - if (method == 2) { - - if (nr == 3) { - ch1.m.pri <- apply(data[, c(ch1, ch1.pri.1)], 1, mean, na.rm = TRUE) - ch2.m.pri <- apply(data[, c(ch2, ch2.pri.1)], 1, mean, na.rm = TRUE) - ch1.m.pos <- apply(data[, c(ch1, ch1.pos.1)], 1, mean, na.rm = TRUE) - ch2.m.pos <- apply(data[, c(ch2, ch2.pos.1)], 1, mean, na.rm = TRUE) - } - - if (nr == 5) { - foo <- function(x) x * c(1.5, 2, 1) - ch1.w.pri <- t(apply(!is.na(data[, c(ch1, ch1.pri.1, ch1.pri.2)]), 1, foo)) - ch2.w.pri <- t(apply(!is.na(data[, c(ch2, ch2.pri.1, ch2.pri.2)]), 1, foo)) - ch1.w.pos <- t(apply(!is.na(data[, c(ch1, ch1.pos.1, ch1.pos.2)]), 1, foo)) - ch2.w.pos <- t(apply(!is.na(data[, c(ch2, ch2.pos.1, ch2.pos.2)]), 1, foo)) + } else { + + for(i in 1:dim(data)[1]) { - ch1.m.pri <- data[, c(ch1, ch1.pri.1, ch1.pri.2)] * ch1.w.pri - ch2.m.pri <- data[, c(ch2, ch2.pri.1, ch2.pri.2)] * ch2.w.pri - ch1.m.pos <- data[, c(ch1, ch1.pos.1, ch1.pos.2)] * ch1.w.pos - ch2.m.pos <- data[, c(ch2, ch2.pos.1, ch2.pos.2)] * ch2.w.pos + geno.row <- data[i, row] + geno.col <- data[i, col] + rows <- (geno.row - nr %/% 2):(geno.row + nr %/% 2) + columns <- (geno.col - ncb):(geno.col + ncb) - ch1.m.pri <- apply(ch1.m.pri, 1, sum, na.rm = TRUE) - ch2.m.pri <- apply(ch2.m.pri, 1, sum, na.rm = TRUE) - ch1.m.pos <- apply(ch1.m.pos, 1, sum, na.rm = TRUE) - ch2.m.pos <- apply(ch2.m.pos, 1, sum, na.rm = TRUE) + cond1 <- data[, col] %in% columns & data[, row] %in% rows + cond2 <- data[, geno] %in% c(ch1, ch2) - ch1.w.pri <- apply(ch1.w.pri, 1, sum, na.rm = TRUE) - ch2.w.pri <- apply(ch2.w.pri, 1, sum, na.rm = TRUE) - ch1.w.pos <- apply(ch1.w.pos, 1, sum, na.rm = TRUE) - ch2.w.pos <- apply(ch2.w.pos, 1, sum, na.rm = TRUE) + temp <- data[cond1 & cond2, trait.aj] - ch1.m.pri <- ch1.m.pri / ch1.w.pri - ch2.m.pri <- ch2.m.pri / ch2.w.pri - ch1.m.pos <- ch1.m.pos / ch1.w.pos - ch2.m.pos <- ch2.m.pos / ch2.w.pos + if (length(temp) > 0 & !(data[i, geno] %in% c(ch1, ch2))) { + af <- mean(temp) + data[i, trait.aj] <- data[i, trait.aj] / (1 + af) + } } - - l.pri <- (ch1.m.pri * data[, ch1.w] + ch2.m.pri * data[, ch2.w]) / (data[, ch1.w] + data[, ch2.w]) - l.pos <- (ch1.m.pos * data[, ch1.w] + ch2.m.pos * data[, ch2.w]) / (data[, ch1.w] + data[, ch2.w]) - - af <- apply(cbind(l.pri, l.pos), 1, mean) } - - # Make adjustment - - data[, trait.aj] <- data[, trait.aj] / (1 + af) # Return diff --git a/R/checks.R b/R/checks.R index 4cc3742..a27b97a 100644 --- a/R/checks.R +++ b/R/checks.R @@ -301,7 +301,7 @@ ck.2f <- function(trait, A, B, rep, data) { #' @param col Label for columns. #' @param ncb Number of columns between two check columns. #' @param data The name of the data frame. -#' @return Five control values (\code{c1}, \code{c2}, \code{c3}, \code{c4}, \code{c5}) +#' @return Four control values (\code{c1}, \code{c2}, \code{c3}, and \code{c4}, #' for the grid of checks, the number of missing values for checks (\code{nmis.check}) #' and genotypes \code{nmis}, and the proportion of missing values for checks #' (\code{pmis.check}) and genotypes (\code{pmis}). @@ -332,10 +332,9 @@ ck.w <- function(trait, geno, ch1, ch2, row, col, ncb, data) { # Controls c1 <- 0 # All column checks with checks - c2 <- 0 # Last column with checks - c3 <- 0 # All column genotypes with genotypes - c4 <- 0 # Alternating checks without in correlative row order - c5 <- 0 # All genotypes with checks to the left and right + c2 <- 0 # All column genotypes with genotypes + c3 <- 0 # Alternating checks without in correlative row order + c4 <- 0 # All genotypes with checks to the left and right # Columns with checks @@ -346,26 +345,21 @@ ck.w <- function(trait, geno, ch1, ch2, row, col, ncb, data) { if (sum(!(data[data[, col] %in% cch, geno] %in% checks)) > 0) c1 <- 1 - # Last column with checks - - if (max(cch) != nc.max) - c2 <- 1 - # Check columns with genotypes if (sum(data[!(data[, col] %in% cch), geno] %in% checks) > 0) - c3 <- 1 + c2 <- 1 # Alternating checks for (i in cch) for (j in (min(data[data[, col] == i, row]) + 1):max(data[data[, col] == i, row])) if (data[data[, col] == i & data[, row] == j, geno] == data[data[, col] == i & data[, row] == j - 1, geno]) - c4 <- 1 + c3 <- 1 for (i in 2:length(cch)) if (data[data[, col] == cch[i] & data[, row] == nr.min, geno] == data[data[, col] == cch[i - 1] & data[, row] == nr.min, geno]) - c4 <- 1 + c3 <- 1 # All genotypes must have one check to the left and one to the right @@ -374,7 +368,7 @@ ck.w <- function(trait, geno, ch1, ch2, row, col, ncb, data) { columns <- (data[i, col] - ncb):(data[i, col] + ncb) temp <- data[data[, row] == rows & data[, col] %in% columns, geno] if (sum(temp %in% checks) == 0) - c5 <- 1 + c4 <- 1 } # Number of missing values for checks @@ -395,6 +389,6 @@ ck.w <- function(trait, geno, ch1, ch2, row, col, ncb, data) { # Return - list(c1 = c1, c2 = c2, c3 = c3, c4 = c4, c5 = c5, nmis = nmis, pmis = pmis, + list(c1 = c1, c2 = c2, c3 = c3, c4 = c4, nmis = nmis, pmis = pmis, nmis.check = nmis.check, pmis.check = pmis.check) } diff --git a/man/aj.w.Rd b/man/aj.w.Rd index 5706bc9..dfe1d86 100644 --- a/man/aj.w.Rd +++ b/man/aj.w.Rd @@ -2,13 +2,13 @@ % Please edit documentation in R/aj_westcott.R \name{aj.w} \alias{aj.w} -\title{Adjust values with a grid of checks} +\title{Adjust values following the method of Westcott} \usage{ aj.w(trait, geno, ch1, ch2, row, col, nr = 5, ncb = 10, method = 2, p = 0.5, ind = TRUE, data) } \arguments{ -\item{trait}{The trait to analyze.} +\item{trait}{The trait to adjust.} \item{geno}{The genotypes.} @@ -55,6 +55,9 @@ If \code{p = 1} then the values are adjusted in the same proportion that the checks vary around the field. For values lower than 1 the values are adjusted based on that proportion over the checks variation. If \code{p = 0} then there is no adjustment. + +If the layout does not correspond with the Westcott method, then the observed values +are adjusted with the values of the checks planted nearby and a warning is issued. } \references{ Westcott, B. (1981). Two methods for early generation yield assessment in winter wheat. diff --git a/man/ck.w.Rd b/man/ck.w.Rd index 6bb9cd4..d7f0570 100644 --- a/man/ck.w.Rd +++ b/man/ck.w.Rd @@ -24,7 +24,7 @@ ck.w(trait, geno, ch1, ch2, row, col, ncb, data) \item{data}{The name of the data frame.} } \value{ -Five control values (\code{c1}, \code{c2}, \code{c3}, \code{c4}, \code{c5}) +Four control values (\code{c1}, \code{c2}, \code{c3}, and \code{c4}, for the grid of checks, the number of missing values for checks (\code{nmis.check}) and genotypes \code{nmis}, and the proportion of missing values for checks (\code{pmis.check}) and genotypes (\code{pmis}).