User Tools

Site Tools


logistic_regression

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
logistic_regression [2024/12/10 13:30] – [Logit 성질] hkimscillogistic_regression [2024/12/11 11:57] (current) – [exercise: binary IV] hkimscil
Line 156: Line 156:
 5      0      0 5      0      0
 6      0      0 6      0      0
-7      0      0 +>
-8      0      1 +
-9      0      0 +
-10          0 +
-11          0 +
-12          0 +
-13          0 +
-14          0 +
-15          0 +
-16          0 +
-17          0 +
-18          0 +
-19          0 +
-20          0 +
-21          0 +
-22          0 +
-23          0 +
-24          0 +
-25          0 +
-26          0 +
-27          0 +
-28          0 +
-29          1 +
-30          1 +
-31          0 +
-32          0 +
-33          0 +
-34          0 +
-35          0 +
-36          0 +
-37          1 +
-38          0 +
-39          0 +
-40          0 +
-41          0 +
-42          0 +
-43          0 +
-44          0 +
-45          0 +
-46          1 +
-47          0 +
-48          0 +
-49          0 +
-50          0 +
-51          1 +
-52          0 +
-53          0 +
-54          0 +
-55          0 +
-56          0 +
-57          0 +
-58          0 +
-59          1 +
-60          1 +
-61          0 +
-62          0 +
-63          0 +
-64          0 +
-65          0 +
-66          0 +
-67          0 +
-68          0 +
-69          0 +
-70          0 +
-71          0 +
-72          0 +
-73          0 +
-74          0 +
-75          0 +
-76          0 +
-77          0 +
-78          0 +
-79          0 +
-80          0 +
-81          0 +
-82          0 +
-83          0 +
-84          0 +
-85          0 +
-86          0 +
-87          0 +
-88          1 +
-89          0 +
-90          0 +
-91          1 +
-92          1 +
-93          0 +
-94          0 +
-95          0 +
-96          0 +
-97          0 +
-98          0 +
-99          0 +
-100    0      0 +
-101    0      1 +
-102    0      0 +
-103    0      0 +
-104    0      0 +
-105    0      0 +
-106    0      0 +
-107    0      1 +
-108    0      0 +
-109    0      0 +
-110    0      0 +
-111    0      0 +
-112    0      0 +
-113    0      0 +
-114    0      1 +
-115    0      1 +
-116    0      0 +
-117    0      0 +
-118    0      0 +
-119    0      0 +
-120    0      0 +
-121    0      0 +
-122    0      0 +
-123    0      0 +
-124    0      0 +
-125    0      0 +
-126    0      0 +
-127    0      0 +
-128    0      0 +
-129    0      0 +
-130    0      0 +
-131    0      1 +
-132    0      1 +
-133    0      1 +
-134    0      0 +
-135    0      0 +
-136    0      0 +
-137    0      0 +
-138    0      0 +
-139    0      0 +
-140    0      0 +
-141    1      0 +
-142    1      0 +
-143    1      0 +
-144    1      0 +
-145    1      0 +
-146    1      0 +
-147    1      0 +
-148    1      0 +
-149    1      0 +
-150    1      0 +
-151    1      0 +
-152    1      0 +
-153    1      0 +
-154    1      0 +
-155    1      0 +
-156    1      0 +
-157    1      0 +
-158    1      0 +
-159    1      0 +
-160    1      0 +
-161    1      0 +
-162    1      0 +
-163    1      0 +
-164    1      0 +
-165    1      0 +
-166    1      0 +
-167    1      0 +
-168    1      0 +
-169    1      0 +
-170    1      0 +
-171    1      0 +
-172    1      0 +
-173    1      0 +
-174    1      0 +
-175    1      0 +
-176    1      0 +
-177    1      0 +
-178    1      0 +
-179    1      0 +
-180    1      0 +
-181    1      0 +
-182    1      0 +
-183    1      0 +
-184    1      0 +
-185    1      0 +
-186    1      0 +
-187    1      0 +
-188    1      0 +
-189    1      0 +
-190    1      0 +
-191    1      0 +
-192    1      0 +
-193    1      0 +
-194    1      0 +
-195    1      0 +
-196    1      0 +
-197    1      0 +
-198    1      0 +
-199    1      0 +
-200    1      0 +
-201    1      0 +
-202    1      0 +
-203    1      1 +
-204    1      0 +
-205    1      0 +
-206    1      0 +
-207    1      0 +
-208    1      0 +
-209    1      0 +
-210    1      0 +
-211    1      0 +
-212    1      0 +
-213    1      0 +
-214    1      0 +
-215    1      0 +
-216    1      0 +
-217    1      0 +
-218    1      0 +
-219    1      0 +
-220    1      0 +
-221    1      0 +
-222    1      0 +
-223    1      0 +
-224    1      0 +
-225    1      0 +
-226    1      1 +
-227    1      0 +
-228    1      0 +
-229    1      1 +
-230    1      0 +
-231    1      0 +
-232    1      0 +
-233    1      0 +
-234    1      0 +
-235    1      0 +
-236    1      0 +
-237    1      0 +
-238    1      0 +
-239    1      0 +
-240    1      0 +
-241    1      0 +
-242    1      1 +
-243    1      0 +
-244    1      0 +
-245    1      0 +
-246    1      0 +
-247    1      0 +
-248    1      0 +
-249    1      0 +
-250    1      0 +
-251    1      0 +
-252    1      0 +
-253    1      0 +
-254    1      0 +
-255    1      0 +
-256    1      0 +
-257    1      0 +
-258    1      0 +
-259    1      0 +
-260    1      0 +
-261    1      0 +
-262    1      0 +
-263    1      0 +
-264    1      0 +
-265    1      0 +
-266    1      0 +
-267    1      0 +
-268    1      0 +
-269    1      0 +
-270    1      0 +
-271    1      0 +
-272    1      0 +
-273    1      0 +
-274    1      0 +
-275    1      0 +
-276    1      0 +
-277    1      0 +
-278    1      0 +
-279    1      0 +
-280    1      0 +
-281    1      0 +
-282    1      0 +
-283    1      0 +
-284    1      0 +
-285    1      0 +
-286    1      0 +
-287    1      0 +
-288    1      0 +
-289    1      0 +
-290    1      0 +
-291    1      0 +
-292    1      0 +
-293    1      0 +
-294    1      0 +
-295    1      0 +
-296    1      0 +
-297    1      0 +
-298    1      0 +
-299    1      0 +
-300    1      0 +
-301    1      0 +
-302    1      0 +
-303    1      0 +
-304    1      0 +
-305    1      1 +
-306    1      0 +
-307    1      0 +
-308    1      0 +
-309    1      0 +
-310    1      0 +
-311    1      0 +
-312    1      0 +
-313    1      0 +
-314    1      0 +
-315    1      0 +
-316    1      0 +
-317    1      0 +
-318    1      0 +
-319    1      0 +
-320    1      0 +
-321    1      0 +
-322    1      0 +
-323    1      0 +
-324    1      0 +
-325    1      0 +
-326    1      0 +
-327    1      0 +
-328    1      1 +
-329    1      0 +
-330    1      0 +
-331    1      0 +
-332    1      0 +
-333    1      0 +
-334    1      0 +
-335    1      0 +
-336    1      0 +
-337    1      0 +
-338    1      0 +
-339    1      0 +
-340    1      0 +
-341    1      0 +
-342    1      0 +
-343    1      0 +
-344    1      0 +
-345    1      0 +
-346    1      0 +
-347    1      0 +
-348    1      0 +
-349    1      0 +
-350    1      0 +
-351    1      0 +
-352    1      0 +
-353    1      0 +
-354    1      0 +
-355    1      0 +
-356    1      0+
 > df$gene <- factor(df$gene, levels = c(0,1), labels = c("mutant", "norm")) > df$gene <- factor(df$gene, levels = c(0,1), labels = c("mutant", "norm"))
 > df$cancer <- factor(df$cancer, levels = c(0,1), labels = c("nocancer", "cancer")) > df$cancer <- factor(df$cancer, levels = c(0,1), labels = c("nocancer", "cancer"))
