Overview
\[P(\mathsf{Target\ outcome}) = \frac{\mathsf{Number\ of\ target\ outcomes}}{\mathsf{Number\ of\ all\ outcomes}}\]
\[P = \frac{72}{200} = .36\]
pain.df <- data.frame(Ethnicity = c("Latin", "Non-Latin"),
Diagnosed = c(72, 223),
Not_Diagnosed = c(128, 241)
)
<-
assigns a value, function, etc. to an “object,”
here pain.df
, which I’ve made a
data.frame
c
“concatenates” (combines) a series of values, etc.
into a single list (“array” or “vector”)library(knitr)
library(kableExtra)
table.of.pain <- pain.df %>%
kable(col.names = c("Ethnicity", "Diagnosed", "Not Diagnosed"), align = c("l", "c", "c")) %>%
kable_styling(font_size = 42) %>% kable_material("hover") %>%
add_header_above(c(" " = 1, "Pain Disorder Diagnosis" = 2))
R
are available in add-on packages;
library
invokes a given package
knitr
prepares
output for reportskableExtras
pimps out tables (kable
s)
made by knitr
%>%
is a “pipe” command, which essentially nests
commands inside other commands
pain.df
data frametable.of.pain
object
Pain Disorder Diagnosis
|
||
---|---|---|
Ethnicity | Diagnosed | Not Diagnosed |
Latin | 72 | 128 |
Non-Latin | 223 | 241 |
\[P(\mathsf{Latin\ Patient\ Has\ a\ Pain\ Disorder}) = \frac{\mathsf{Target\ Event}}{\mathsf{All\ Events}}\]
\[= \frac{\mathsf{Latins\ \overline{c}\ Pain\ Disorders}}{(\mathsf{Latins\ \overline{c}\ Pain\ Disorders})+(\mathsf{Latins\ \overline{s}\ Pain\ Disorders})}\]
\[= \frac{72}{72+128} = \frac{72}{200} = .36\]
[]
) index parts (“elements”) of an
object## [1] "Latin"
## [1] 72
## Ethnicity Diagnosed Not_Diagnosed
## 1 Latin 72 128
## Ethnicity Diagnosed Not_Diagnosed
## 1 Latin 72 128
## 2 Non-Latin 223 241
## [1] 200
p.latin.patient.diagnosed.with.pain.disorder <- pain.df[1, 2] / (pain.df[1, 2] + pain.df[1, 3])
p.latin.patient.diagnosed.with.pain.disorder
## [1] 0.36
$
accesses a column (or other variable-like element) of
an object…## [1] "Latin" "Non-Latin"
## [1] 72 223
## [1] 0.3600000 0.4806034
pain.df$Risk_of_Pain_Diagnosis <- pain.df$Diagnosed / (pain.df$Diagnosed + pain.df$Not_Diagnosed)
pain.df$Risk_of_Pain_Diagnosis
## [1] 0.3600000 0.4806034
## Ethnicity Diagnosed Not_Diagnosed Risk_of_Pain_Diagnosis
## 1 Latin 72 128 0.3600000
## 2 Non-Latin 223 241 0.4806034
Probability of continued opioid use for ≤ 365 days for patients with
childbirth, surgery, trauma, or other pain diagnosis in the week before
their first opioid prescription or chronic pain diagnosis in the 6
months before their first opioid prescription.
Abbreviation: CNCP,
chronic non-cancer pain
Shah, Hayes, & Martin (2017)
\[P(\mathsf{Being\ Diagnosed\ \overline{c}\ Pain\ Disorder})= \frac{72}{72+128} = \frac{72}{200}\]
\[\mathsf{Odds\ of\ Being\ Diagnosed\ with\ Pain\ Disorder} = \frac{72}{128} \approx .56\]
Ethnicity | Diagnosed | Not Diagnosed |
---|---|---|
Latin | 72 | 128 |
Non-Latin | 223 | 241 |
Odds—not odds ratios—for each group:
Group | Present | Not Present) |
---|---|---|
Target | A | B |
Reference | C | D |
\[OR = \frac{(\textsf{Target & Present / Target & Not Present})}{(\textsf{Reference & Present / Reference & Not Present})}\]
\[OR = \frac{(\textsf{A / B)}}{\textsf{(C / D)}}\]
Ethnicity | Diagnosed | Not Diagnosed |
---|---|---|
Latin | 72 | 128 |
Non-Latin | 223 | 241 |
\[OR = \frac{(\textsf{Latin & Diagnosed / Latin & Not Diagnosed})}{(\textsf{Non-Latin & Diagnosed / Non-Latin & Not Diagnosed})}\]
\[OR = \frac{(72 / 128)}{(223 / 241)} \approx \frac{.56}{.92}\approx .61\]
\[OR = \frac{(223 / 241)}{(72 / 128)} = \frac{.92}{.56} \approx 1.6\]
or simply: \(\frac{1}{.61} \approx 1.6\)
Yeah, like this:
Ethnicity | Diagnosed | Not Diagnosed |
---|---|---|
Latin | 72 | 128 |
Non-Latin | 223 | 241 |
pain.df.counts <- subset(pain.df, select = c("Diagnosed", "Not_Diagnosed"))
fisher.test(pain.df.counts)
##
## Fisher's Exact Test for Count Data
##
## data: pain.df.counts
## p-value = 0.00491
## alternative hypothesis: true odds ratio is not equal to 1
## 95 percent confidence interval:
## 0.4250886 0.8665430
## sample estimates:
## odds ratio
## 0.6083658
p-value = .00491
0.425
–
0.866
) for the odds ratio (0.608
) doesn’t
overlap 1fisher.test
also creates an objectsummary()
command:## Length Class Mode
## p.value 1 -none- numeric
## conf.int 2 -none- numeric
## estimate 1 -none- numeric
## null.value 1 -none- numeric
## alternative 1 -none- character
## method 1 -none- character
## data.name 1 -none- character
str()
command:
str()
works to “look inside” most any object or
function in R
## List of 7
## $ p.value : num 0.00491
## $ conf.int : num [1:2] 0.425 0.867
## ..- attr(*, "conf.level")= num 0.95
## $ estimate : Named num 0.608
## ..- attr(*, "names")= chr "odds ratio"
## $ null.value : Named num 1
## ..- attr(*, "names")= chr "odds ratio"
## $ alternative: chr "two.sided"
## $ method : chr "Fisher's Exact Test for Count Data"
## $ data.name : chr "pain.df.counts"
## - attr(*, "class")= chr "htest"
$
can access any of those elements of the
fisher.test
object:## [1] 0.004910079
## [1] 0.4250886 0.8665430
## attr(,"conf.level")
## [1] 0.95
Formula for a normal distribution:
\[f(x, \mu, \sigma) = \frac{1}{\sigma\sqrt{2\pi}}e^{\frac{-(x - \mu)^2}{2\sigma^2}}\]
From StackExchange
\[ \chi^2 = \sum{\frac{(Observed - Expected)^2}{Expected}}\]
So:
Ethnicity | Diagnosed | Not Diagnosed | Total |
---|---|---|---|
Latin | 72 | 128 | 200 |
Value Type | Diagnosed | Not Diagnosed | Total |
---|---|---|---|
Observed | 72 | 128 | 200 |
Expected | 100 | 100 | 200 |
Value Type | Diagnosed | Not Diagnosed |
---|---|---|
Observed | 72 | 128 |
Expected | 100 | 100 |
Observed - Expected | -28 | 28 |
## [1] 3.841459