1. 建立迴歸模型
data(iris)
# 建立模型:花萼寬度 ~ 其他變數
model1 <- feols(Sepal.Width ~ Sepal.Length + Petal.Length + Petal.Width + Species, data = iris)
summary(model1)
## OLS estimation, Dep. Var.: Sepal.Width
## Observations: 150
## Standard-errors: IID
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.657164 0.255946 6.47467 1.4001e-09 ***
## Sepal.Length 0.377773 0.065569 5.76147 4.8675e-08 ***
## Petal.Length -0.187567 0.083493 -2.24650 2.6194e-02 *
## Petal.Width 0.625710 0.123376 5.07156 1.1956e-06 ***
## Speciesversicolor -1.160285 0.193294 -6.00268 1.5033e-08 ***
## Speciesvirginica -1.398255 0.277146 -5.04519 1.3445e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## RMSE: 0.262392 Adj. R2: 0.622493
2. 結果解釋
變數 | 估計值 (Estimate) | 標準誤 (Std. Error) | t 值 | Pr(> | t |
---|---|---|---|---|---|
(Intercept) | 1.657 | 0.256 | 6.47 | <0.001 | 基準值:當各變數為 0 且物種為 setosa 時的預測值 |
Sepal.Length | 0.378 | 0.066 | 5.76 | <0.001 | 每增加 1 單位花萼長度,花萼寬度約增加 0.38 |
Petal.Length | -0.188 | 0.083 | -2.25 | 0.026 | 花瓣長度增加 1 單位,花萼寬度減少約 0.19 |
Petal.Width | 0.626 | 0.124 | 5.07 | <0.001 | 花瓣寬度增加 1 單位,花萼寬度增加約 0.63 |
Speciesversicolor | -1.160 | 0.193 | -6.00 | <0.001 | 相較 setosa,versicolor 花萼寬度小 1.16 |
Speciesvirginica | -1.398 | 0.277 | -5.05 | <0.001 | 相較 setosa,virginica 花萼寬度小 1.40 |
統計意義
幾乎所有變數的 p 值 < 0.05,表示在 95% 信心水準下皆具顯著性。
星號代表顯著程度:
***:p < 0.001(極顯著)
**:p < 0.01(顯著)
*:p < 0.05(中度顯著)
模型表現
RMSE = 0.262:平均預測誤差約 0.26 單位,屬於合理範圍。
調整後 R² = 0.622:模型能解釋約 62% 的花萼寬度變異,顯示具有良好解釋力。
解釋總結
花萼長度與花瓣寬度呈正相關;花瓣長度呈負相關。
不同物種之間花萼寬度有明顯差異,versicolor 與 virginica 的花萼都比setosa小。
模型整體顯著,結果穩定且具解釋力。
3. 同時檢定兩個係數
linearHypothesis(model1, c("Petal.Length = 0", "Petal.Width = 0"))
##
## Linear hypothesis test:
## Petal.Length = 0
## Petal.Width = 0
##
## Model 1: restricted model
## Model 2: Sepal.Width ~ Sepal.Length + Petal.Length + Petal.Width + Species
##
## Res.Df Df Chisq Pr(>Chisq)
## 1 146
## 2 144 2 26.014 2.245e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Linear hypothesis test: Petal.Length = 0 and Petal.Width = 0
Model 1: restricted model
Model 2: Sepal.Width ~ Sepal.Length + Petal.Length + Petal.Width + Species
p 值 = 2.245 × 10⁻⁶ < 0.001 → 拒絕虛無假設 。 這表示:花瓣長度 (Petal.Length) 和花瓣寬度 (Petal.Width) 至少有一個對花萼寬度 (Sepal.Width) 具有顯著影響。 也就是說,把這兩個變數拿掉後,模型的解釋力會顯著下降。
4. 殘差診斷圖
library(ggplot2)
ggplot(data.frame(fitted = fitted(model1), resid = resid(model1)),
aes(x = fitted, y = resid)) +
geom_point(color = "blue", alpha = 0.6) +
geom_hline(yintercept = 0, color = "red") +
theme_minimal() +
labs(
title = "Residuals vs Fitted (feols Model)",
x = "Fitted Values",
y = "Residuals"
)
5. 使用 fixest::feols() 比較模型與 etable()
model1 <- feols(Sepal.Width ~ Sepal.Length + Petal.Length, data = iris)
model2 <- feols(Sepal.Width ~ Sepal.Length + Petal.Length + Petal.Width + Species, data = iris)
# 模型比較表
etable(list(
"簡化模型" = model1,
"完整模型" = model2
),
style.tex = FALSE,
dict = c(
Sepal.Width = "花萼寬度",
Sepal.Length = "花萼長度",
Petal.Length = "花瓣長度",
Petal.Width = "花瓣寬度"
))
## 簡化模型 完整模型
## Dependent Var.: 花萼寬度 花萼寬度
##
## Constant 1.038*** (0.2882) 1.657*** (0.2559)
## 花萼長度 0.5612*** (0.0653) 0.3778*** (0.0656)
## 花瓣長度 -0.3353*** (0.0307) -0.1876* (0.0835)
## 花瓣寬度 0.6257*** (0.1234)
## Speciesversicolor -1.160*** (0.1933)
## Speciesvirginica -1.398*** (0.2771)
## _________________ ___________________ __________________
## S.E. type IID IID
## Observations 150 150
## R2 0.45641 0.63516
## Adj. R2 0.44902 0.62249
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
6. 結論
根據 fixest::feols() 模型分析結果,花萼寬度 (Sepal.Width) 與花萼長度 (Sepal.Length)、花瓣長度 (Petal.Length) 及花瓣寬度 (Petal.Width) 之間具有顯著線性關係。 在完整模型中,花萼長度與花瓣寬度對花萼寬度呈正向影響,而花瓣長度則呈負向影響;不同物種間的花萼寬度也有明顯差異,versicolor 與 virginica 的花萼顯著比 setosa 小。
linearHypothesis() 檢定顯示花瓣長度與花瓣寬度的係數並非同時為零(p < 0.001),代表兩者對模型具有顯著貢獻。 etable() 比較顯示完整模型的調整後 R²(0.622)明顯高於簡化模型(0.449),說明加入花瓣變數與物種後能有效提升模型解釋力。
殘差圖與散點圖(含 geom_smooth(method = “lm”) 迴歸線)顯示資料分佈合理、線性假設成立、無明顯異質變異。 整體而言,完整模型能準確捕捉花萼寬度的變異,並展現花形特徵與物種分類之間的明確關聯。
7. 繪製迴歸線圖
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point(alpha = 0.8, size = 2) +
geom_smooth(method = "lm", se = FALSE, linewidth = 1) +
theme_minimal(base_size = 14) +
labs(
title = "Linear Relationship between Sepal Width and Sepal Length",
x = "Sepal Length",
y = "Sepal Width"
)
## `geom_smooth()` using formula = 'y ~ x'