logistic_regression
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| logistic_regression [2023/12/14 07:50] – [coefficient (계수) 해석] hkimscil | logistic_regression [2024/12/11 11:57] (current) – [exercise: binary IV] hkimscil | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| https:// | https:// | ||
| data: https:// | data: https:// | ||
| + | |||
| + | [[:Logistic Regression/ | ||
| ====== Data preparation ====== | ====== Data preparation ====== | ||
| * [[https:// | * [[https:// | ||
| Line 73: | Line 75: | ||
| * wald test | * wald test | ||
| < | < | ||
| - | n <- 350 | + | ########## |
| - | p.cancer <- 0.08 | + | # see youtube |
| - | p.mutant | + | # https:// |
| + | n.mut <- 23+117 | ||
| + | n.norm <- 6+210 | ||
| + | p.cancer.mut <- 23/(23+117) | ||
| + | p.cancer.norm | ||
| - | c <- runif(n, 0, 1) | + | set.seed(1011) |
| - | canc <- ifelse(c> | + | c <- runif(n.mut, 0, 1) |
| - | c <- runif(n, 0, 1) | + | # 0 = not cancer, |
| - | gene <- ifelse(c> | + | mutant |
| - | da <- data.frame(gene, | + | c <- runif(n.norm, |
| - | da | + | # 0 = not cancer, 1 = cancer among normal gene |
| - | tab <- table(da) | + | normal <- ifelse(c> |
| + | |||
| + | # 0 = mutant; 1 = normal | ||
| + | gene <- c(rep(0, length(mutant)), | ||
| + | # 0 = not cancer; 1 = cancer | ||
| + | cancer <- c(mutant, normal) | ||
| + | |||
| + | df <- as.data.frame(cbind(gene, | ||
| + | df | ||
| + | df$gene <- factor(df$gene, | ||
| + | df$cancer <- factor(df$cancer, | ||
| + | df | ||
| + | tab <- table(df) | ||
| tab | tab | ||
| + | tab[1,2] | ||
| + | tab[1,1] | ||
| + | # p.c.m = p.cancer.mut the above | ||
| + | p.cancer.mutant <- tab[1, | ||
| + | p.nocancer.mutant <- tab[1, | ||
| + | p.cancer.mutant | ||
| + | 1-p.cancer.mutant | ||
| + | p.nocancer.mutant | ||
| + | |||
| + | p.cancer.norm <- tab[2, | ||
| + | p.nocancer.norm <- 1-p.cancer.norm | ||
| + | p.cancer.norm | ||
| + | p.nocancer.norm | ||
| + | |||
| + | odds(p.cancer.mutant) | ||
| + | odds(p.cancer.norm) | ||
| + | odds.ratio(p.cancer.mutant, | ||
| </ | </ | ||
| + | < | ||
| + | > ########## | ||
| + | > # see youtube | ||
| + | > # https:// | ||
| + | > n.mut <- 23+117 | ||
| + | > n.norm <- 6+210 | ||
| + | > p.cancer.mut <- 23/(23+117) | ||
| + | > p.cancer.norm <- 6/(6+210) | ||
| + | > | ||
| + | > set.seed(1011) | ||
| + | > c <- runif(n.mut, | ||
| + | > # 0 = not cancer, 1 = cancer among mutant gene | ||
| + | > mutant <- ifelse(c> | ||
| + | > | ||
| + | > c <- runif(n.norm, | ||
| + | > # 0 = not cancer, 1 = cancer among normal gene | ||
| + | > normal <- ifelse(c> | ||
| + | > | ||
| + | > # 0 = mutant; 1 = normal | ||
| + | > gene <- c(rep(0, length(mutant)), | ||
| + | > # 0 = not cancer; 1 = cancer | ||
| + | > cancer <- c(mutant, normal) | ||
| + | > | ||
| + | > df <- as.data.frame(cbind(gene, | ||
| + | > df | ||
| + | gene cancer | ||
| + | 1 0 0 | ||
| + | 2 0 1 | ||
| + | 3 0 0 | ||
| + | 4 0 0 | ||
| + | 5 0 0 | ||
| + | 6 0 0 | ||
| + | > | ||
| + | > df$gene <- factor(df$gene, | ||
| + | > df$cancer <- factor(df$cancer, | ||
| + | > df | ||
| + | gene | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | 5 | ||
| + | 6 | ||
| + | > | ||
| + | > tab <- table(df) | ||
| + | > tab | ||
| + | cancer | ||
| + | gene | ||
| + | mutant | ||
| + | norm 210 6 | ||
| + | > tab[1,2] | ||
| + | [1] 19 | ||
| + | > tab[1,1] | ||
| + | [1] 121 | ||
| + | > | ||
| + | > # p.c.m = p.cancer.mut the above | ||
| + | > p.cancer.mutant <- tab[1, | ||
| + | > p.nocancer.mutant <- tab[1, | ||
| + | > p.cancer.mutant | ||
| + | [1] 0.1357143 | ||
| + | > 1-p.cancer.mutant | ||
| + | [1] 0.8642857 | ||
| + | > p.nocancer.mutant | ||
| + | [1] 0.8642857 | ||
| + | > | ||
| + | > p.cancer.norm <- tab[2, | ||
| + | > p.nocancer.norm <- 1-p.cancer.norm | ||
| + | > p.cancer.norm | ||
| + | [1] 0.02777778 | ||
| + | > p.nocancer.norm | ||
| + | [1] 0.9722222 | ||
| + | > | ||
| + | > odds(p.cancer.mutant) | ||
| + | [1] 0.1570248 | ||
| + | > odds(p.cancer.norm) | ||
| + | [1] 0.02857143 | ||
| + | > odds.ratio(p.cancer.mutant, | ||
| + | [1] 5.495868 | ||
| + | > | ||
| + | </ | ||
| ====== Logit 성질 ====== | ====== Logit 성질 ====== | ||
| 여기서 | 여기서 | ||
| \begin{align*} | \begin{align*} | ||
| - | y & = ln(x) \\ | + | ln(x) & = y |
| - | & = log_e {x} \\ | + | log_e {x} & = y |
| x & = e^{y} \\ | x & = e^{y} \\ | ||
| \end{align*} | \end{align*} | ||
| Line 196: | Line 311: | ||
| > | > | ||
| </ | </ | ||
| - | ====== Odds ratio in logistic ====== | ||
| - | \begin{align*} | ||
| - | ln(\frac{p}{1-p}) = & y \\ | ||
| - | \frac {p}{1-p} = & e^{y} \;\;\; \text{where } \;\; y = a + bX \\ | ||
| - | \text {odds} = & e^{y} = e^{a + bX} \\ | ||
| - | \text{then} \;\;\; \text{odds ratio} (y_{2}/ | ||
| - | & \text{odds of y at one point, } y_1 \text { and } \\ | ||
| - | & \text{odds of y at another point, } y_2 \\ | ||
| - | \text{and | ||
| - | y_2 = & a + b (X+1) \\ | ||
| - | \text{then | ||
| - | \text {odds of } y_1 = & e^{(a+b(X))} \\ | ||
| - | \text {odds of } y_2 = & e^{(a+b(X+1))} \\ | ||
| - | \text {odds ratio for } y_1 = & \frac {e^{(a+bX+b)} } {e^{(a+bX)}} \\ | ||
| - | = & \frac {e^{(a+bX)} * e^{b}} {e^{(a+bX)} } \\ | ||
| - | = & e^b | ||
| - | \end{align*} | ||
| - | * 위의 $e^b$ 가 의미하는 것은 $X$가 한 유닛만큼 증가하면 $Y$는 $b$만큼 증가하는 것이 되는데 이 $b$는 | ||
| - | * $y2$와 $y1$ 간의 $\text{log of odds ratio}$ 로 이해되어야 한다. 따라서 | ||
| - | * y2와 y1 간의 $\text{odds ratio} = e^b $ 이 된다. | ||
| ====== Logitistic Regression Analysis ====== | ====== Logitistic Regression Analysis ====== | ||
| Line 310: | Line 405: | ||
| </ | </ | ||
| + | |||
| + | ===== Odds ratio in logistic ===== | ||
| + | \begin{align*} | ||
| + | ln(\frac{p}{1-p}) = & y \\ | ||
| + | \frac {p}{1-p} = & e^{y} \;\;\; \text{where } \;\; y = a + bX \\ | ||
| + | \text {odds} = & e^{y} = e^{a + bX} \\ | ||
| + | \text{then} \;\;\; \text{odds ratio} (y_{2}/ | ||
| + | & \text{odds of y at one point, } y_1 \text { and } \\ | ||
| + | & \text{odds of y at another point, } y_2 \\ | ||
| + | \text{and | ||
| + | y_2 = & a + b (X+1) \\ | ||
| + | \text{then | ||
| + | \text {odds of } y_1 = & e^{(a+b(X))} \\ | ||
| + | \text {odds of } y_2 = & e^{(a+b(X+1))} \\ | ||
| + | \text {odds ratio for } y_1 = & \frac {e^{(a+bX+b)} } {e^{(a+bX)}} \\ | ||
| + | = & \frac {e^{(a+bX)} * e^{b}} {e^{(a+bX)} } \\ | ||
| + | = & e^b | ||
| + | \end{align*} | ||
| + | * 위의 $e^b$ 가 의미하는 것은 $X$가 한 유닛만큼 증가하면 $Y$는 $b$만큼 증가하는 것이 되는데 이 $b$는 | ||
| + | * $y2$와 $y1$ 간의 $\text{log of odds ratio}$ 로 이해되어야 한다. 따라서 | ||
| + | * y2와 y1 간의 $\text{odds ratio} = e^b $ 이 된다. | ||
| ===== coefficient (계수) 해석 ===== | ===== coefficient (계수) 해석 ===== | ||
| Line 318: | Line 434: | ||
| * 따라서 $a + b = -0.13504 + 0.36784 = 0.2328 $ | * 따라서 $a + b = -0.13504 + 0.36784 = 0.2328 $ | ||
| * 즉, $ln(odds) = 0.2328 $ 이고 | * 즉, $ln(odds) = 0.2328 $ 이고 | ||
| - | * $ odds = e^{0.2328} = 1.262129$ | + | * $ odds = \displaystyle \frac {p_{\text{ of male yes}}}{p-1} |
| * $ p = e^{0.2328} / (1 + e^{0.2328}) = 0.5579386 $ 그리고 X는 1일 경우의 prob = 0.558 정도이다. | * $ p = e^{0.2328} / (1 + e^{0.2328}) = 0.5579386 $ 그리고 X는 1일 경우의 prob = 0.558 정도이다. | ||
| * or '' | * or '' | ||
| Line 330: | Line 446: | ||
| * 즉, $log(om/of) = b$ | * 즉, $log(om/of) = b$ | ||
| * $log(1.444613) = b$ | * $log(1.444613) = b$ | ||
| + | * $ 1.444613 = e^b$ | ||
| < | < | ||
| > log(1.444613) | > log(1.444613) | ||
| Line 440: | Line 557: | ||
| </ | </ | ||
| 마리화나의 사용경험에서 남성이 여성보다 큰 승산이 있다고 판단되었다 (Odds ratio (OR) = 1.44; 95% CI = 1.13, 1.86; p = .004). 남성은 여성보다 약 44% 더 사용경험을 할 승산을 보였다 (OR = 1.44). | 마리화나의 사용경험에서 남성이 여성보다 큰 승산이 있다고 판단되었다 (Odds ratio (OR) = 1.44; 95% CI = 1.13, 1.86; p = .004). 남성은 여성보다 약 44% 더 사용경험을 할 승산을 보였다 (OR = 1.44). | ||
| + | ====== exercise: binary IV ====== | ||
| + | < | ||
| + | ######################################## | ||
| + | # exercise | ||
| + | |||
| + | head(df) | ||
| + | table(df) | ||
| + | # base 바꾸기 | ||
| + | df.norm <- df %>% mutate(gene = relevel(gene, | ||
| + | df.mut <- df %>% mutate(gene = relevel(gene, | ||
| + | |||
| + | |||
| + | logm.cancer.gene.1 <- glm(cancer ~ gene, family = binomial, data = df.norm) | ||
| + | summary(logm.cancer.gene.1) | ||
| + | a <- logm.cancer.gene.1$coefficients[1] | ||
| + | b <- logm.cancer.gene.1$coefficients[2] | ||
| + | a | ||
| + | b | ||
| + | a+b | ||
| + | # when b = 0; 즉, mutant = 0 일 때 | ||
| + | # log(odds.norm) = a 이므로 | ||
| + | # odds.norm = e^a | ||
| + | exp(a) | ||
| + | # 확인 | ||
| + | odds(p.can.norm) | ||
| + | # odds.mut = e^(a+b) | ||
| + | exp(a+b) | ||
| + | odds(p.can.mut) | ||
| + | # odds.ratio = e^(b) | ||
| + | exp(b) | ||
| + | odds.ratio(p.can.mut, | ||
| + | |||
| + | |||
| + | logm.cancer.gene.2 <- glm(cancer ~ gene, family = binomial, data = df.mut) | ||
| + | summary(logm.cancer.gene.2) | ||
| + | a <- logm.cancer.gene.2$coefficients[1] | ||
| + | b <- logm.cancer.gene.2$coefficients[2] | ||
| + | a | ||
| + | b | ||
| + | a+b | ||
| + | # when b = 0; 즉, mutant = 0 일 때 | ||
| + | # log(odds.norm) = a 이므로 | ||
| + | # odds.norm = e^a | ||
| + | exp(a) | ||
| + | # 확인 | ||
| + | odds(p.can.mut) | ||
| + | # odds.mut = e^(a+b) | ||
| + | exp(a+b) | ||
| + | odds(p.can.norm) | ||
| + | # odds.ratio = e^(b) | ||
| + | exp(b) | ||
| + | odds.ratio(p.can.norm, | ||
| + | |||
| + | |||
| + | </ | ||
| ====== X: numeric variable ====== | ====== X: numeric variable ====== | ||
| < | < | ||
logistic_regression.1702507807.txt.gz · Last modified: by hkimscil
