## Created by EmpowerStats @ Thu, 18 Sep 25 19:43:22 +0800## 
#******************** Regarding ALL Following R Code   ******************** 
#*** COPYRIGHT (c) 2010, 2021 X&Y Solutions, ALL RIGHT RESERVED *********** 
#******************* www.EmpowerStats.com ********************************* 
#************************************************************************** 
Sys.setlocale(category = 'LC_ALL', locale = 'English_United States.1252'); 
.libPaths(file.path(R.home(),'library')); 
library(doBy); 
options(timeout=600); 
library(plotrix); 
library(stringi); 
library(stringr); 
library(survival); 
library(rms); 
library(nnet); 
library(car); 
library(mgcv); 
pdfwd<-6; pdfht<-6; 
load('D:/EmpowerRCH/Analysis/zfg2/longitudinal 2010.Rdata'); 
if (length(which(ls()=='EmpowerStatsR'))==0) EmpowerStatsR<-get(ls()[1]); 
names(EmpowerStatsR)<-toupper(names(EmpowerStatsR)); 
originalVNAME<-names(EmpowerStatsR); 
ofname<-'zfg2_190_tbl'; 
g2idx<-function(x) {
 g<-levels(factor(x));  y<-rep(NA,length(x))
 for (i in 1:length(g)) y[x==g[i]]<-i;
 y[is.na(y)]<-length(g)+1;
 return(y);
}
zscorevname<-function(xvname, gvname, mu, std, ref, stratum=c()) {
 tmpd<-EmpowerStatsR[,xvname]; tmpg_<-rep(1,length(tmpd));
 ifelse(gvname>"", tmpd<-cbind(tmpd,EmpowerStatsR[,gvname]), tmpd<-cbind(tmpd,tmpg_));
 ng<-length(levels(factor(tmpd[,2])))
 if (ref>"") {
   tmpd0<-EmpowerStatsR[eval(parse(text=ref)),xvname]; tmpg0_<-rep(1,length(tmpd0));
   ifelse(gvname>"", tmpd0<-cbind(tmpd0,EmpowerStatsR[eval(parse(text=ref)),gvname]), tmpd0<-cbind(tmpd0,tmpg0_));
 } else { tmpd0<-tmpd; }
 ng0<-length(levels(factor(tmpd0[,2])))
 if (ng!=ng0) tmpd0<-tmpd
 tmpd[,2]<-g2idx(tmpd[,2]); tmpd0[,2]<-g2idx(tmpd0[,2])
 nmu<-length(mu)
 if (nmu==0 | nmu!=length(std) | !is.numeric(mu) | !is.numeric(std) | nmu!=ng) mu<-c()
 if (length(stratum)>0) {
   if (length(mu) != length(stratum)) mu <- c()
 }
 if (length(mu)==0) {
   mu<-c(tapply(tmpd0[,1],tmpd0[,2], mean, na.rm=TRUE))
   std<-c(tapply(tmpd0[,1],tmpd0[,2], sd, na.rm=TRUE))
 } else {
   if (length(stratum)>0) {
      ord <- match(stratum, as.numeric(levels(factor(tmpd0[,2]))))
      mu <- mu[ord]
      std <- std[ord]
   }
 }
 print("Mean:"); print(mu); print("SD:"); print(std);
 y<-(tmpd[,1]-mu[tmpd[,2]])/std[tmpd[,2]];
 return(y);
} 
attach(EmpowerStatsR); 
sink(paste(ofname,'_datastep.lst',sep='')); 
print('Creating new variable: BMI2'); 
BMI2<-BMI*BMI; 
summary(BMI2); 
EmpowerStatsR<-cbind(EmpowerStatsR,BMI2); 
print('Creating new variable: ALT.2'); 
ALT.2<-ALT; 
ALT.2[(is.na(ALT))]<-16; 
summary(ALT.2); 
EmpowerStatsR<-cbind(EmpowerStatsR,ALT.2); 
print('Creating new variable: AST.2'); 
AST.2<-AST; 
AST.2[(is.na(AST))]<-21; 
summary(AST.2); 
EmpowerStatsR<-cbind(EmpowerStatsR,AST.2); 
print('Creating new variable: TP.1'); 
TP.1<-TP; 
TP.1[(is.na(TP))]<-73.9; 
summary(TP.1); 
EmpowerStatsR<-cbind(EmpowerStatsR,TP.1); 
print('Creating new variable: ALB.1'); 
ALB.1<-ALB; 
ALB.1[(is.na(ALB))]<-44.5; 
summary(ALB.1); 
EmpowerStatsR<-cbind(EmpowerStatsR,ALB.1); 
print('Creating new variable: GLB.1'); 
GLB.1<-GLB; 
GLB.1[(is.na(GLB))]<-29.3; 
summary(GLB.1); 
EmpowerStatsR<-cbind(EmpowerStatsR,GLB.1); 
print('Creating new variable: DBP1.1'); 
DBP1.1<-DBP1; 
DBP1.1[(is.na(DBP1))]<-72; 
summary(DBP1.1); 
EmpowerStatsR<-cbind(EmpowerStatsR,DBP1.1); 
print('Creating new variable: CUNBAE'); 
CUNBAE<-rep(NA, times=nrow(EmpowerStatsR)); 
tmp <- is.na(CUNBAE) & (SEX==0); 
tmp <- (!is.na(tmp) & tmp); 
CUNBAE[tmp]<-((0.503*AGE)+10.689+(3.172*BMI)-(0.026*BMI2)+(0.181*BMI)-(0.02*BMI*AGE)-(0.005*BMI2)+(0.00021*BMI2*AGE)-44.988)[tmp]; 
tmp <- is.na(CUNBAE) & (SEX==1); 
tmp <- (!is.na(tmp) & tmp); 
CUNBAE[tmp]<-((0.503*AGE)+(3.172*BMI)-(0.026*BMI2)-(0.02*BMI*AGE)+(0.00021*BMI2*AGE)-44.988)[tmp]; 
summary(CUNBAE); 
EmpowerStatsR<-cbind(EmpowerStatsR,CUNBAE); 
print('Creating new variable: CUNBAE.Z'); 
CUNBAE.Z<- zscorevname("CUNBAE","",c(),c(),"",c()); 
summary(CUNBAE.Z); 
EmpowerStatsR<-cbind(EmpowerStatsR,CUNBAE.Z); 
print('Creating new variable: DBP.CX'); 
DBP.CX<- 0+(DBP1.1>=90); 
DBP.CX[is.na(DBP1.1)]<- NA; 
summary(DBP.CX); 
EmpowerStatsR<-cbind(EmpowerStatsR,DBP.CX); 
rm(BMI2,ALT.2,AST.2,TP.1,ALB.1,GLB.1,DBP1.1,CUNBAE,CUNBAE.Z,DBP.CX); 
sink(); 
vname<-c(NA,'SEX','SEX.0','SEX.1','AGE','NAFLD','NAFLD.0','NAFLD.1','ALT','AST','TP','ALB','GLB','BUN','CR','UA','GLU1','TC1','TG1','HDL1','LDL1','BMI','SBP1','DBP1','MONTH','BMI2','ALT.2','AST.2','TP.1','ALB.1','GLB.1','DBP1.1','CUNBAE','CUNBAE.Z','DBP.CX','DBP.CX.0','DBP.CX.1')[-1]; 
vlabel<-c(NA,'SEX','  0','  1','AGE','NAFLD','  0','  1','ALT','AST','TP','ALB','GLB','BUN','CR','UA','GLU1','TC1','TG1','HDL1','LDL1','BMI','SBP1','DBP1','MONTH','BMI2','ALT','AST','TP','ALB','GLB','DBP1','CUNBAE','CUNBAE Z-score','DBP1 categorical','  <90','  >=90')[-1]; 
varused4this <- c('SEX','AGE','NAFLD','ALT','AST','TP','ALB','GLB','BUN','CR','UA','GLU1','TC1','TG1','HDL1','LDL1','BMI','SBP1','DBP1','MONTH','BMI2','ALT.2','AST.2','TP.1','ALB.1','GLB.1','DBP1.1','CUNBAE','CUNBAE.Z','DBP.CX'); 
pkgs<-c('gdata','geepack','mgcv','survival'); 
for (g in pkgs) {  
if (!(g %in% rownames(installed.packages()))) install.packages(g,repos='https://cloud.r-project.org'); 
}
library(gdata); 
library(geepack); 
library(mgcv); 
library(survival); 
WD <- EmpowerStatsR; rm(EmpowerStatsR); gc(); 
title<-'分层分析DBP'; 
wd.subset=''; 
weights.var<- NA; 
yvname<-c('NAFLD'); 
ydist<-c('breslow'); 
ylink<-c('0'); 
ylv<-c(2); 
xvname<-c('DBP.CX'); 
sxf<-c(0); 
xlv<-c(2); 
svname<-c('ALT.2','AST.2','TP.1','ALB.1','GLB.1','BUN','CR','UA','TC1','TG1','HDL1','LDL1','GLU1','SBP1'); 
sdf<-c(0,0,0,0,0,0,0,0,0,0,0,0,0,0); 
slv<-c(0,0,0,0,0,0,0,0,0,0,0,0,0,0); 
avname<-c('CUNBAE.Z'); 
alv<-c(0); 
cox<- 1; 
timevar<-'MONTH'; timevlv<- 0; 
vname.start<- NA; 
subjvname<- NA; 
gee.TYPE<-NA; 
bvar<- NA; 
par1<-10; 
prn<-1; 
dec<-2; 
##R package## gdata geepack mgcv survival ##R package##;
pvformat<-function(p,dec) {
  pp <- sprintf(paste("%.",dec,"f",sep=""),as.numeric(p))
  if (is.matrix(p)) {pp<-matrix(pp, nrow=nrow(p)); colnames(pp)<-colnames(p);rownames(pp)<-rownames(p);}
  lw <- paste("<",substr("0.00000000000",1,dec+1),"1",sep="");
  pp[as.numeric(p)<(1/10^dec)]<-lw
  return(pp)
}
numfmt<-function(p,dec) {
  if (is.list(p)) p<-as.matrix(p)
  pp <- sprintf(paste("%.",dec,"f",sep=""),as.numeric(p))
  if (is.matrix(p)) {pp<-matrix(pp, nrow=nrow(p));colnames(pp)<-colnames(p);rownames(pp)<-rownames(p);}
  pp[as.numeric(p)>10000000]<- "inf."
  pp[is.na(p) | gsub(" ","",p)==""]<- ""
  pp[p=="-Inf"]<-"-Inf"
  pp[p=="Inf"]<-"Inf"
  return(pp)
}
getLandmarkDF <- function(yvname, WD, timevar, tp) {
  ny<-length(yvname); tp<-c(0, tp, max(WD[,timevar],na.rm=TRUE)); ntp<-length(tp)-1;
  tmpWD<-WD[!is.na(WD[,timevar]),]
  for (i in (1:ntp)) {
    wd.i<-tmpWD[tmpWD[,timevar]>tp[i],]
    for (j in (1:ny)) {wd.i[wd.i[,yvname[j]]==1 & wd.i[,timevar]>tp[i+1],yvname[j]]<-0;}
    wd.i[wd.i[,timevar]>tp[i+1],timevar]<-tp[i+1]
    wd.i<-cbind(wd.i, TIME.PHASE.4LMCOX=i)
    if (i==1) {wd.p<-wd.i;} else {wd.p<-rbind(wd.p,wd.i);}
  }
  return(wd.p)
}
mat2htmltable<-function(mat) {
  t1<- apply(mat,1,function(z) paste(z,collapse="</td><td>"))
  t2<- paste("<tr><td>",t1,"</td></tr>")
  return(paste(t2,collapse=" "))
}
mdl2oo<-function(i, j, b) {
  decp<-dec+2; if (decp>4) decp<-4;
  aaname<-aaname_[[j]]
  fmlx<-paste(avname_[j],fmladj)
  if (b==nblv) fmlx<-paste(fmlx,"+factor(",bvar,")",sep="")
  if (clogit==1) {
    fml<-paste(yvname[i],"~",fmlx,"+strata(",subjvname,")",sep="")
    mdl<-try(clogit(formula(fml),weights=wdtmp$weights,data=wdtmp,na.action=na.omit,method="exact"))
  } else {
    fml<-paste(fmlt,yvname[i],")~",fmlx);
    mdl<-try(coxph(formula(fml),weights=wdtmp$weights,data=wdtmp,na.action=na.omit,method=ydist[i]))
  }
  if (substr(mdl[1],1,5)!="Error") {
    gs<-summary(mdl); print(mdl$formula); print(gs)
    tmpn<-gs$n; gsparm <- gs$coefficients
    gsp<-gsparm[match(aaname,rownames(gsparm)),]
    if (length(aaname)==1) {beta<-gsp[1]; se<-gsp[3]; pv<-gsp[5]; 
    } else {beta<-gsp[,1]; se<-gsp[,3]; pv<-gsp[,5]; }
    ci1<- beta-1.96*se; ci2<- beta+1.96*se
    pvx<-substr(rep("****",length(pv)),1,(pv<=0.05)+(pv<=0.01)+(pv<=0.001)) 
    if (colprn==3) {pvv<-pvx;} else {pvv<-pvformat(pv,decp);}
    o1<-paste(numfmt(exp(beta),dec)," (",numfmt(exp(ci1),dec),", ",numfmt(exp(ci2),dec),")",sep="")
    o1<-paste(o1,pvv); o1[is.na(beta)]<-NA;
  } else {o1<-rep(NA,length(aaname)); tmpn<-nrow(wdtmp); }
  if (length(aaname)>1) {o1[1]<-""; o1[2]<-"1.0";}
  return(list(o1,tmpn))
}
recodevar <- function (var,oldcode,newcode) {
  tmp.v <- var
  nc.tmp <- length(oldcode)
  for (i in (1:nc.tmp)) {tmp.v[(var==oldcode[i])]=newcode[i]}
  if (is.factor(tmp.v)) {tmp.v1<-as.numeric(as.character(tmp.v))} else {tmp.v1<-as.numeric(tmp.v)}
  rm(tmp.v);  return(tmp.v1)
} 
rankvar <- function(var, num) {
  qprobs <- 1/num
  if (num>2) {for (i in (2:(num-1))) {qprobs <- c(qprobs, 1/num * i) } } 
  outvar <- rep(0, times=length(var))
  outvar[is.na(var)] <- NA
  cutpoints <- quantile(var,probs=qprobs, na.rm=TRUE)
  for (k in (1:length(cutpoints))) { outvar[var>=cutpoints[k]] <- k; }
  return(outvar)
}
removeNA<-function(i,j,k,wdf) {
 vvv<-c(yvname[i],avname[j],xvname[k],subjvname,bvar,vname.start,timevar); 
 vvv<-vvv[!is.na(vvv)]; vvv<-vvv[vvv>" "]
 tmp<-is.na(wdf[,vvv]); 
 return(wdf[apply(tmp,1,sum)==0,])
}
varstats<-function(k,j,wdtmp0) {
  vk<-xvname[k]; klv<-xxlvl_[[k]];nklv<-length(klv);
  if (!is.na(j)) {vj<-avname[j]; jlv<-aalvl_[[j]];njlv<-length(jlv);} else njlv<-0;
  if (njlv<=2) {
    a<-table(wdtmp0[,vk])
    return(cbind(xxlbl_[[k]],c(" ",a[match(klv,names(a))])))
  } else {
    a<-table(wdtmp0[,vk],wdtmp0[,vj])
    a<-a[match(klv,rownames(a)),match(jlv,colnames(a))]
    oon<-c(" ",matrix(t(cbind(" ",a)),ncol=1))
    t1<-matrix(rep(paste("&nbsp&nbsp",aalbl_[[j]][-1]),nklv),ncol=nklv)
    return(cbind(c(xb[k],matrix(rbind(xxlbl[[k]],t1),ncol=1)),oon))
  }
}

