⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logrank.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
字号:
*! version 7.0.4  04oct2004
program define logrank /* timevar [deadvar] [, by(group) t0(t0) id(tvid)] */, rclass
	version 6.0, missing
	syntax varlist(min=1 max=2) [if] [in] [fw iw] [, /*
		*/ BY(varlist) CHECK Detail ID(varname) LOGRANK /*
		*/ MAT(string) T0(varname) noTItle /*
		*/ STrata(varlist) TVid(varname) trend]
	tokenize `varlist'

	if `"`by'"'==`""' { 
		di in red `"by() required"'
		exit 198 
	}

	if `"`tvid'"' != `""' {
		local id `"`tvid'"'
		local tvid
	}


	if `"`strata'"' != `""' {
		if `"`detail'"' != `""' {
			if `"`check'"' != `""' {
				tempname v V vi Vi
				local matopt `"mat(`vi' `Vi')"'
			}
			tempvar touse  sv j
			mark `touse' `if' `in' [`weight'`exp']
			markout `touse' `varlist' `t0'
			markout `touse' `by' `strata', strok
			sort `touse' `strata'
			qui {
				by `touse' `strata': /* 
				*/ gen `sv'=cond(_n==1 & `touse',1,0)
				replace `sv' = sum(`sv')
				qui gen long `j' = _n
				compress `j' `sv'
			}
			if `"`t0'"' != `""' {
				local t0opt `"t0(`t0')"'
			}
			if `"`id'"' != `""' {
				local idopt `"id(`id')"'
			}
			local nsv = `sv'[_N]
			Title `"`title'"' `"`strata'"'
			local i 1 
			while `i' <= `nsv' {
				qui sum `j' if `sv'==`i'
				local x = `strata'[_result(5)]
				di _n in gr `"-> `strata' = `x'"'
				logrank `varlist' [`weight'`exp'] /* 
				*/ if `touse' & `sv'==`i' /* 
				*/ , by(`by') `t0opt' `idopt' `matopt' notitle
				if `"`check'"' != `""' {
					if `i'==1 {
						mat `v' = `vi'
						mat `V' = `Vi'
					}
					else {
						mat `v' = `v' + `vi'
						mat `V' = `V' + `Vi'
					}
				}
				local i = `i' + 1
			}
			di 
			di in gr `"-> Total"'
			logrank `varlist' [`weight'`exp'] if `touse' /*
			*/ , by(`by') `t0opt' `idopt' strata(`strata') /*
			*/ `trend' notitle
			if `"`check'"' != `""' {
				mat `V' = syminv(`V')
				mat `V' = `v'*`V'
				mat `V' = `V'*`v' '
				di `"CHECK:  "' `V'[1,1]
				global S_7 = `V'[1,1]
			}
			exit
		}
	}

	local t1 `"`1'"'
	if `"`2'"'==`""' {
		tempvar dead
		qui gen byte `dead' = 1
	}
	else	local dead `"`2'"'

	tempvar touse
	mark `touse' `if' `in' [`weight'`exp']
	markout `touse' `t1' `dead'
	markout `touse' `by' `strata', strok

	if `"`t0'"'!=`""' & `"`id'"'!=`""' {
		local id
	}
	if `"`t0'"'==`""' & `"`id'"'==`""' {
		tempvar t0
		qui gen byte `t0' = 0
	}
	else if `"`t0'"' != `""' { 
		markout `touse' `t0'
	}
	else if `"`id'"'!=`""' {
		markout `touse' `id'
		quietly {
			sort `touse' `id' `t1'
			local ty : type `t1'
			by `touse' `id': gen `ty' `t0' = /*
				*/ cond(_n==1,0,`t1'[_n-1])
		}
		capture assert `t1'>`t0'
		if _rc {
			di in red `"repeated records at same `t1' within `id'"'
			exit 498
		}
	}

	capture assert `t1'>0 if `touse'
	if _rc { 
		di in red `"survival time `t1' <= 0"'
		exit 498
	}
	capture assert `t0'>=0 if `touse'
	if _rc { 
		di in red `"entry time `t0' < 0"'
		exit 498
	}
	capture assert `t1'>`t0' if `touse'
	if _rc {
		di in red `"entry time `t0' >= survival time `t1'"'
		exit 498
	}
	capture assert `dead'==0 if `touse'
	if _rc==0 { 
		di in red `"no test possible because there are no failures"'
		exit 2000
	}

	preserve 

	if `"`weight'"' != `""' { 
		tempvar w 
		quietly gen double `w' `exp' if `touse'
		local wv `"`w'"'
	}
	else	local w 1

	tempfile lister
	tempvar op g n d
	quietly { 
		keep if `touse'
		keep `wv' `t0' `t1' `by' `strata' `dead'

		sort `by'
		by `by': gen long `g' = 1 if _n==1
		replace `g' = sum(`g') 
		local ng = `g'[_N]
		save `"`lister'"'

		local N = _N
		expand 2 
		gen byte `op' = 3/*add*/ in 1/`N'
		replace `t1' = `t0' in 1/`N'
		drop `t0'
		local N = `N' + 1
		replace `op' = cond(`dead'==0,2/*cens*/,1/*death*/) in `N'/l

		if `"`strata'"'!=`""' {
			local bystr `"by `strata':"'
		}

		sort `strata' `t1' `op' `by'

		`bystr' gen double `n' = sum(cond(`op'==3,`w',-`w'))
		by `strata' `t1': gen `d' = sum(`w'*(`op'==1))

		local i 1
		while `i' <= `ng' { 
			tempvar ni di
			`bystr' gen double `ni' = /*
			*/ sum(cond(`g'==`i', cond(`op'==3,`w',-`w'), 0))
			by `strata' `t1': gen double `di' = /* 
			*/ sum(cond(`g'==`i', `w'*(`op'==1), 0))
			local nlist `"`nlist' `ni'"'
			local dlist `"`dlist' `di'"'
			local i = `i' + 1 
		}
		by `strata' `t1': keep if _n==_N
		tempvar newn
		`bystr' gen double `newn' = `n'[_n-1]
		drop `n' 
		rename `newn' `n'

		local i 1
		while `i' <= `ng' {
			local ni : word `i' of `nlist'
			`bystr' gen double `newn' = `ni'[_n-1] if _n>1
			drop `ni' 
			rename `newn' `ni'
			local i = `i' + 1
		}
		drop if `d'==0

		tempvar wi
		tempname w wo v
		mat `w' = J(1,`ng',0)
		mat `wo' = `w'
		local i 1
		while `i' <= `ng' {
			local ni : word `i' of `nlist'
			local di : word `i' of `dlist'
			gen double `wi' = sum(`ni'*`d'/`n')
			mat `w'[1,`i'] = `wi'[_N]
			drop `wi'
			summ `di'
			mat `wo'[1,`i'] = _result(18)
			local i = `i' + 1
		}
	}

	tempname V
	mat `V' = J(`ng',`ng',0)
	local i 1
	tempvar cons
	qui gen double `cons' = `d'*(`n'-`d')/(`n'*`n'*(`n'-1))
	
	while `i' <= `ng' {
		local ni : word `i' of `nlist'
		local di : word `i' of `dlist'
		gen double `wi' = sum( `ni'*(`n'-`ni')*`cons' )
		mat `V'[`i',`i'] = `V'[`i',`i'] + `wi'[_N]
		drop `wi'
		local j 1
		while `j' < `i' { 
			local nj : word `j' of `nlist'
			local dj : word `j' of `dlist'
			gen double `wi' = sum( -`ni'*`nj'*`cons')
			mat `V'[`i',`j'] = `V'[`i',`j'] + `wi'[_N]
			mat `V'[`j',`i'] = `V'[`i',`j']
			drop `wi'
			local j = `j' + 1
		}
		local i = `i' + 1
	}



	tempname v
	mat `v' = `w' - `wo'
	if `"`mat'"' != `""' {
		parse `"`mat'"', parse(`" "')
		mat `1' = `v'
		mat `2' = `V'
	}

	tempname mV mv
	mat `mV' = `V'
	mat `mv' = `v'
	/* `mV' is the covariate matrix */
	/* `mv' is Z matrix */

	mat `V' = syminv(`V')

	tempname wt
	mat `V' = `v' * `V'
	mat `V' = `V' * `v' '

	quietly { 
		use `"`lister'"', clear 
		by `by': keep if _n==1
		keep `g' `by'
		sort `g'
		tempvar grp X
		gen str50 `grp' = `""' 
		local second 0
		parse `"`by'"', parse(`" "')
		while `"`1'"' != `""' {
			if `second' { 
				local ttlsub = abbrev("`1'",8)
				local ttl `"`ttl', `ttlsub'"'
				local ttlsub
				replace `grp' = `grp' + `", "'
			}
			else {
				if `"`2'"'=="" {
					local ttl = abbrev(`"`1'"', 12)
				}
				else	local ttl = abbrev(`"`1'"', 8)
				local second 1
			}
			local ty : type `1'
			if substr(`"`ty'"',1,3)==`"str"' { 
				replace `grp' = `grp' + /*
				*/ trim(substr(trim(`1'),1,30))
			}
			else {
				local vlab : value label `1'
				if `"`vlab'"' != `""' { 
					decode `1', gen(`X') maxlen(30)
					replace `grp' = `grp' + trim(`X')
					drop `X'
				}
				else	replace `grp' = `grp'+trim(string(`1'))
			}
			mac shift
		}
		compress `grp'
	}
	local len1 = length(`"`ttl'"')
	local ty : type `grp'
	local len2 = substr(`"`ty'"',4,.)
	local len = max(`len1', `len2', 5) + 1

	Title `"`title'"' `"`strata'"'

        di in smcl in gr _n _col(`len') `" {c |}   Events         Events"'
	local pad = `len' - `len1'
	if `"`strata'"'==`""' { local dup `"   expected"' }
	else	local dup `"expected(*)"'
	di in smcl in gr `"`ttl'"' _skip(`pad') `"{c |}  observed    `dup'"'
	di in smcl in gr "{hline `len'}{c +}{hline 25}" 

	local sum 0
	local i 1 
	while `i' <= _N { 
		local x = `grp'[`i']
		local pad = `len' - length(`"`x'"')
		di in smcl in gr `"`x'"' _skip(`pad') "{c |}" in ye /* 
			*/ %10.0g `wo'[1,`i'] `"     "' %10.2f `w'[1,`i']
		local sum = `sum' + `wo'[1,`i']
		local i = `i' + 1
	}
	di in smcl in gr "{hline `len'}{c +}{hline 25}" 
        local pad = `len' - 5
	di in smcl in gr `"Total"' _skip(`pad') `"{c |}"' in ye /* 
			*/ %10.0g `sum' `"     "' %10.2f `sum'

	if `"`strata'"' != `""' {
		di in gr _n `"(*) sum over calculations within `strata'"'	
	}

	local pad = `len' + 7
	ret scalar df = colsof(`w') - 1
	ret scalar chi2 = `V'[1,1]

	/* double save */
	global S_1 `"`by'"'
	global S_5 = colsof(`w') - 1
	global S_6 = `V'[1,1]
	/* double save ends */
	local pad1 = `pad' - ($S_5>=10)
	di _n in gr _col(`pad1') `"chi2($S_5) = "' in ye %10.2f `V'[1,1] 
	di in gr _col(`pad') `"Pr>chi2 = "' in ye %10.4f chiprob($S_5, `V'[1,1])
	if "`trend'"~="" {
		if _N<=2 {
			di in red "trend test requires 3 or more groups"
			exit 198
		}
		_sttrend `mV' `mv' `by' `pad1'  `pad'
		ret add
	}
end

program define Title /* <title mark> <strata> */
	local title `"`1'"'
	local strata `"`2'"'

	if `"`title'"' == `""' {
		if `"`strata'"'==`""' {
			di _n(2) in smcl in gr /*
*/ `"{title:Log-rank test for equality of survivor functions}"'
		}
		else	di _n(2) in smcl in gr /*
*/ `"{title:Stratified log-rank test for equality of survivor functions}"'
	}
end
exit

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -