4c_reversestage_analysis.R 5.17 KB
Newer Older
luroth's avatar
luroth committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Working directory with temperature data
path_home <- 'E:/Scripts/htfp_data_processing'
path_simulation <- 'E:/Simulation/Runs'
setwd(path_home)


# Libraries to use
library(readr)
library(tidyr)
library(purrr)
library(ggplot2)
library(lubridate)
library(gridExtra)
library(plyr)
library(stringr)
library(asreml)

library(dplyr)

source("R/Model/FitREML.R")
source("R/Model/Graphs.R")

start_run <- 1
max_runs <- 500
#500

number_of_cpus <- 5

sigma_error <- 10

# Plot control overview
plot_ids <- c(
  "FIP20150006",
  "FIP20160002",
  "FIP20170009",
  "FIP20180013",
  "FIP20190003")



## Measurement frequencies
measurement_dates_freq_1d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 1), "%m%d")
)

measurement_dates_freq_2d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 2), "%m%d")
)

measurement_dates_freq_3d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 3), "%m%d")
)

measurement_dates_freq_4d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 4), "%m%d")
)

measurement_dates_freq_5d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 5), "%m%d")
)

measurement_dates_freq_6d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 6), "%m%d")
)

measurement_dates_freq_7d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 7), "%m%d")
)


measurement_dates_freq_9d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 9), "%m%d")
)

measurement_dates_freq_11d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 11), "%m%d")
)

measurement_dates_freq_13d <- c(
  format(seq.Date(from = as.Date("2020-03-01"), as.Date("2020-07-20"), by = 13), "%m%d")
)



measurement_dates_sets <- list(
  "3 d" = measurement_dates_freq_3d)

# Read data
df_designs <- read_csv('Simulation/designs.csv')
df_designs <- df_designs %>% mutate(plot.discrete_x = plot.row * if_else(plot.replication > 1, 2, 1),
                                    plot.discrete_y = plot.range * if_else(plot.replication > 1, 2, 1))

df_genotypes <- read_csv('Simulation/genotypes.csv')
df_temp <- read_csv('Simulation/covariate_temp.csv')



run <- 19
for(run in start_run:max_runs) {

  #try({
  # Validation data
  df_genotypes_true <- read_csv(paste0(path_simulation, "/", run, "/genotypes_params.csv"))
  df_genotypes_true <- df_genotypes_true %>% mutate(
    bplm_slope = bplm_Asym / (bplm_cOpt- bplm_c0))
  df_genotypes_true <- df_genotypes_true %>%
    pivot_longer(c(start_growth, stop_growth, bplm_c0, bplm_cOpt, bplm_Asym, bplm_slope), names_to = "parameter")
  
  
  print(paste0("Run ", run))
  
  
  i <- length(measurement_dates_sets)
  for(i in length(measurement_dates_sets):1) {
    measurement_dates_set_name = names(measurement_dates_sets)[[i]]
    measurement_dates_set = measurement_dates_sets[[i]]
    set <- paste0("set", str_replace_all(measurement_dates_set_name, " ", ""))
    print(paste0("Set: ", measurement_dates_set_name))
    
    
    files <- Sys.glob(paste0(path_simulation, "/", run, "/", set, "*_genotype_true_versus_predict_onestage.csv"))
    
    if(TRUE | length(files)==0) {
      
      df_params_BLUES_yearsite <- read_csv(paste0(path_simulation, "/", run, "/", set, "_year_site_BLUE_predict.csv"))
      df_params_BLUES_yearsite <- df_params_BLUES_yearsite %>% filter(model %in% c("spline"), parameter != "final_height")
      df_params_BLUES_yearsite$weights_vcov <- 1/(df_params_BLUES_yearsite$BLUE_SE^2)

      print("Fit stage 2 (asreml-R)")

      df_BLUEs_weights <- df_params_BLUES_yearsite %>% filter(!is.na(use_weights)) %>%
        mutate(year_site.UID = year_site.UID_) %>% group_by(parameter, model, stage1) %>%
        nest() %>% mutate(BLUEs = map(data, fit_REML, parameter = paste(parameter, model), use_weights=T)) %>%
        select(-data) %>% unnest(BLUEs) %>% unnest(BLUE)
      df_BLUEs_weights$stage2 <- "weighted"

      df_BLUEs_no_weights <- df_params_BLUES_yearsite %>% filter(!is.na(use_weights)) %>%
        mutate(year_site.UID = year_site.UID_) %>% group_by(parameter, model, stage1) %>%
        nest() %>% mutate(BLUEs = map(data, fit_REML, parameter = paste(parameter, model), use_weights=F)) %>%
        select(-data) %>% unnest(BLUEs) %>% unnest(BLUE)
      df_BLUEs_no_weights$stage2 <- "not weighted"

      df_BLUES <- bind_rows(df_BLUEs_weights, df_BLUEs_no_weights)
      df_BLUES <- df_BLUES %>% rename(predict = BLUE)

      # Validate with true values
      df_BLUES_validate <- inner_join(df_genotypes_true, df_BLUES, by=c("genotype.id", "parameter"))

      df_BLUES_validate <- df_BLUES_validate %>% mutate(label = paste(parameter, model, stage1, stage2, sep="_"))

      # plot <- ggplot(data = df_BLUES_validate, aes(value, predict)) +
      #   geom_point() +
      #   geom_abline(intercept = 0, slope=1) +
      #   facet_wrap(~label, scales ="free") +
      #   ggtitle(paste("Run:", run, set))
      # plot(plot)

      df_BLUES_validate$set <- set
      write_csv(df_BLUES_validate, paste0(path_simulation, "/", run, "/", set, "_reversegenotype_true_versus_predict.csv"))

    } else {
      print("Results present, skip")
    }
  }
  #})
}