Line 516: Line 167:
 5   mutant nocancer 5   mutant nocancer
 6   mutant nocancer 6   mutant nocancer
-7   mutant nocancer +>
-8   mutant   cancer +
-9   mutant nocancer +
-10  mutant nocancer +
-11  mutant nocancer +
-12  mutant nocancer +
-13  mutant nocancer +
-14  mutant nocancer +
-15  mutant nocancer +
-16  mutant nocancer +
-17  mutant nocancer +
-18  mutant nocancer +
-19  mutant nocancer +
-20  mutant nocancer +
-21  mutant nocancer +
-22  mutant nocancer +
-23  mutant nocancer +
-24  mutant nocancer +
-25  mutant nocancer +
-26  mutant nocancer +
-27  mutant nocancer +
-28  mutant nocancer +
-29  mutant   cancer +
-30  mutant   cancer +
-31  mutant nocancer +
-32  mutant nocancer +
-33  mutant nocancer +
-34  mutant nocancer +
-35  mutant nocancer +
-36  mutant nocancer +
-37  mutant   cancer +
-38  mutant nocancer +
-39  mutant nocancer +
-40  mutant nocancer +
-41  mutant nocancer +
-42  mutant nocancer +
-43  mutant nocancer +
-44  mutant nocancer +
-45  mutant nocancer +
-46  mutant   cancer +
-47  mutant nocancer +
-48  mutant nocancer +
-49  mutant nocancer +
-50  mutant nocancer +
-51  mutant   cancer +
-52  mutant nocancer +
-53  mutant nocancer +
-54  mutant nocancer +
-55  mutant nocancer +
-56  mutant nocancer +
-57  mutant nocancer +
-58  mutant nocancer +
-59  mutant   cancer +
-60  mutant   cancer +
-61  mutant nocancer +
-62  mutant nocancer +
-63  mutant nocancer +
-64  mutant nocancer +
-65  mutant nocancer +
-66  mutant nocancer +
-67  mutant nocancer +
-68  mutant nocancer +
-69  mutant nocancer +
-70  mutant nocancer +
-71  mutant nocancer +
-72  mutant nocancer +
-73  mutant nocancer +
-74  mutant nocancer +
-75  mutant nocancer +
-76  mutant nocancer +
-77  mutant nocancer +
-78  mutant nocancer +
-79  mutant nocancer +
-80  mutant nocancer +
-81  mutant nocancer +
-82  mutant nocancer +
-83  mutant nocancer +
-84  mutant nocancer +
-85  mutant nocancer +
-86  mutant nocancer +
-87  mutant nocancer +
-88  mutant   cancer +
-89  mutant nocancer +
-90  mutant nocancer +
-91  mutant   cancer +
-92  mutant   cancer +
-93  mutant nocancer +
-94  mutant nocancer +
-95  mutant nocancer +
-96  mutant nocancer +
-97  mutant nocancer +
-98  mutant nocancer +
-99  mutant nocancer +
-100 mutant nocancer +
-101 mutant   cancer +
-102 mutant nocancer +
-103 mutant nocancer +
-104 mutant nocancer +
-105 mutant nocancer +
-106 mutant nocancer +
-107 mutant   cancer +
-108 mutant nocancer +
-109 mutant nocancer +
-110 mutant nocancer +
-111 mutant nocancer +
-112 mutant nocancer +
-113 mutant nocancer +
-114 mutant   cancer +
-115 mutant   cancer +
-116 mutant nocancer +
-117 mutant nocancer +
-118 mutant nocancer +
-119 mutant nocancer +
-120 mutant nocancer +
-121 mutant nocancer +
-122 mutant nocancer +
-123 mutant nocancer +
-124 mutant nocancer +
-125 mutant nocancer +
-126 mutant nocancer +
-127 mutant nocancer +
-128 mutant nocancer +
-129 mutant nocancer +
-130 mutant nocancer +
-131 mutant   cancer +
-132 mutant   cancer +
-133 mutant   cancer +
-134 mutant nocancer +
-135 mutant nocancer +
-136 mutant nocancer +
-137 mutant nocancer +
-138 mutant nocancer +
-139 mutant nocancer +
-140 mutant nocancer +
-141   norm nocancer +
-142   norm nocancer +
-143   norm nocancer +
-144   norm nocancer +
-145   norm nocancer +
-146   norm nocancer +
-147   norm nocancer +
-148   norm nocancer +
-149   norm nocancer +
-150   norm nocancer +
-151   norm nocancer +
-152   norm nocancer +
-153   norm nocancer +
-154   norm nocancer +
-155   norm nocancer +
-156   norm nocancer +
-157   norm nocancer +
-158   norm nocancer +
-159   norm nocancer +
-160   norm nocancer +
-161   norm nocancer +
-162   norm nocancer +
-163   norm nocancer +
-164   norm nocancer +
-165   norm nocancer +
-166   norm nocancer +
-167   norm nocancer +
-168   norm nocancer +
-169   norm nocancer +
-170   norm nocancer +
-171   norm nocancer +
-172   norm nocancer +
-173   norm nocancer +
-174   norm nocancer +
-175   norm nocancer +
-176   norm nocancer +
-177   norm nocancer +
-178   norm nocancer +
-179   norm nocancer +
-180   norm nocancer +
-181   norm nocancer +
-182   norm nocancer +
-183   norm nocancer +
-184   norm nocancer +
-185   norm nocancer +
-186   norm nocancer +
-187   norm nocancer +
-188   norm nocancer +
-189   norm nocancer +
-190   norm nocancer +
-191   norm nocancer +
-192   norm nocancer +
-193   norm nocancer +
-194   norm nocancer +
-195   norm nocancer +
-196   norm nocancer +
-197   norm nocancer +
-198   norm nocancer +
-199   norm nocancer +
-200   norm nocancer +
-201   norm nocancer +
-202   norm nocancer +
-203   norm   cancer +
-204   norm nocancer +
-205   norm nocancer +
-206   norm nocancer +
-207   norm nocancer +
-208   norm nocancer +
-209   norm nocancer +
-210   norm nocancer +
-211   norm nocancer +
-212   norm nocancer +
-213   norm nocancer +
-214   norm nocancer +
-215   norm nocancer +
-216   norm nocancer +
-217   norm nocancer +
-218   norm nocancer +
-219   norm nocancer +
-220   norm nocancer +
-221   norm nocancer +
-222   norm nocancer +
-223   norm nocancer +
-224   norm nocancer +
-225   norm nocancer +
-226   norm   cancer +
-227   norm nocancer +
-228   norm nocancer +
-229   norm   cancer +
-230   norm nocancer +
-231   norm nocancer +
-232   norm nocancer +
-233   norm nocancer +
-234   norm nocancer +
-235   norm nocancer +
-236   norm nocancer +
-237   norm nocancer +
-238   norm nocancer +
-239   norm nocancer +
-240   norm nocancer +
-241   norm nocancer +
-242   norm   cancer +
-243   norm nocancer +
-244   norm nocancer +
-245   norm nocancer +
-246   norm nocancer +
-247   norm nocancer +
-248   norm nocancer +
-249   norm nocancer +
-250   norm nocancer +
-251   norm nocancer +
-252   norm nocancer +
-253   norm nocancer +
-254   norm nocancer +
-255   norm nocancer +
-256   norm nocancer +
-257   norm nocancer +
-258   norm nocancer +
-259   norm nocancer +
-260   norm nocancer +
-261   norm nocancer +
-262   norm nocancer +
-263   norm nocancer +
-264   norm nocancer +
-265   norm nocancer +
-266   norm nocancer +
-267   norm nocancer +
-268   norm nocancer +
-269   norm nocancer +
-270   norm nocancer +
-271   norm nocancer +
-272   norm nocancer +
-273   norm nocancer +
-274   norm nocancer +
-275   norm nocancer +
-276   norm nocancer +
-277   norm nocancer +
-278   norm nocancer +
-279   norm nocancer +
-280   norm nocancer +
-281   norm nocancer +
-282   norm nocancer +
-283   norm nocancer +
-284   norm nocancer +
-285   norm nocancer +
-286   norm nocancer +
-287   norm nocancer +
-288   norm nocancer +
-289   norm nocancer +
-290   norm nocancer +
-291   norm nocancer +
-292   norm nocancer +
-293   norm nocancer +
-294   norm nocancer +
-295   norm nocancer +
-296   norm nocancer +
-297   norm nocancer +
-298   norm nocancer +
-299   norm nocancer +
-300   norm nocancer +
-301   norm nocancer +
-302   norm nocancer +
-303   norm nocancer +
-304   norm nocancer +
-305   norm   cancer +
-306   norm nocancer +
-307   norm nocancer +
-308   norm nocancer +
-309   norm nocancer +
-310   norm nocancer +
-311   norm nocancer +
-312   norm nocancer +
-313   norm nocancer +
-314   norm nocancer +
-315   norm nocancer +
-316   norm nocancer +
-317   norm nocancer +
-318   norm nocancer +
-319   norm nocancer +
-320   norm nocancer +
-321   norm nocancer +
-322   norm nocancer +
-323   norm nocancer +
-324   norm nocancer +
-325   norm nocancer +
-326   norm nocancer +
-327   norm nocancer +
-328   norm   cancer +
-329   norm nocancer +
-330   norm nocancer +
-331   norm nocancer +
-332   norm nocancer +
-333   norm nocancer +
-334   norm nocancer +
-335   norm nocancer +
-336   norm nocancer +
-337   norm nocancer +
-338   norm nocancer +
-339   norm nocancer +
-340   norm nocancer +
-341   norm nocancer +
-342   norm nocancer +
-343   norm nocancer +
-344   norm nocancer +
-345   norm nocancer +
-346   norm nocancer +
-347   norm nocancer +
-348   norm nocancer +
-349   norm nocancer +
-350   norm nocancer +
-351   norm nocancer +
-352   norm nocancer +
-353   norm nocancer +
-354   norm nocancer +
-355   norm nocancer +
-356   norm nocancer+
 > tab <- table(df) > tab <- table(df)
 > tab > tab
