library(tidyverse)
<-
url "https://raw.githubusercontent.com/jlacasa/stat705_fall2024/main/classes/data/lotus_part1.csv"
<- read.csv(url)
data
%>%
data ggplot(aes(species, agb_g))+
geom_boxplot()+
labs(x = "Species",
y = "Aboveground biomass (g)")+
theme_classic()+
theme(aspect.ratio = 1)
Day 10 - 09/11/2024
Statistical Inference
Let’s take a simple statistical model:
The aboveground biomass from the \(i\)th observation, \(y_i\), can be described as \[y_i \sim N(\mu_i , \sigma^2),\]
\[\mu_i = \beta_1 x_{1i} + \beta_2 x_{2i} + \beta_3 x_{3i},\] \[x_{1 i} \left \{ \begin{aligned} & 1 && \text{if species A} \\ & 0 && \text{else} \end{aligned} \right.,\] \[x_{2 i} \left \{ \begin{aligned} & 1 && \text{if species C} \\ & 0 && \text{else} \end{aligned} \right.,\] \[x_{3 i} \left \{ \begin{aligned} & 1 && \text{if species D} \\ & 0 && \text{else} \end{aligned} \right.,\]
for \(i =1, 2, ..., n\), where \(n\) is the total number of observations, \(\mu_i\) is the mean of the \(i\)th observation, \(x_{1i}\), \(x_{2i}\), and \(x_{3i}\) are indicator variables that identify the species as A, C, or D, and thus \(\beta_1\), \(\beta_2\), and \(\beta_3\) are the means of genotypes A, C, and D, respectively, and \(\sigma^2\) is the variance of the data.
<- lm(agb_g ~ 0 + species , data = data)
m <- coef(m)
beta_hat
m
Call:
lm(formula = agb_g ~ 0 + species, data = data)
Coefficients:
speciesA speciesC speciesD
2.937 2.397 2.143
95% Confidence intervals
confint(m)
2.5 % 97.5 %
speciesA 2.688742 3.184410
speciesC 2.148861 2.644529
speciesD 1.895419 2.391086
T-test
<- model.matrix(m)
X <- summary(m)$sigma
sigma_hat <- solve(t(X)%*%X)
unscaled_covariance <- sqrt(diag(unscaled_covariance))*sigma_hat) (se
speciesA speciesC speciesD
0.124231 0.124231 0.124231
<- (nrow(data)-3) dfe
The t statistic \[T = \frac{\hat\theta}{s.e.(\hat\theta)}\]
is used to compute the p value in hypothesis tests. The p value is the probability of observing the t statistic under the conditions of the null hypothesis. In this formula, \(\hat\theta\) is an estimator of an operation of the parameters, e.g., \(\theta = \beta_1 - 0\) or \(\theta = \beta_1-\beta_2\).
The t distribution is similar to a normal distribution, but with heavier tails.
<- seq(-5, 5, by = .1)
x <- dt(x, dfe)
dt <- qt(.975, dfe)
tc
data.frame(x, dt) %>%
ggplot(aes(x, dt))+
geom_ribbon(aes(ymin = 0, ymax = dt),
data = . %>% filter(x < -tc))+
geom_ribbon(aes(ymin = 0, ymax = dt),
data = . %>% filter(x > tc))+
labs(y = "[t]",
x= "t score")+
geom_line()+
theme(aspect.ratio = 1)+
coord_cartesian(expand = F)+
theme_classic()
In the summary
output, we have \(\theta = \beta_j-0\), i.e., we are testing if a given \(\beta_j\) is different to zero.
<- (beta_hat[1] - 0)/se[1]
t_A t_A
speciesA
23.63803
<- (beta_hat[2] - 0)/se[2]
t_C t_C
speciesC
19.29225
<- (beta_hat[3] - 0)/se[3]
t_D t_D
speciesD
17.25216
dt(t_A, dfe)
speciesA
1.087321e-34
dt(t_C, dfe)
speciesC
2.495304e-29
dt(t_D, dfe)
speciesD
1.624738e-26
summary(m)
Call:
lm(formula = agb_g ~ 0 + species, data = data)
Residuals:
Min 1Q Median 3Q Max
-1.04136 -0.46186 0.03253 0.44846 1.41732
Coefficients:
Estimate Std. Error t value Pr(>|t|)
speciesA 2.9366 0.1242 23.64 <2e-16 ***
speciesC 2.3967 0.1242 19.29 <2e-16 ***
speciesD 2.1433 0.1242 17.25 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.6086 on 69 degrees of freedom
Multiple R-squared: 0.9468, Adjusted R-squared: 0.9445
F-statistic: 409.5 on 3 and 69 DF, p-value: < 2.2e-16
For next class
- Homework 2 due.
- Project proposals due.