envir_b<-ls()
## Regression frame
dta_frm<-dta[,c(outs,sts,adjs_10m)]
colnames(dta_frm)[1]<-"outs"

lst<-list()
## 'rtreat' horisontal, 'pase_0_bin' vertical
for (i in 1:length(outs)){
  for (j in 1:length(sts)){
    ls_22<-list(print_diff_bygroup(meas="outs",group=sts[j],var=sts[length(sts)+1-j],adj=adjs_10m,data=dta_frm))
    names(ls_22)<-paste0(outs[i],"_",sts[j],"_ver_",sts[length(sts)+1-j],"_hor")
    lst<-append(lst,ls_22)
  }
  lst<-append(lst,print_pred_stratum(meas="outs",adj=c(sts[1],adjs_10m),strat=sts[2],data=dta_frm,include.stratum = T)[1])
}

# outs (outcome), Active
oadr<-lst[[2]][c(6,8)][2,1] # LvsH, Diff raw
oada<-lst[[2]][c(6,8)][2,2] # LvsH, Diff adj
#oada_1<-oada

# outs, Placebo
opdr<-lst[[2]][c(6,8)][3,1] # LvsH, Diff raw
opda<-lst[[2]][c(6,8)][3,2] # LvsH, Diff adj
#opda_1<-opda

spc<-""
spc4<-c("","","","")

mdi1_tbl<-rbind(lst[[1]],matrix(c("Unadjusted mean diff.",spc,oadr,spc,opdr,spc4,
                                  "Adjusted mean diff.",spc,oada,spc,opda,spc4),ncol=ncol(lst[[1]]),byrow=T,dimnames = list(c("a","b"),names(lst[[1]]))))

# write.csv(mdi1_tbl,"mdi1_2x2.csv")

reg_frm<-cbind(mdi1_tbl[,1],"",mdi1_tbl[,2:6],mdi1_tbl[,8])
names(reg_frm)<-c("By_PA", "Rand_Total", "N_Active", "Mean_Active", "N_Placebo", "Mean_Placebo","Unadjusted_mean_diff", "Adjusted_mean_diff")
reg_frm[1,1]<-"PASE_total"
reg_frm[1,2]<-paste0(round(mean(dta_frm$outs, na.rm = TRUE), 1)," (",round(sd(dta_frm$outs, na.rm = TRUE), 1),")")
reg_frm[1,3]<-nrow(dta_frm[dta_frm$rtreat=="Active"&!is.na(dta_frm$outs),])
reg_frm[1,4]<-paste0(round(mean(dta_frm$outs[dta_frm$rtreat=="Active"], na.rm = TRUE), 1)," (",round(sd(dta_frm$outs[dta_frm$rtreat=="Active"], na.rm = TRUE), 1),")")
reg_frm[1,5]<-nrow(dta_frm[dta_frm$rtreat=="Placebo"&!is.na(dta_frm$outs),])
reg_frm[1,6]<-paste0(round(mean(dta_frm$outs[dta_frm$rtreat=="Placebo"], na.rm = TRUE), 1)," (",round(sd(dta_frm$outs[dta_frm$rtreat=="Placebo"], na.rm = TRUE), 1),")")

## Det var fedt med en universel løsning her, så den vender rigtigt i forhold til vektoren
reg_frm[2,2]<-paste0(round(mean(dta_frm$outs[dta_frm$pase_0_bin=="lower"], na.rm = TRUE), 1)," (",round(sd(dta_frm$outs[dta_frm$pase_0_bin=="lower"], na.rm = TRUE), 1),")")
reg_frm[3,2]<-paste0(round(mean(dta_frm$outs[dta_frm$pase_0_bin=="higher"], na.rm = TRUE), 1)," (",round(sd(dta_frm$outs[dta_frm$pase_0_bin=="higher"], na.rm = TRUE), 1),")")

reg_frm[4,2]<-lst[[3]][[1]][7,3]
reg_frm[5,2]<-lst[[3]][[1]][7,4]

reg_frm[1,7]<-lst[[3]][[1]][4,3]
reg_frm[1,8]<-lst[[3]][[1]][4,4]

source("/Volumes/Data/func/remove_all_but.R")

remove_all_but(c("reg_frm",envir_b))