흡연과 폐암에 대한 베이즈 정리 문제 with R

Matt Lee·2020년 7월 21일
1

기초 확률론

목록 보기
13/26
post-thumbnail

이번 포스팅에서는 흡연과 폐암에 대한 간단한 베이즈 정리 문제를 R을 활용 해서 풀어 보겠습니다.

문제

  • Suppose 0.1% of the American population currently has lung cancer, that 90% of all lung cancer cases are smokers, and that 21% of those without lung cancer also smoke. (These values are fairly close to the values given on the American Lung Association web site as of 2011.) Consider the following questions.

  • What percent of smokers have lung cancer?

  • What percent of non-smokers have lung cancer?

  • How much more likely is a smoker to have lung cancer than a non-smoker?

문제 풀이

1. 문제에서 주어진 정보를 가지고 확률을 R 코딩을 통해 정의 해 보겠습니다.

Suppose 0.1%0.1\% of the American population currently has lung cancer

  • 위의 정보를 바탕으로 폐암에 걸린 미국인의 비율이 0.1%0.1\% 임을 알 수 있습니다. 여기서 LC는 Lung Cancer를 의미 하며 확률식으로는 P(LC)P(\text{LC}) 입니다.
> LC <- (0.1/100)
> LC
[1] 0.001

다음은 LC의 확률을 가지고 LC의 여사건의 확률을 정의 합니다.

  • 여기서 notLC는 Lung Cancer에 걸리지 않았다는 의미 이며 확률식은 다음과 같습니다.

    P(notLC)P(\text{notLC}) 또는 P(LC)P(\text{LC}^{'})

  • R 코드는 다음과 같습니다.

> notLC <- 1 - LC
> notLC
[1] 0.999

90%90\% of all lung cancer cases are smokers

  • 위의 정보는 폐암 환자 중 흡연자의 비율이 90%90\% 임을 알려 줍니다. 바꿔 말하면 폐암이 주어 졌을 때 흡연자의 비율입니다. 즉, 이 것은 조건부 확률입니다. 여기서 Smokers_Given_LC에 대한 확률식은 다음과 같습니다.

    P(SmokersLC)P(\text{Smokers}|\text{LC})

  • R 코드는 다음과 같습니다.

> Smokers_Given_LC <- 90/100
> Smokers_Given_LC
[1] 0.9

다음은 Smokers_Given_LC의 확률을 가지고 Smokers_Given_LC의 여사건의 확률을 정의 합니다. 여기서 Nonsmokers_Given_LC에 대한 확률식은 다음과 같습니다.

P(NonsmokersLC)P(\text{Nonsmokers}|\text{LC}) 또는 P(SmokersLC)P(\text{Smokers}^{'}|\text{LC})

  • R 코드는 다음과 같습니다.
> Nonsmokers_Given_LC <- 1 - Smokers_Given_LC
> Nonsmokers_Given_LC
[1] 0.1

21%21\% of those without lung cancer also smoke

  • 위의 정보는 폐암 환자가 아닌 흡연자의 비율이 21%21\%임을 의미 합니다. 바꿔 말하면 폐암이 아니다 라는 정보가 주어 졌을 때의 흡연자의 비율입니다. 즉, 이 것은 조건부 확률입니다. 여기서 Smokers_Given_notLC에 대한 확률식은 다음과 같습니다.

    P(SmokersnotLC)P(\text{Smokers}|\text{notLC}) 또는 P(SmokersLC)P(\text{Smokers}|\text{LC}^{'})

  • R 코드는 다음과 같습니다.

> Smokers_Given_notLC <- 21/100
> Smokers_Given_notLC
[1] 0.21

다음은 Smokers_Given_notLC의 확률을 가지고 Smokers_Given_notLC의 여사건의 확률을 정의 합니다.

  • 여기서 Nonsmokers_Given_notLC의 확률식은 다음과 같습니다.

    P(NonsmokersnotLC)P(\text{Nonsmokers}|\text{notLC}) 또는 P(SmokersLC)P(\text{Smokers}^{'}|\text{LC}^{'})

  • R 코드는 다음과 같습니다.

> Nonsmokers_Given_notLC <- 1 - Smokers_Given_notLC
> Nonsmokers_Given_notLC
[1] 0.79

2. 1번에서 정의된 사건들의 확률을 바탕으로 전체 확률 법칙 정의

Total Probability for Smokers

  • 흡연자에 대한 전체 확률 법칙은 다음과 같습니다.

    P(LC)×P(SmokersLC)+P(notLC)×P(SmokersnotLC)P(\text{LC}) \times P(\text{Smokers}|\text{LC}) + P(\text{notLC}) \times P(\text{Smokers}|\text{notLC})

  • R 코드는 다음과 같습니다.

> Smokers <- (LC * Smokers_Given_LC) + (notLC * Smokers_Given_notLC)
> Smokers
[1] 0.21069

Total Probability for Non-smokers

  • 비흡연자에 대한 전체 확률 법칙은 다음과 같습니다.

    P(LC)×P(NonsmokersLC)+P(notLC)×P(NonmokersnotLC)P(\text{LC}) \times P(\text{Nonsmokers}|\text{LC}) + P(\text{notLC}) \times P(\text{Nonmokers}|\text{notLC})

  • R 코드는 다음과 같습니다.

> Nonsmokers <- (LC * Nonsmokers_Given_LC) + (notLC * Nonsmokers_Given_notLC)
> Nonsmokers
[1] 0.78931

3. 1번과 2번 정보를 바탕으로 베이즈 정리를 활용한 확률 계산

베이즈 정리를 사용하기 위한 모든 확률 정보가 생성 되었습니다. 이 정보를 바탕으로 베이즈 정리를 사용해서 우리가 원하는 목표 확률을 계산 하겠습니다.

What percent of smokers have lung cancer? Using Bayes’ Theorem
  • 흡연자 중에 폐암에 걸린 사람은 몇 퍼센트 입니까? 이 문제에 대한 답변은 P(LC|Smokers)를 이용해서 계산 할 수 있습니다. P(LC|Smokers)에 대한 베이즈 정리 확률식은 다음과 같습니다.

    P(LCSmokers)=P(LC)×P(SmokersLC)P(Smokers)=P(LC)×P(SmokersLC)P(LC)×P(SmokersLC)+P(notLC)×P(SmokersnotLC)\begin{aligned} P(\text{LC}|\text{Smokers}) &= \frac{P(\text{LC}) \times P(\text{Smokers}|\text{LC})}{P(\text{Smokers})} \\ &= \frac{P(\text{LC}) \times P(\text{Smokers}|\text{LC})}{P(\text{LC}) \times P(\text{Smokers}|\text{LC}) + P(\text{notLC}) \times P(\text{Smokers}|\text{notLC})} \end{aligned}
  • R 코드는 다음과 같습니다.

> LC_Given_Smokers <- ((LC * Smokers_Given_LC) / Smokers)*100
> LC_Given_Smokers
[1] 0.4271679
  • 위의 R 계산 결과를 보면 흡연자 중에서 폐암에 걸린 사람은 약 0.43%0.43\% 임을 알 수 있습니다.
What percent of non-smokers have lung cancer? Using Bayes’ Theorem
  • 비흡연자 중에 폐암에 걸린 사람은 몇 퍼센트 입니까? 이 문제에 대한 답변은 P(LC|Non-smokers)를 이용해서 계산 할 수 있습니다. P(LC|Non-smokers)에 대한 베이즈 정리 확률식은 다음과 같습니다.

    P(LCNon-smokers)=P(LC)×P(NonsmokersLC)P(Nonsmokers)=P(LC)×P(NonsmokersLC)P(LC)×P(NonsmokersLC)+P(notLC)×P(NonmokersnotLC)\begin{aligned} P(\text{LC}|\text{Non-smokers}) &= \frac{P(\text{LC}) \times P(\text{Nonsmokers}|\text{LC})}{P(\text{Nonsmokers})} \\ &= \frac{P(\text{LC}) \times P(\text{Nonsmokers}|\text{LC})}{P(\text{LC}) \times P(\text{Nonsmokers}|\text{LC}) + P(\text{notLC}) \times P(\text{Nonmokers}|\text{notLC})} \end{aligned}
  • R 코드는 다음과 같습니다.

> LC_Given_Nonsmokers <- ((LC * Nonsmokers_Given_LC) / Nonsmokers)*100
> LC_Given_Nonsmokers
[1] 0.01266929
  • 위의 R 계산 결과를 보면 비흡연자 중에서 폐암에 걸린 사람은 약 0.01%0.01\%임을 알 수 있습니다.
How much more likely is a smoker to have lung cancer than a non-smoker?
  • 흡연자가 비흡연자에 비해 폐암에 걸릴 확률은 얼마나 됩니까? 이 문제에 대한 답변은 간단하게 위에서 구한 흡연자가 폐암에 걸릴 확률을 비흡연자가 폐암에 걸릴 확률로 나눠서 계산 할 수 있습니다.
  • R 코드는 다음과 같습니다.
> LC_Given_Smokers / LC_Given_Nonsmokers
[1] 33.71679
  • 위의 R 계산 결과를 보면 흡연자가 비흡연자에 약 34배 폐암에 걸릴 확률이 높다는 것을 알 수 있습니다.
profile
미국에 서식 중인 응용 수학과 대학원생, 아직은 잉여지만 그래도 행복 :)

5개의 댓글

comment-user-thumbnail
2020년 7월 21일

오타를 하나 찾은 것 같아서요, '위의 정보를 바탕으로 폐암에 걸리지 않은 미국인의 비율이 0.1% 임을 알 수 있습니다.' 이 문장에서 '걸리지 않은' 이 아니고, '걸린' 이 맞는 것 같아서요.

1개의 답글
comment-user-thumbnail
2020년 7월 22일

LC_Given_Smokers <- ((LC * Smokers_Given_LC) / Smokers)*100
LC_Given_Smokers
[1] 0.4271679
LC_Given_Smokers * 100
[1] 42.71679

에서 이미 100을 곱했는데 왜 또 100을 곱하나요?

1개의 답글
comment-user-thumbnail
2022년 7월 12일

아무리 쳐다봐도 시원하게 이해가 되지 않아서 막고 품어보았습니다...
확률계산만으로 이해가 되시는 분들이 정말 부럽습니다.

  • 10만명 기준
    Smk nSmk
    LC 90 10
    nLC 20,979 78,921

90/(90+29079) --> 0.43%
10/(10+78921) --> 0.013%
0.43/0.013 --> 34배

답글 달기

관련 채용 정보