\[\Large \boldsymbol{Y} = \boldsymbol{\beta X} + \boldsymbol{\epsilon}\]
Why not have Spores = Longevity + Longevity2
2. Generalized Linear Models
- Assessing Error Assumptions
3. Poisson Regression
4. Logistic Regression
Metabolic Rate = a ∗ massb
log(Metabolic Rate) = a + b*log(mass) + error
log(MetabolicRate) = log(a) + b ∗ log(mass) + error implies
\[Metabolic Rate = a ∗ mass^b ∗ error\]
but we often want
\[Metabolic Rate = a ∗ mass^b + error\]
Algorithmically searches to minimize \(\sum{(\hat{Y}-Y_i)^2}\)
library(bbmle)
primate.mle <- mle2(bmr.watts ~ dnorm(a*mass.g^b, sigma),
data=metabolism,
start=list(a = 0.0172858,
b = 0.74160,
sigma=5),
optimizer = "nlminb",
lower=c(sigma=1e-7))
But this may not solve the problem as…
Why?
\[\Large \boldsymbol{Y} = \boldsymbol{\beta X} + \boldsymbol{\epsilon}\]
\[\boldsymbol{\epsilon} \sim N(\boldsymbol{0}, \boldsymbol{\sigma})\]
\[\Large \boldsymbol{\hat{Y}_{i}} = \boldsymbol{\beta X_i} \]
\[\Large Y_i \sim \mathcal{N}(\hat{Y_i},\sigma^{2})\]
\[\Large \boldsymbol{\eta_{i}} = \boldsymbol{\beta X_i} \]
\[\Large \hat{Y_i} = \eta_{i}\] Identity Link Function
\[\Large Y_i \sim \mathcal{N}(\hat{Y_i},\sigma^{2})\]
\[\Large \boldsymbol{\eta_{i}} = \boldsymbol{\beta X_i} \]
\[\Large Log(\hat{Y_i}) = \eta_{i}\] Log Link Function
\[\Large Y_i \sim \mathcal{N}(\hat{Y_i},\sigma^{2})\]
\[\Large \boldsymbol{\eta_{i}} = \boldsymbol{\beta X_i} \]
\[\Large Log(\hat{Y_i}) = \eta_{i}\]
\[\Large Y_i \sim \mathcal{N}(\hat{Y_i},\sigma^{2})\] Error is Normal
\[\boldsymbol{\eta} = \boldsymbol{\beta X}\]
\[f(\boldsymbol{\hat{Y}}) = \boldsymbol{\eta}\]
f(y) is called the link function
\[\boldsymbol{Y} = E(\boldsymbol{\hat{Y}}, \theta)\]
E is any distribution from the Exponential Family
\(\theta\) is an error parameter, and can be a function of Y
Basic Premise:
The error distribution is from the exponential family
For these distributions, the variance is a funciton of the fitted value on the curve: \(var(Y_i) = \theta V(\mu_i)\)
For a normal distribution, \(var(\mu_i) = \theta*1\) as \(V(\mu_i)=1\)
For a poisson distribution, \(var(\mu_i) = 1*\mu_i\) as \(V(\mu_i)=\mu_i\)
Error Generating Proces | Common Use | Typical Data Generating Process Shape |
---|---|---|
Log-Linear | Error accumulates additively, and then is exponentiated | Exponential |
Binomial | Frequency, probability data | Logistic |
Poisson | Count data | Exponential |
Gamma | Waiting times | Inverse or exponential |
McElreath’s Statistical Rethinking
\[ Y_i \sim B(prob, size) \]
Basic Premise:
We have a linear predictor, \(\eta_i = a+Bx\)
That predictor is linked to the fitted value of \(Y_i\), \(\mu_i\)
We call this a link function, such that \(g(\mu_i) = \eta_i\)
For example, for a linear function, \(\mu_i = \eta_i\)
For an exponential function, \(log(\mu_i) = \eta_i\)
McElreath’s Statistical Rethinking
\[\Large \boldsymbol{\eta_{i}} = \boldsymbol{\beta X_i} \]
\[\Large Logit(\hat{Y_i}) = \eta_{i}\] Logit Link Function
\[\Large Y_i \sim \mathcal{B}(\hat{Y_i}, size)\]
OR, with Success and Failures
LR Chisq | Df | Pr(>Chisq) | |
---|---|---|---|
Dose | 233.8357 | 1 | 0 |
And logit coefficients
term | estimate | std.error | statistic | p.value |
---|---|---|---|---|
(Intercept) | -1.4077690 | 0.1484785 | -9.481298 | 0 |
Dose | 0.0134684 | 0.0010464 | 12.870912 | 0 |
\[Log-Odds = Log\frac{p}{1-p} = logit(p)\]
\[\beta = logit(p_2) - logit(p_1)\]
\[\beta = Log\frac{p_1}{1-p_1} - Log\frac{p_2}{1-p_2}\]
We need to know both p1 and \(\beta\) to interpret this.
If p1 = 0.7, \(\beta\) = 0.01347, then p2 = 0.702
If not - overdispersion - i.e. variance and prediction not scaling well
Error Generating Proces | Common Use | Typical Data Generating Process Shape |
---|---|---|
Log-Linear | Error accumulates additively, and then is exponentiated | Exponential |
Poisson | Count data | Exponential |
Binomial | Frequency, probability data | Logistic |
Gamma | Waiting times | Inverse or exponential |
Example from http://seananderson.ca/2014/04/08/gamma-glms/
\[Y_i \sim Gamma(shape, scale)\]
\[Y_i \sim Gamma(shape, scale)\]
For a fit value \(\hat{Y_i}\):
LR Chisq | Df | Pr(>Chisq) | |
---|---|---|---|
time_fishing | 93.38621 | 1 | 0 |
Estimate | Std. Error | t value | Pr(>|t|) | |
---|---|---|---|---|
(Intercept) | 0.5399 | 0.1752 | 3.0812 | 0.0027 |
time_fishing | 1.1304 | 0.1158 | 9.7637 | 0.0000 |
Is this linear?
OR
Call:
glm(formula = FRONDS ~ HLD_DIAM, family = quasipoisson(link = "log"),
data = kelp)
Deviance Residuals:
Min 1Q Median 3Q Max
-5.9021 -2.3871 -0.5574 1.6132 6.5117
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.778059 0.160455 11.081 < 2e-16 ***
HLD_DIAM 0.023624 0.002943 8.027 1.45e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for quasipoisson family taken to be 7.852847)
Null deviance: 1289.17 on 107 degrees of freedom
Residual deviance: 832.56 on 106 degrees of freedom
(32 observations deleted due to missingness)
AIC: NA
Number of Fisher Scoring iterations: 5
Call:
glm.nb(formula = FRONDS ~ HLD_DIAM, data = kelp, init.theta = 2.178533101,
link = log)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.3712 -0.9699 -0.2338 0.5116 1.9956
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.657831 0.166777 9.940 < 2e-16 ***
HLD_DIAM 0.026365 0.003707 7.113 1.14e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(2.1785) family taken to be 1)
Null deviance: 165.63 on 107 degrees of freedom
Residual deviance: 114.49 on 106 degrees of freedom
(32 observations deleted due to missingness)
AIC: 790.23
Number of Fisher Scoring iterations: 1
Theta: 2.179
Std. Err.: 0.336
2 x log-likelihood: -784.228
betareg
for example - it’s just another glm!