検査の感度・特異度・PPV・NPV〜あいまいな「確率情報」を活用して判断するための準備3

  • この記事の構成
  • このような大規模集計を行っていると、検査の感度・特異度・PPV・NPVなどはリアルタイムで更新して利用することが可能となる
  • 検査の感度・特異度は、ある検査の結果を集めて、それについて、診断名ごとに陽性・陰性の2x2表を作って算出する(着目診断名以外の診断名がついたケースは「コントロール」として扱っている)
  • 検査のPPV・NPVは、対象集団を定めて、それについて、診断名ごとに陽性・陰性の2x2表を作って算出する
#Number of records
Nr<-10000

#Number of Questions
Nq<-10

#Number of categories per Questions
Cs<-rep(2,Nq)

Cs[1]<-5

#Fianl Diagnosis
#Number of types of Dx
Nd<-3


#Data
Q<-matrix(0,Nr,Nq)
for(i in 1:Nq){
	Q[,i]<-sample(1:Cs[i],Nr,replace=TRUE)
}

D<-sample(1:Nd,Nr,replace=TRUE)
#Q[,1]<-D
# 尤度を考える

for(i in 1:Nq){
	print(paste("Q",i,"の分割表"))
	tmpt<-as.matrix(table(Q[,i],D))
	print(tmpt)
	print(paste("診断名ごとの質問項目ファクターの確率"))
	tmpt2<-tmpt/apply(tmpt,2,sum)
	print(tmpt2)
	print(paste("質問項目ファクターごとの診断名の尤度"))
	tmpt3<-tmpt2/apply(tmpt2,1,sum)
	print(tmpt3)
}

# 検査の感度・特異度のこと
for(i in 2:Nq){
	print(paste("Q",i,"の分割表"))
	tmpt<-as.matrix(table(Q[,i],D))
	print(tmpt)
	print(paste("診断名ごとの質問項目感度特異度を出すための2x2表"))
	for(j in 1:Nd){
		print(paste("診断名",j))
		t2x2<-cbind(tmpt[,j],apply(tmpt[,-j],1,sum))
		print(t2x2)
		print(paste("感度,特異度"))
		print(t2x2[1,1]/sum(t2x2[,1]))
		print(t2x2[2,2]/sum(t2x2[,2]))
	}
}

# 主訴ごとに検査を使い分けるとする
# 主訴ごとに受診科が異なる、と考えてもよい
for(i in 2:Nq){
	print(paste("主訴ごとにまとめなおして考える"))
	for(j in 1:Cs[1]){
		print(paste("検査",i))
		print(paste("主訴",j))
		print(paste("主訴",j,"検査",i,"の分割表"))
		syusos<-which(Q[,1]==j)
		tmpt<-as.matrix(table(Q[syusos,i],D[syusos]))
		print(tmpt)
		for(k in 1:Nd){
			print(paste("主訴",j,"診断名",k,"検査",i))
			t2x2<-cbind(tmpt[,k],apply(tmpt[,-k],1,sum))
			print(t2x2)
			print(paste("PPV,NPV"))
			print(t2x2[1,1]/sum(t2x2[1,]))
			print(t2x2[2,2]/sum(t2x2[2,]))
		}

	}
}