library(tidyverse)
library(magrittr)
library(readxl)
library(openxlsx)
library(lubridate)
library(cowplot)
library(lsmeans)
library(lme4)
library(lmerTest) # import lmerTest *AFTER* lme4
Define functions for histogram and QQ plot:
norm.hist <- function(data.vec, xlab){ # function to draw normal density plot
hist.x <- sort(na.omit(data.vec))
hist.seq <- seq(min(hist.x), max(hist.x), length.out=length(hist.x))
hist.d <- dnorm(hist.seq, mean(hist.x), sd(hist.x))
df.hist <- data.frame(x = hist.x, seq = hist.seq, d = hist.d)
ggplot(df.hist) + geom_histogram(aes(x = x, y = ..density..)) +
geom_line(aes(x = seq, y = d), colour = "red", size = 1.2) +
labs(x = xlab, y = "Frequency")
}
draw.qq <- function(data.vec, xlab){ # function to draw QQ-plot
y <- sort(na.omit(data.vec))
probs <- (1:length(y))/(length(y) + 1)
norm.quantiles = qnorm(probs, mean(y), sd(y))
df.qq <- data.frame(y = y, nq = norm.quantiles)
p <- ggplot(df.qq, aes(x = nq, y = y)) + geom_point() +
geom_abline(slope = 1, intercept = 0, color='red') +
labs(x = "Normal Quantiles", y = xlab) +
geom_abline(slope = 1, intercept = 0, color='red')
}
Preparation of Wenjie_first experiment: W1 flowering data.
# Read in indiv plant data from excel
in_name <- "wenjie_first-2020-04-23.xlsx"
infile <- file.path(path.expand("~"),"FlrGWAS","Flr_Data", in_name)
df <- read_excel(infile, sheet = 1)
dim(df)
## [1] 2172 7
df2 <- df %>% rename(RefPop = GenoNum,
Experiment = ExptNum) %>%
mutate(RepNum = as.factor(RepNum),
RackNum = as.factor(RackNum)) %>%
select(Experiment, FlrAge, PltNum, RefPop, RepNum, RackNum, Alive2)
names(df2)
## [1] "Experiment" "FlrAge" "PltNum" "RefPop" "RepNum"
## [6] "RackNum" "Alive2"
(NumPlanted <- nrow(df2))
## [1] 2172
Prepare data:
df2$Alive2 <- ifelse(df2$Alive2 == 2, 1, df2$Alive2) # Alive at time 2
df2 <- filter(df2, Alive2 >= 0)
(NumSurvived <- sum(df2$Alive2))
## [1] 1474
df3 <- filter(df2, FlrAge > 0) # Exclude if FlrAge is nonpositive
(NumFlowered <- nrow(df3))
## [1] 1474
glimpse(df3)
## Rows: 1,474
## Columns: 7
## $ Experiment <chr> "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1"…
## $ FlrAge <dbl> 11, 17, 17, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19…
## $ PltNum <dbl> 466, 868, 358, 2149, 1319, 1420, 2002, 2029, 411, 864, 127…
## $ RefPop <chr> "RP116", "RP481", "RP311", "RP453", "RP259", "RP455", "RP5…
## $ RepNum <fct> 2, 2, 1, 5, 4, 4, 5, 5, 1, 2, 3, 2, 3, 4, 3, 5, 5, 1, 3, 4…
## $ RackNum <fct> 4, 6, 2, 15, 10, 10, 14, 14, 3, 6, 9, 4, 9, 10, 9, 13, 14,…
## $ Alive2 <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
(maxFlrAge <- max(df3$FlrAge))
## [1] 121
(num_genos <- n_distinct(df3$RefPop, na.rm = TRUE)) # Number of flowering genotypes)
## [1] 402
Now, histogram and QQ-plot with original and log-transformed Flower_Age:
p1 <- norm.hist(df3$FlrAge, "Flower_Age")
p2 <- draw.qq(df3$FlrAge, "Flower_Age")
plot_grid(p1, p2, nrow = 1, labels = c("A", "B")) # join & print figures
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
The original flowering distribution deviates slightly from normality
df3 <- df3 %>% mutate(LogFlrAge = log10(FlrAge))
names(df3)
## [1] "Experiment" "FlrAge" "PltNum" "RefPop" "RepNum"
## [6] "RackNum" "Alive2" "LogFlrAge"
p1 <- norm.hist(df3$LogFlrAge, "Log10FlrAge")
p2 <- draw.qq(df3$LogFlrAge, "Log10FlrAge")
plot_grid(p1, p2, nrow = 1, labels = c("A", "B")) # join & print figures
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
The log transformed flowering distribution has slightly improved normality. For consistency among experiments, we will analyze this.
Save data file for GxE analysis
df3.W1 <- df3 %>% select(Experiment, RefPop, RackNum, LogFlrAge)
file_name <- "W1_plants_LogFlr.txt"
out_name <- file.path(path.expand("~"),"FlrGWAS", "output", file_name)
write_tsv(df3.W1, out_name)
Mean flowering time in Wenjie’s first experiment (original scale):
(FlrInfo <- select(df3, FlrAge) %>%
summarize_all(list(mean = mean, sd = sd), na.rm = TRUE))
## # A tibble: 1 x 2
## mean sd
## <dbl> <dbl>
## 1 33.6 11.4
Summary: The original number of plants was 2172. Of these, 1474 survived until the end of cold treatment. The number of flowering plants was 1474. Surviving plants that did not flower total 0, or 0%. Latest flowering ocurred at 121 days after cold treatment. The number of genotypes that flowered is 402. Average flowering age is 33.5814 +/- 0.297.
Compute random effects ANOVA:
# See review on formulas:
# https://conjugateprior.org/2013/01/formulae-in-r-anova/
# Excluding RepNum to allow model convergence
mod <- lmer(LogFlrAge ~ (1 | RefPop) + (1 | RackNum), data=df3)
summary(mod)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: LogFlrAge ~ (1 | RefPop) + (1 | RackNum)
## Data: df3
##
## REML criterion at convergence: -2277
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.131 -0.570 -0.059 0.508 4.700
##
## Random effects:
## Groups Name Variance Std.Dev.
## RefPop (Intercept) 0.00829 0.0910
## RackNum (Intercept) 0.00129 0.0359
## Residual 0.00806 0.0898
## Number of obs: 1474, groups: RefPop, 402; RackNum, 15
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 1.5043 0.0109 19.2849 138 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Notice that RepNum was excluded to enable model convergence
Get BLUPs
blup <- ranef(mod) # get BLUPs
RP.blup <- as.vector(blup$RefPop[, 1]) # vector of BLUPs for each genotype
# get RefPop means
df.byRefPop <- df3 %>% group_by(RefPop)
RP.means <- summarize(df.byRefPop, RPmean = mean(LogFlrAge))
RP_IDs <- RP.means$RefPop # vector of RefPop IDs
RP.means <- as.vector(RP.means$RPmean) #vector of genotype means
df.mn.blup <- tibble(ID = RP_IDs, RP_meanW1 = RP.means, RP_blupW1 = RP.blup)
dim(df.mn.blup)
## [1] 402 3
cor(RP.means, RP.blup) # Correlation between BLUPs and RefPop means
## [1] 0.9739
Conclusion: BLUP estimates are highly correlated with genotype means.
# histogram of BLUPs:
norm.hist(df.mn.blup$RP_blupW1, "LogFlrAge BLUPs")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#Output BLUP data frame
file_name <- "Wenjie_first_LogFlr_BLUPS.txt"
out_name <- file.path(path.expand("~"),"FlrGWAS", "output", file_name)
write_tsv(df.mn.blup, out_name)
Variance components, Coefficient of Determination, & Heritability
Residual Var Comp is taken from summary()
printout. Change manually if the data or model change.
# Residual variance component for this model only
# Change this value if the model or data are changed
vc.Resid <- 0.00806 # This is model specific! ***
Next, compute R-squared and Heritability
vcs <- summary(mod)$varcor
# str(vcs)
(vc.RefPop <- vcs$RefPop[1,1])
## [1] 0.008289
# (vc.RepNum <- vcs$RepNum[1,1])
(vc.Block <- vcs$RackNum[1,1])
## [1] 0.001288
# (vc.Explained <- sum(vc.RefPop + vc.RepNum + vc.Block)) # total of variance components
(vc.Explained <- sum(vc.RefPop + vc.Block)) # total of variance components
## [1] 0.009576
(vc.Tot <- (vc.Explained + vc.Resid))
## [1] 0.01764
(R2 <- vc.Explained / vc.Tot) # R-squared
## [1] 0.543
(H2 <- vc.RefPop / vc.Tot) # heritability
## [1] 0.47
Conclusions:
The model explains 54.2993% of variation in log-transformed age at first flowering. Heritability of LogFlrAge = 46.9965%.