Study on risks factor for low birth weights from Baystate medical center in Springfield (MA, USA) during 1986 [Hosmer & Lemeshow (1989), Applied Logistic Regression]
low
: low birth weight indicator coded as: 1
(low birth weight, i.e. \(\leq 2.5kg\)) and 0
(normal birth weight)age
: mother’s age (in years) at the time of birthlwt
: mother’s weight (in pounds) at the time of the last menstrual periodrace
: mother’s race coded as: 1
(white), 2
(black) and 3
(other)smoke
: mother’s smoking status coded as 1
(smoker) and 0
(non-smoker)ptl
: number of previous premature laboursht
: hypertension history indicator coded as: 1
(yes) and 0
(no)ui
: uterine irritability indicator coded as: 1
(yes) and 0
(no)ftv
: number of physician visits during the first trimesterbwt
: infant’s birth weight (in grams) R
primerR
101 for M2 PHDSRstudio
.Rmd
file from the default template"Knit"
button import the dataset in birthweight.txt
(you can use the "Import Dataset"
button from Rstudio
briefly describe the data (use nice table outputs in Rmarkdown
and ggplot2
graphics)
browser()
to debug your function when evaluating at negative argumentsStatistics: summarizing information from experimental observations and quantifying the associated uncertainty
Always start with the research/scientific question !
Statistical Inference: we use a simple Generative Probabilistic model that could have generated the observations (Machine Learning sometimes reject this paradigm – cf. L. Breiman)
The likelihood is a fundamental building block of Biostatistics:
The likelihood function quantifies how likely it is that a given (set of) observation(s) has been generated by our hypothesized Generative Probabilistic model.
The likelihood function is equal to the model joint probability distribution computed for the observations, and thus only a function of the model parameters.
The idea of the MLE is to to optimize the likelihood function given the observations, by finding the model parameters that would give these observations the highest probability of being generated under the model.
seems like a reasonable and intuitive idea !
Reverend Thomas Bayes proposed an alternative framework for statistical inference (actually before the “frequentist” method). It also relies on a probabilistic model through the likelihood function, but has different philosophical grounds than the frequentist.
To be continued…
Computational statistics have become essential in modern statistics, with always bigger data, and always more sophisticated approaches.
Maximizing the likelihood can easily be done analytically for simple linear models.
However: non-linear likelihoods are hard (sometimes impossible) to optimize analytically !
\(\Rightarrow\) numerical optimization
An algorithm to find values for which a function is zero.
Applied to the derivative of the likelihood function, this will identify the MLE
given that the log-likelihood is a concave function
Generally, we maximize the log-likelihood instead of the likelihood.
This is not taught often but it can come very handy if you are writing your own statistical/optimisation program:
\[ \log \sum_{i=1}^n e^{x_i} = c + \log \sum_{i=1}^n e^{x_i-c} \]
Disclaimer: not useful today
About the linear approximation at the \(n + 1\) step: - goes through \(f(x_n)\) - has slope \(f'(x_n)\)
So it has the following equation: \(y= f'(x_n)(x-x_n) + f(x_n)\). Thus we find \(x_{n+1}\) by setting \(y=0\), which gives us \(x_{n+1} = x_n -\frac{f(x_n)}{f'(x_n)}\)
#install.packages("animation")
library("animation")
newton.method(FUN = function(x) (x - 2)^2 - 1, init = 9.5,
rg = c(-1, 10), tol = 0.001, interact = FALSE,
col.lp = c("orange", "red3", "dodgerblue1"),
lwd=1.5)
propose a generative probabilistic model
define the parameter of interest
Write down the log-likelihood and its the first derivative (by hand)
Show that the maximum value (that nullify the derivative) of this likelihood is for \(\displaystyle\widehat{\pi}_{MLE} = \dfrac{1}{n}\sum_{i=1}^ny_i\)
Now let’s use R
to program a Newton-Raphson algorithm to maximise this likelihood numerically
write an R
function that computes either the likelihood or the log-likelihood for a probabiliyt p
, with two additional arguments: i) obs
the vector of observations (that is bw_data$low
by default) and ii) log
a logical (that is FALSE
by default)
Write two R
functions of p
that compute the first and the second derivatives of the log-likelihood respectivelly (each with obs=bw_data$low
as an additional argument)
plot those 4 functions (the likelihood, log-likelihood, )
write a Newton-Raphson function with 5 arguments (the first deriative of the function to maximize, its second derivative, the initial starting point, the tolerance, the maximum number of iterations)
use all three functions to compute the MLE of the low birthweight prevalence
Try several initial values, different tolerance, and experiment with different stopping rules – i.e. convergence criterion – (you can combine them). Compare the results. Does it always work ?
Considering the null hypothesis \(H_0: \pi=0.5\), compute the p-value for: i) the Wald test, ii)the score test, and iii) the Likelihood Ratio Test respectively, and compare to the glm
and anova
functions output.
Comment on the AIC and the 95% confidence interval
# logit and expit are bijective functions used to go from the real space ]-inf, +inf[ to the probability space [0,1]
logit <- function(p){
log(p/(1-p))
}
expit <- function(x){
exp(x)/(1+exp(x))
}
# logisitc regression with just an intercept
my_logistic_reg <- glm(low~1, data=bw_data, family = "binomial")
summary(my_logistic_reg)
expit(my_logistic_reg$coefficients)