Skip to content

Commit 9709699

Browse files
committed
tweaks
1 parent 38f02df commit 9709699

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

to_explain_or_predict.Rmd

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,18 +299,30 @@ $$\large{y= \alpha + \beta_{i} x_{i} + \epsilon}$$
299299

300300

301301
---
302-
# The Generalized Linear Model (GLM):
302+
## How can we use regression on other distributions / data types?
303303

304-
For distributions in the exponential family, GLM allows the linear model to relate to response through a function:
304+
We can 'zoom out' to a more the 'Generalized Linear Model (GLM):
305+
306+
_For distributions in the exponential family, GLM allows the linear model to relate to response through a function:_
305307

306308
$$\large{g(\mu)= \alpha + \beta_{i} x_{i}}$$
307309
<br>
308310
+ Where $\mu$ is the _expectation_ of $Y$,
309311
+ $g$ is the link function - related to each
310312

311313
--
314+
<br>
315+
316+
###So, for logistic regression, the link function is 'logit' or the log odds of the event.
317+
318+
???
319+
320+
As the name suggest, a more general form allows us to use different distributions, but understand them as a linear model on a given scale. E.g. for count data, $\mu$ would be the expected count, and $g$ would be the natural logarithm, and the model is using the Poisson distribution.
312321

313-
So, for logistic regression, the link function is 'logit' or the log odds of the event.
322+
In our case, for binary, we are modelling the 'odds' of the outcome (death) on the log scale, with a binomial distribution.
323+
This is the log-odd, or 'logit' link fiction: hence logistic regression.
324+
325+
__Importantly, I can use it to examine the explanatory relationships, or to predict new data__
314326

315327
---
316328

@@ -607,10 +619,9 @@ You might be happy with a 'wrong' model for in prediction, if it gives better pr
607619
]
608620