if (!is.na(bvar)) {
 if (bvar=="TIME.PHASE.4LMCOX") {
  WD<-getLandmarkDF(yvname, WD, timevar, tp);
  tmptimeb <- vlabel[vname==timevar];
  vname<-c(vname,"TIME.PHASE.4LMCOX", "TIME.PHASE.4LMCOX.1");
  vlabel<-c(vlabel, paste(tmptimeb, "segment"), paste("  <=", tp[1]));
  if (length(tp)>1) {
    vname<-c(vname, "TIME.PHASE.4LMCOX.2", "TIME.PHASE.4LMCOX.3");
    vlabel<-c(vlabel,  paste("  >", tp[1], " & <=", tp[2],sep=""),paste("  >", tp[2]));
  } else {
    vname<-c(vname, "TIME.PHASE.4LMCOX.2");
    vlabel<-c(vlabel,  paste("  >", tp[1]));
  }
  bv <- WD[,"TIME.PHASE.4LMCOX"];
}}
if (!is.na(weights.var)) {weights<-WD[,weights.var];} else {weights<-1;}
WD<-cbind(WD,weights);
vlabelN<-(substr(vlabel,1,1)==" ");
vlabelZ<-vlabel[vlabelN];vlabelV<-vlabel[!vlabelN]
vnameV<-vname[!vlabelN];vnameZ<-vname[vlabelN];
w<-c("<!DOCTYPE html><html lang='zh'><head><meta charset='utf-8'></head><body>")
w<-c(w,paste("<h2>", title, "</h2>"))
if (length(svname)>0) { 
  if (sum((sdf=="s" | sdf=="S") & slv>0)>0) w<-c(w,"</br>Spline smoothing only applies for continuous variables")
  sdf[slv>0]<-"";
  if (!is.na(subjvname) & sum(sdf=="s" | sdf=="S")>0) {
    sdf<-rep(0,length(svname));
    w<-c(w,"</br>Generalized estimate equation could not be used with spline smoothing terms")
  }
}
sxf<-as.numeric(sxf);sxf[is.na(sxf)]<-0;
if (sum(xlv==0 & sxf==0)>0) w<-c(w,"</br>Stratified variables should be categorical")
if (sum(sxf>1 & xlv>0)>0) w<-c(w,"Categorizing only applies to continuous variables");
allvname<-c(yvname,xvname,svname,bvar,avname,subjvname,vname.start,timevar,"weights"); 
allvname<-allvname[!is.na(allvname)]
WD<-WD[,allvname];
if (!is.na(subjvname)) WD<-WD[order(WD[,subjvname]),]
sxf[xlv>0]<-0
sxf[xlv==0 & sxf==0]<-3
nx<-length(xvname)
if (sum(sxf>1 & xlv==0)>0) {
     t.xname<-NA;t.xlv<-NA; 
     for (i in 1:nx) {
       if (sxf[i]>1 & xlv[i]==0) {
          tmp.Xi<- rankvar(WD[,xvname[i]],sxf[i])
          tmp.newcode <- tapply(WD[,xvname[i]],tmp.Xi,function(z) median(z,na.rm=TRUE))
          tmp.low <- tapply(WD[,xvname[i]],tmp.Xi,function(z) min(z,na.rm=TRUE))
          tmp.upp <- tapply(WD[,xvname[i]],tmp.Xi,function(z) max(z,na.rm=TRUE))
          tmp.Xi2<- recodevar(tmp.Xi,(1:sxf[i])-1,tmp.newcode)
          tmp.Xi<-cbind(tmp.Xi,tmp.Xi2)
          tmp.NM<-paste(xvname[i],c("grp","grp.cont"),sep=".")
          colnames(tmp.Xi)<-tmp.NM
          WD<-cbind(WD,tmp.Xi)
          t.xname<-c(t.xname,tmp.NM)
          t.xlv<-c(t.xlv,sxf[i],0)
          vnameV<-c(vnameV,tmp.NM)
          vlabelV<-c(vlabelV,paste(vlabelV[vnameV==xvname[i]],c("group","group trend")))
          vnameZ<-c(vnameZ,paste(tmp.NM[1],(1:sxf[i])-1,sep="."))
          vlabelZ<-c(vlabelZ,paste(tmp.low,"-",tmp.upp))
       } else {
          t.xname<-c(t.xname,xvname[i]); t.xlv<-c(t.xlv,xlv[i])
       }
     }
     xvname<-t.xname[-1]; xlv<-t.xlv[-1];
}
xvname<-xvname[xlv>0]; xlv<-xlv[xlv>0]; nx<-length(xvname)
xxlbl_<-list(NA); xxlvl_<-list(NA); xxlbl<-list(NA); 
xb<-vlabelV[match(xvname,vnameV)]; xb[is.na(xb)]<-xvname[is.na(xb)]
for (j in 1:nx) {
  tmp<-levels(factor(WD[,xvname[j]])); ntmp<-length(tmp)
  xxlvl_[[j+1]]<-tmp;
  tmp<-paste(xvname[j],".",tmp,sep="")
  xxlbl_[[j+1]]<-paste(c("",rep("&nbsp&nbsp",ntmp)),c(xb[j],vlabel[match(tmp,vname)]))
  xxlbl[[j+1]]<-paste(xb[j],"=",vlabelZ[match(tmp,vnameZ)])
}
xxlbl_<-xxlbl_[-1]; xxlvl_<-xxlvl_[-1]; xxlbl<-xxlbl[-1]
avname<-c(avname[alv<=2],avname[alv>2])
alv<-c(alv[alv<=2],alv[alv>2]); alv[alv<=2]<-0;
na<-length(avname); na0<-sum(alv<=2); na1<-sum(alv>2);
ab<-vlabelV[match(avname,vnameV)]
avname_<-avname
avname_[alv>2]<-paste("factor(",avname[alv>2],")",sep="")
aaname_<-list(NA); aalbl_<-list(NA); aalvl_<-list(NA)
for (j in (1:na)) {
  if (alv[j]==0) {
    aaname_[[j+1]]<-avname[j];aalbl_[[j+1]]<-ab[j];aalvl_[[j+1]]<-0
  } else {
    aalvl_[[j+1]]<-levels(factor(WD[,avname[j]]))
    tmp<-paste(avname[j],".",aalvl_[[j+1]],sep="")
    aalbl_[[j+1]]<-c(ab[j],vlabelZ[match(tmp,vnameZ)])
    aalbl_[[j+1]]<-paste(c("",rep("&nbsp&nbsp",length(aalbl_[[j+1]])-1)),aalbl_[[j+1]])
    aaname_[[j+1]]<-c(avname[j],paste("factor(",avname[j],")",aalvl_[[j+1]],sep=""))
  }
}
aaname_<-aaname_[-1]; aalbl_<-aalbl_[-1]; aalvl_<-aalvl_[-1]
ns=0; svb=""; smoothsv<-0; fmladj<-"";
if (length(svname)>0) {
  svb<-vlabelV[match(svname,vnameV)];
  svname_ <- svname 
  smoothsvi<-((sdf=="s" | sdf=="S") & slv==0)
  smoothsv<-sum(smoothsvi)
  svname_[smoothsvi]<-paste("s(",svname[smoothsvi],")",sep="")
  svb1<-svb
  svb1[smoothsvi]<-paste(svb[smoothsvi],"(Smooth)",sep="")
  svname_[slv>0]<-paste("factor(",svname[slv>0],")",sep="")
  fmladj<-paste("+",paste(svname_,collapse="+"))
}
ny<-length(yvname);
yb<-vlabelV[match(yvname,vnameV)]; yb[is.na(yb)]<-yvname[is.na(yb)]
if (is.na(bvar)) {
  blvb<-"Total"; blvb_<-"Total"; nblv<-1;
} else {
  blv<-levels(factor(WD[,bvar])); nblv<-length(blv)+1
  blvb_<-vlabelZ[match(paste(bvar,".",blv,sep=""),vnameZ)]; blvb_[is.na(blvb_)]<-blv[is.na(blvb_)];
  blvb<-c(paste(vlabelV[vnameV==bvar],blvb_,sep="="),"Total");
  blvb_<-c(blvb_,"Total")
  WD<-WD[!is.na(WD[,bvar]),]
}
if (!is.na(vname.start)) {
  fmlt<-paste("Surv(,",vname.start,",",timevar,",",sep="");
  WD<-WD[!is.na(WD[,vname.start]) & !is.na(WD[,vname.stop]),]
} else {
  if (!is.na(timevar)) {
    fmlt<-paste("Surv(",timevar,",",sep="");
    WD<-WD[!is.na(WD[,timevar]),]
  }
}
opt<-ifelse(!is.na(subjvname), "gee", ifelse(smoothsv>0, "gam", "glm"));
colprn<-prn; if (colprn>3) colprn<-1;
if (!is.na(bvar)) {prn<-"S";
} else if (na==1 & alv[1]>2) {prn<-"CX";
} else if (ny>1 & ny<5) {prn<-"Y";
} else if (na>1 & na<5 & sum(alv>2)==0) {prn<-"X";
} else {prn<-"Y";}
clogit<-0; 
if (!is.na(subjvname) & is.na(timevar)) clogit<-1
sink(paste(ofname,".lst",sep=""))
if (prn=="Y") {
  tt<-rep(NA,ny+2); nn<-c("Exposure","Sub-group",yb);
  for (j in 1:na) {
     tt<-rbind(tt,c(paste("X=",ab[j]),rep(" ",ny+1)),c("","N",yb));
     for (k in 1:nx) {
       wdtmp0<-removeNA(NA,j,k,WD)
       colk<-varstats(k,j,wdtmp0);
       nlvl<-length(xxlvl_[[k]]);
       nnk<-cbind(ab[j],xxlbl[[k]])
       for (i in 1:ny) {
          wdtmp0<-removeNA(i,j,k,WD)
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          tmpc<-""; tmpn<-"";
          for (v in 1:nlvl) {
            print(xxlbl[[k]][v]);
            wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
            tmpooi<-mdl2oo(i,j,0);
            tmpc<-c(tmpc,tmpooi[[1]]);
            tmpn<-c(tmpn,nrow(wdtmp));
          }
          colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpn[-1])
       }
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}
if (prn=="X") {
  tt<-rep(NA,na+2); nn<-c("Outcome","Sub-group",ab);
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",na+1)),c("","N",ab));
     for (k in 1:nx) {
       wdtmp0<-removeNA(i,NA,k,WD)
       colk<-varstats(k,NA,wdtmp0);
       nlvl<-length(xxlvl_[[k]])
       nnk<-cbind(yb[i],xxlbl[[k]])
       for (j in 1:na) {
          wdtmp0<-removeNA(i,j,k,WD)
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          tmpc<-""; tmpn<-""
          for (v in 1:nlvl) {
            print(xxlbl[[k]][v]);
            wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
            tmpn<-c(tmpn,nrow(wdtmp))
            tmpooi<-mdl2oo(i,j,0); tmpc<-c(tmpc,tmpooi[[1]]);
          }
          colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpn[-1])
       }
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}
if (prn=="CX") {
  nlv<-length(aalvl_[[1]]);  tt<-rep(NA,nlv+2); nn<-c("Outcome","Sub-group",ab[1])
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",nlv+1)),c("","N",aalbl_[[1]][-1]));
     for (k in 1:nx) {
       wdtmp0<-removeNA(i,1,k,WD)
       colk<-varstats(k,NA,wdtmp0);
       nlvl<-length(xxlvl_[[k]])
       nnk<-cbind(yb[i],xxlbl[[k]])
       fml<-paste(yvname[i],"~",avname_[1],fmladj,sep="")
       tmpc<-rep("",nlv); tmpn<-"";
       for (v in 1:nlvl) {
          print(xxlbl[[k]][v]);
          wdtmp<-wdtmp0[wdtmp0[,xvname[k]]==xxlvl_[[k]][v],]
          tmpn<-c(tmpn,nrow(wdtmp))
          tmpooi<-mdl2oo(i,j,0);
          tmpc<-rbind(tmpc,tmpooi[[1]][-1])
       }
       colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpn[-1])
       tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
     }
  }
}
if (prn=="S") {
  tt<-rep(NA,nblv+2); nn<-c("Outcome","Exposure","Sub-group",blvb)
  for (i in 1:ny) {
     tt<-rbind(tt,c(paste("Y=",yb[i]),rep(" ",nblv+1)));
     for (j in 1:na) {
       tt<-rbind(tt,c(paste("X=",ab[j]),rep(" ",nblv+1)),c("","Total N",blvb));
       for (k in 1:nx) {
          wdtmp0<-removeNA(i,j,k,WD)
          colk<-varstats(k,j,wdtmp0);
          nlvl<-length(xxlvl_[[k]]);
          nnk<-cbind(yb[i],ab[j],xxlbl[[k]])
          fml<-paste(yvname[i],"~",avname_[j],fmladj)
          for (b in 1:nblv) {
            if (b<nblv) {wdtmp1<-wdtmp0[wdtmp0[,bvar]==blv[b],];} else {wdtmp1<-wdtmp0;}
            tmpc<-""; tmpn<-""
            for (v in 1:nlvl) {
              print(paste(blvb[b],xxlbl[[k]][v],sep="; "));
              wdtmp<-wdtmp1[wdtmp1[,xvname[k]]==xxlvl_[[k]][v],]
              tmpn<-c(tmpn,nrow(wdtmp))
              tmpooi<-mdl2oo(i,j,b); tmpc<-c(tmpc,tmpooi[[1]]);
            }
            colk<-cbind(colk,tmpc); nnk<-cbind(nnk,tmpn[-1])
          }
          tt<-rbind(tt,colk); nn<-rbind(nn,nnk)
       }
     }
  }
}
tt<-tt[-1,];
sink()
w<-c(w,"</br><table border=3>", mat2htmltable(tt), "</table>")
 

