# Create dataframe for dcross

sel_df <- data.frame(id = DH@id, S_dr)

#------ Select pairs for product development ----------- 

dcross25_res <- dCross(sel_df, H_dr,                           
                           n_sel = nSelProdDev,                            
                           lambda = wt*0.25,                       
                           n_balance_factors = k_maxfit-1,            
                           n_top = 1000)   


pairs <- dcross25_res$pair_ids

# Record (out of interest) the expected progeny (F1) averages
id_lookup <- dcross25_res$selected_df$id
X1_lookup <- dcross25_res$selected_df$X1
X2_lookup <- dcross25_res$selected_df$X2

# Match ids in pairs to rows in selected_df
X1 <- (X1_lookup[match(pairs$id1, id_lookup)] + 
         X1_lookup[match(pairs$id2, id_lookup)]) / 2
X2 <- (X2_lookup[match(pairs$id1, id_lookup)] + 
         X2_lookup[match(pairs$id2, id_lookup)]) / 2

# Final result
F1dcross25_predicted_averages <- data.frame(pairs, X1, X2)


#---- Create crossing plan ----- 

dcross25_crossPlan <- rbind(cbind(as.character(pairs[,1]), as.character(pairs[,2])))

#---- Make PD population and record GVs in a dataframe ----

F1dcross25 = makeCross(DH, dcross25_crossPlan, simParam=SP)
DHdcross25 <- makeDH(F1dcross25, nDHProdDev)

df_dcross25 <- data.frame(DHdcross25@gv[,-1], strat = "dcross0.25")
f1_dcross25 <- data.frame(F1dcross25@gv[,-1], strat = "dcross0.25")