609621
.pull-right[
610-
<center>
611-
<div class="tenor-gif-embed" data-postid="1688826665026359193" data-share-method="host" data-aspect-ratio="1.25758" data-width="75%"><a href="https://tenor.com/view/miles-morales-miles-across-the-spider-verse-i-made-another-mistake-i-made-a-mistake-gif-1688826665026359193">Miles Morales Across The Spider Verse GIF</a>from <a href="https://tenor.com/search/miles+morales-gifs">Miles Morales GIFs</a></div> <script type="text/javascript" async src="https://tenor.com/embed.js"></script></center>
612-
<br>
613-
> "With great power comes greate responsibility"
622+
623+
624+
> "With great power comes great responsibility"
614625
- Stan Lee (via Spiderman's Uncle Ben)
615626

616627
]

to_explain_or_predict.html

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>To Explain or Predict: how different modelling objectives change how you use the same tools</title>
55
<meta charset="utf-8" />
66
<meta name="author" content="Chris Mainey" />
7-
<meta name="date" content="2024-11-18" />
7+
<meta name="date" content="2024-11-19" />
88
<script src="libs/header-attrs/header-attrs.js"></script>
99
<link rel="stylesheet" href="xaringan-themer.css" type="text/css" />
1010
</head>
@@ -223,18 +223,30 @@
223223

224224

225225
---
226-
# The Generalized Linear Model (GLM):
226+
## How can we use regression on other distributions / data types?
227227

228-
For distributions in the exponential family, GLM allows the linear model to relate to response through a function:
228+
We can 'zoom out' to a more the 'Generalized Linear Model (GLM):
229+
230+
_For distributions in the exponential family, GLM allows the linear model to relate to response through a function:_
229231

230232
`$$\large{g(\mu)= \alpha + \beta_{i} x_{i}}$$`
231233
&lt;br&gt;
232234
+ Where `\(\mu\)` is the _expectation_ of `\(Y\)`,
233235
+ `\(g\)` is the link function - related to each
234236

235237
--
238+
&lt;br&gt;
239+
240+
###So, for logistic regression, the link function is 'logit' or the log odds of the event.
241+
242+
???
243+
244+
As the name suggest, a more general form allows us to use different distributions, but understand them as a linear model on a given scale. E.g. for count data, `\(\mu\)` would be the expected count, and `\(g\)` would be the natural logarithm, and the model is using the Poisson distribution.
236245

237-
So, for logistic regression, the link function is 'logit' or the log odds of the event.
246+
In our case, for binary, we are modelling the 'odds' of the outcome (death) on the log scale, with a binomial distribution.
247+
This is the log-odd, or 'logit' link fiction: hence logistic regression.
248+
249+
__Importantly, I can use it to examine the explanatory relationships, or to predict new data__
238250

239251
---
240252

@@ -298,8 +310,8 @@
298310
## Dep. Variable: DEATH_EVENT No. Observations: 299
299311
## Model: Logit Df Residuals: 296
300312
## Method: MLE Df Model: 2
301-
## Date: Mon, 18 Nov 2024 Pseudo R-squ.: 0.1359
302-
## Time: 15:02:46 Log-Likelihood: -162.16
313+
## Date: Tue, 19 Nov 2024 Pseudo R-squ.: 0.1359
314+
## Time: 17:16:16 Log-Likelihood: -162.16
303315
## converged: True LL-Null: -187.67
304316
## Covariance Type: nonrobust LLR p-value: 8.308e-12
305317
## =====================================================================================
@@ -430,7 +442,7 @@
430442
```
431443

432444
```
433-
## 0.69
445+
## 0.7713068181818182
434446
```
435447

436448
---
@@ -571,10 +583,9 @@
571583
]
572584

573585
.pull-right[
574-
&lt;center&gt;
575-
&lt;div class="tenor-gif-embed" data-postid="1688826665026359193" data-share-method="host" data-aspect-ratio="1.25758" data-width="75%"&gt;&lt;a href="https://tenor.com/view/miles-morales-miles-across-the-spider-verse-i-made-another-mistake-i-made-a-mistake-gif-1688826665026359193"&gt;Miles Morales Across The Spider Verse GIF&lt;/a&gt;from &lt;a href="https://tenor.com/search/miles+morales-gifs"&gt;Miles Morales GIFs&lt;/a&gt;&lt;/div&gt; &lt;script type="text/javascript" async src="https://tenor.com/embed.js"&gt;&lt;/script&gt;&lt;/center&gt;
576-
&lt;br&gt;
577-
&gt; "With great power comes greate responsibility"
586+
587+
588+
&gt; "With great power comes great responsibility"
578589
- Stan Lee (via Spiderman's Uncle Ben)
579590

580591
]

working_r.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,29 @@ predictions <- predict(ridge1, newx=x.test, type="response") |> as.vector()
7777

7878
ModelMetrics::auc(Test$DEATH_EVENT, predictions)
7979

80+
81+
82+
83+
84+
# logarithmic relationship tested
85+
86+
library(readr)
87+
heart_failure_dt <- read_csv("data/heart_failure_clinical_records_dataset.csv")
88+
89+
r_model_exp2 <- glm(DEATH_EVENT ~ serum_creatinine + ejection_fraction
90+
, data=heart_failure_dt
91+
, family = "binomial")
92+
93+
94+
r_model_exp3 <- glm(DEATH_EVENT ~ serum_creatinine + ejection_fraction
95+
, data=heart_failure_dt
96+
, family = binomial(link = "cloglog"))
97+
98+
99+
summary(r_model_exp2)
100+
summary(r_model_exp3)
101+
102+
103+
library(ModelMetrics)
104+
105+
auc(r_model_exp2)

xaringan-themer.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ a, a > code {
123123
color: var(--link-color);
124124
text-decoration: none;
125125
}
126+
126127
.mjx-chtml{ font-size: 130% !important; }
127128

128129
.footnote {

0 commit comments

Comments
 (0)