w<-c(w,"</br>表中数据：HR (95% CI) Pvalue *P<0.05 **P<0.01 ***P<0.001")
w<-c(w,paste(c("</br>结果变量:",paste(yb,collapse="; ")),collapse=" "))
w<-c(w,paste(c("</br>暴露变量:",paste(ab,collapse="; ")),collapse=" "))
if (length(svname)==0) svb1<-"None";
w<-c(w,paste(c("</br>调整变量:",paste(svb1,collapse="; ")),collapse=" "))
if (smoothsv>0) w<-c(w,". Restricted cubic spline smoothing were applied")
if (clogit==0) {
  tmpv<-c(vname.start,timevar); tmpv<-tmpv[!is.na(tmpv)];
  w<-c(w,paste(c("</br>Cox model 时间变量:", tmpv)))
}
if (clogit==1) w<-c(w,paste(c("</br>条件 Logistic 回归, 配对组编号变量:", subjvname)))
w<-c(w,"</br></br>各模型所用的样本量</br><table border=3>", mat2htmltable(nn), "</table>")
w<-c(w,wd.subset)
w<-c(w,paste("</br>此表用易侕统计软件 (www.empowerstats.com) 和R软件生成，生成日期：",Sys.Date()))
w<-c(w,"</body></html>")
fileConn<-file(paste(ofname,".htm",sep="")); writeLines(w, fileConn)