International Rice Research Institute commited on
Update app.R
Browse files
app.R
CHANGED
|
@@ -8,7 +8,7 @@ library(emmeans)
|
|
| 8 |
library(plotly)
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
| 11 |
-
library(blme) #
|
| 12 |
|
| 13 |
# UI with updated styling and improved plot containers
|
| 14 |
ui <- fluidPage(
|
|
@@ -360,7 +360,7 @@ ui <- fluidPage(
|
|
| 360 |
),
|
| 361 |
selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
|
| 362 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
| 363 |
-
checkboxInput("use_blme", "Use Bayesian Model for Singular Fit", value = FALSE),
|
| 364 |
div(
|
| 365 |
class = "sidebar-content",
|
| 366 |
div(
|
|
@@ -968,7 +968,7 @@ server <- function(input, output, session) {
|
|
| 968 |
})
|
| 969 |
}
|
| 970 |
|
| 971 |
-
# LME4: Calculate heritability (
|
| 972 |
lme4_calculate_heritability <- function(model, trait) {
|
| 973 |
message("LME4: Calculating heritability for ", trait)
|
| 974 |
if (is.null(model)) {
|
|
@@ -978,17 +978,17 @@ server <- function(input, output, session) {
|
|
| 978 |
varcomp <- as.data.frame(VarCorr(model))
|
| 979 |
vg <- varcomp$vcov[varcomp$grp == "Genotype"]
|
| 980 |
vge <- varcomp$vcov[varcomp$grp == "Environment:Genotype"]
|
| 981 |
-
ve <- sigma(model)^2
|
| 982 |
|
| 983 |
# Check for valid variance components
|
| 984 |
if (length(vg) == 0 || is.na(vg) || vg <= 0) {
|
| 985 |
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero genetic variance"))
|
| 986 |
}
|
| 987 |
if (is.na(ve) || ve <= 0) {
|
| 988 |
-
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero
|
| 989 |
}
|
| 990 |
|
| 991 |
-
#
|
| 992 |
if (input$lme4_include_interaction && !is.na(vge) && vge > 0) {
|
| 993 |
h2 <- (vg + vge) / (vg + vge + ve)
|
| 994 |
} else {
|
|
|
|
| 8 |
library(plotly)
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
| 11 |
+
library(blme) # For Bayesian modeling to handle singular fits
|
| 12 |
|
| 13 |
# UI with updated styling and improved plot containers
|
| 14 |
ui <- fluidPage(
|
|
|
|
| 360 |
),
|
| 361 |
selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
|
| 362 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
| 363 |
+
checkboxInput("use_blme", "Use Bayesian Model for Singular Fit", value = FALSE),
|
| 364 |
div(
|
| 365 |
class = "sidebar-content",
|
| 366 |
div(
|
|
|
|
| 968 |
})
|
| 969 |
}
|
| 970 |
|
| 971 |
+
# LME4: Calculate heritability (modified to use only environmental variance)
|
| 972 |
lme4_calculate_heritability <- function(model, trait) {
|
| 973 |
message("LME4: Calculating heritability for ", trait)
|
| 974 |
if (is.null(model)) {
|
|
|
|
| 978 |
varcomp <- as.data.frame(VarCorr(model))
|
| 979 |
vg <- varcomp$vcov[varcomp$grp == "Genotype"]
|
| 980 |
vge <- varcomp$vcov[varcomp$grp == "Environment:Genotype"]
|
| 981 |
+
ve <- sigma(model)^2 # Residual variance (used as environmental variance)
|
| 982 |
|
| 983 |
# Check for valid variance components
|
| 984 |
if (length(vg) == 0 || is.na(vg) || vg <= 0) {
|
| 985 |
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero genetic variance"))
|
| 986 |
}
|
| 987 |
if (is.na(ve) || ve <= 0) {
|
| 988 |
+
return(data.frame(Trait = trait, Heritability = NA, Error = "Invalid or zero environmental variance"))
|
| 989 |
}
|
| 990 |
|
| 991 |
+
# Calculate heritability using only environmental variance
|
| 992 |
if (input$lme4_include_interaction && !is.na(vge) && vge > 0) {
|
| 993 |
h2 <- (vg + vge) / (vg + vge + ve)
|
| 994 |
} else {
|