Line 1009: Line 311:
  
 </code> </code>
-====== 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}/y_{1}) = & \text {odds ratio between  } \\ 
-& \text{odds of y at one point, } y_1 \text { and } \\ 
-& \text{odds of y at another point, } y_2 \\ 
-\text{and  }  y_1 = & a + b (X) \\ 
-              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 1123: Line 405:
  
 </code> </code>
 +
 +===== 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}/y_{1}) = & \text {odds ratio between  } \\
 +& \text{odds of y at one point, } y_1 \text { and } \\
 +& \text{odds of y at another point, } y_2 \\
 +\text{and  }  y_1 = & a + b (X) \\
 +              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 1143: Line 446:
     * 즉, $log(om/of) = b$     * 즉, $log(om/of) = b$
     * $log(1.444613) = b$     * $log(1.444613) = b$
 +    * $ 1.444613 = e^b$
 <code> <code>
 > log(1.444613) > log(1.444613)
Line 1253: Line 557:
 </code> </code>
 마리화나의 사용경험에서 남성이 여성보다 큰 승산이 있다고 판단되었다 (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 ======
  
 +<code>
 +########################################
 +# exercise
 +
 +head(df)
 +table(df)
 +# base 바꾸기
 +df.norm <- df %>% mutate(gene = relevel(gene, ref = "norm"))
 +df.mut <- df %>% mutate(gene = relevel(gene, ref = "mutant"))
 +
 +
 +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, p.can.norm)
 +
 +
 +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, p.can.mut)
 +
 +
 +</code>
 ====== X: numeric variable ====== ====== X: numeric variable ======
 <code> <code>
logistic_regression.1733805030.txt.gz · Last modified: 2024/12/10 13:30 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki