仮置き場

n1<- 20
n2<- 20
t_mean<- 6
t_sd<- 0.8
t_death<- 6
Niter<- 3
p_values<- rep(0,Niter)
G1<- array(0,c(n1,2,Niter))
G2<- array(0,c(n2,2,Niter))
p1<- c(0.5,0.5)
p2<- c(0.5,0.5)

for(i in 1:Niter){
s1<- sample(0:1,size=n1,replace=TRUE)
s2<- sample(0:1,size=n2,replace=TRUE)

g1<- matrix(s1,nr=n1,nc=2)
g2<- matrix(s2,nr=n2,nc=2)

g1<- matrix(0,nr=n1,nc=2)
g2<- matrix(0,nr=n2,nc=2)

g1[,1]<- s1
g2[,1]<- s2

g1_0<- sum(g1[,1]==0) #length(which(g1[,1]==0))
g2_0<- sum(g2[,1]==0) #length(which(g2[,1]==0))

g1_0_t<- rnorm(g1_0,t_mean,t_sd)
g2_0_t<- rnorm(g2_0,t_mean,t_sd)

g1[which(g1[,1]==0),2]<- g1_0_t
g2[which(g2[,1]==0),2]<- g2_0_t


g1_1<- sum(g1[,1]==1) #length(which(g1[,1]==1))
g2_1<- sum(g2[,1]==1) #length(which(g2[,1]==1))

g1_1_t<- runif(g1_1,0,t_death)
g2_1_t<- runif(g2_1,0,t_death)

g1[which(g1[,1]==1),2]<- g1_1_t
g2[which(g2[,1]==1),2]<- g2_1_t


}

par(mfcol=c(1,2))
hist(g1[which(g1[,1]==0),2],xlab="time",main="Alive people observing time")
hist(g1[which(g1[,1]==1),2],xlab="time",main="Dead people dead time")

par(mfcol=c(1,1))
boxplot(g1[which(g1[,1]==1),2],g1[which(g1[,1]==0),2])

g12<- rbind(cbind(g1,rep(1,n1)),cbind(g2,rep(2,n2)))

library(survival)
g12<- as.data.frame(g12)
result<- survfit(Surv(g12[,2],g12[,1])~g12[,3],g12,type="kaplan-meier")
result<-survfit(Surv(Days,event)~group, d, type="kaplan-meier")