COM6005 Statistical Modeling · Module 4b
Model Building Techniques
COM6005 · Module 4b · Source: L4b_Model_building_L02_Complete.pdf (28 slides)
Contents · 7 sections
🎯 本講重點 (Learning outcomes)
- 點解 categorical variable 唔可以直接乘 ;用 dummy variables:k 個 categories → 個 dummies(statistics approach)vs one-hot encoding k 個(DM approach)。
- Data 要用 good format(long format:所有 Y 喺同一個 column)。
- 讀有 categorical predictors 嘅 output:intercept = baseline group,每個 dummy coefficient = 同 baseline 嘅差。
- Python:
astype('category')、C()、smf.ols('score ~ Worker + Machine', ...)。 - Polynomials:,formula 入面要用
I(x**2);log / reciprocal transformations。 - Interaction terms:;
A:BvsA*B;categorical × categorical 嘅 interaction 數目。
⭐ 考試必溫 Top-5
- k 個 categories → 個 dummy variables(2 → 1、3 → 2、7 → 6);冇 dummy 嗰個係 baseline。
- :Eng = 56、Hum = 61.25、Sci = 58.8——coefficient = 同 baseline (Eng) 嘅平均差。
- Machines output:Intercept 53.6167 = Worker 1 用 Machine A;Machine[T.C] = 13.9167 → 同一個 worker,用 C 比 A 平均高 13.92 分。
- Formula operators:
+加 variable、-移除(-1冇 intercept)、:只有 interaction、*= main effects + interaction;I()做數學運算。 - Interaction 嘅意思:A 對 Y 嘅 effect 取決於 B();categorical: 個 interaction terms(3 × 4 categories → )。
1. 點解要 Model building?
Slide 1:Lecture 4 嘅 multiple linear regression 假設每個 X 都係 numerical、同 Y 係直線關係、而且各自獨立咁影響 Y。現實唔一定咁,所以本講教三種「擴充」model 嘅技巧:
| 技巧 | 解決咩問題 | Formula 寫法 |
|---|---|---|
| Categorical predictors(dummy variables) | X 係類別(college、machine、weekday),唔可以直接乘 | y ~ C(group) |
| Quadratic / cubic terms(polynomials) | X 同 Y 嘅關係係彎曲(non-linear) | y ~ x + I(x**2) |
| Interaction terms | A 嘅 effect 會隨 B 改變 | y ~ A + B + A:B 或 y ~ A*B |
Module 4 講過嘅 residual vs fitted plot:見到曲線 pattern = non-linearity → 本講嘅 polynomial terms 就係其中一個補救方法。加 terms 後記得用 Module 4 嘅 out-of-sample RMSE / AIC / BIC 比較 model,避免 overfitting。
2. Categorical predictors:Dummy variables(slides 2–9)
Slide 2:model 要將 乘 。如果 ,「」冇意思——categorical variables cannot be multiplied directly by 。解決方法:轉做 0/1 數字。
2.1 Statistics approach: dummy variables(slide 3)
| Categories | 例子 | Dummy variables |
|---|---|---|
| 2 | Part-time / Full-time | 1: if Full-time, 0 otherwise |
| 3 | Eng / Hum / Sci | 2: if Hum; if Sci |
| 7 | Sun / Mon / … / Sat | 6: 至 |
規則:k 個 categories → 個 dummy variables。冇自己 dummy 嗰個 category(上面嘅 Part-time、Eng、Sun)叫 baseline / reference category,佢嘅所有 dummies 都係 0。
知道 同 之後,Eng 已經確定(兩個都係 0 就係 Eng),第三個 dummy 係多餘嘅資訊。更重要:如果三個 dummies 都放, 每一行都成立,等於 intercept 嗰欄(全部 1)——X matrix 嘅 columns linearly dependent, 冇 inverse,Module 4 嘅 計唔到。呢個叫 dummy variable trap(perfect multicollinearity)。
2.2 Data format(slides 4–6)
Slide 4 Bad format:每個 college 一個 column(wide format),Score (Y) 分散喺三個 column。Slide 5 Good format:All Y should be in the same column——一個 column 係 X = College,一個 column 係 Y = Score(long format),每行一個 observation。
| Bad (wide) | Good (long) |
|---|---|
| Engineering: 45, 65, 58 Science: 36, 75, 49, 62, 72 Humanity: 86, 49, 67, 43 | (Engineering, 45), (Engineering, 65), (Engineering, 58), (Science, 36), …, (Humanity, 43)——共 12 行 |
Slide 6:Good format transform 之後,College 變成兩個 dummy columns :
| College | ||
|---|---|---|
| Engineering(baseline) | 0 | 0 |
| Science | 1 | 0 |
| Humanity | 0 | 1 |
import pandas as pd
# Bad format(wide)→ Good format(long)
wide = pd.DataFrame({'Engineering': [45, 65, 58, None, None],
'Science': [36, 75, 49, 62, 72],
'Humanity': [86, 49, 67, 43, None]})
df = wide.melt(var_name='College', value_name='Score').dropna() # 12 行,Y 喺同一個 column
COM6001 pandas:melt()(wide → long)同 pivot()(long → wide)。Good format 就係 “tidy data”:每個 variable 一個 column、每個 observation 一行——亦係 COM6002 relational table 嘅形式。
2.3 DM approach:One-hot encoding(slide 7)
Data Mining 嘅做法係 get_dummies()(slide 寫 get_dummy(),pandas 正確名係 pd.get_dummies)= one-hot encoding:k 個 categories → k 個 columns,Engineering = [1,0,0]、Science = [0,1,0]、Humanity = [0,0,1]。
| Statistics approach | DM approach (one-hot) | |
|---|---|---|
| Columns 數 | k | |
| Engineering | [0, 0] | [1, 0, 0] |
| Science | [1, 0] | [0, 1, 0] |
| Humanity | [0, 1] | [0, 0, 1] |
| 用喺 | 有 intercept 嘅 regression(避免 dummy variable trap) | Decision tree、kNN、neural network 等 DM / ML models |
pd.get_dummies(df['College'], dtype=int) # one-hot:3 columns
pd.get_dummies(df['College'], drop_first=True, dtype=int) # k-1:drop 第一個(Engineering)→ Humanity, Science
COM6004 Chapter 2 Data preprocessing:將 nominal attribute 轉 binary attributes 就係 one-hot encoding。
2.4 Model 同 coefficient 解讀(slides 8–9)
Slide 8:Python 入面,對 variable 用 astype('category') 之後,transform 會自動做,唔使自己整 dummies。BUT this is important for the understanding of Python output——你要識讀出嚟嘅 coefficients 代表咩。
Slide 9 嘅 fitted model:
| Case | |||
|---|---|---|---|
| Eng | 0 | 0 | 56 |
| Hum | 1 | 0 | 56 + 5.25 = 61.25 |
| Sci | 0 | 1 | 56 + 2.8 = 58.8 |
- Intercept 56 = baseline group(Engineering)嘅平均 score。
- :Humanity 學生平均比 Engineering 高 5.25 分。
- :Science 平均比 Engineering 高 2.8 分。
- Hum vs Sci:(唔使再 fit,直接相減)。
用 slides 4–5 嘅數據:Eng ;Sci ;Hum 。完全同 slide 9 一樣——OLS 喺每組入面最小化 squared error,答案就係組平均。所以 coefficient = 組平均 − baseline 平均:、。
import statsmodels.formula.api as smf
model = smf.ols('Score ~ College', data=df).fit() # College 係 string,自動當 categorical
print(model.params)
# Intercept 56.00
# College[T.Humanity] 5.25
# College[T.Science] 2.80係 Hum 同 baseline 嘅差,唔係 Hum 嘅平均(61.25)。而 coefficient 嘅 t-test 係測試「Hum 平均 = Eng 平均」,唔係測試 Hum 平均 = 0。換 baseline(例如 Science),所有 coefficients 數值都會變,但 fitted values 唔變。
Module 3a:只有 2 個 categories(1 個 dummy)時,dummy coefficient 嘅 t-test 等同 pooled-variance two-sample t-test;3 個或以上 categories 時,整個 model 嘅 F-test 就係 one-way ANOVA(:所有組平均相等)。
Slide 10 只係一張插圖(實驗室人員操作機器),引入下一個例子,冇額外內容。
3. Real data:Machines(slides 11–16)
Slide 11:Six workers were chosen randomly to operate each machine three times.(Source:Pinheiro and Bates,R package MEMSS)
Worker:worker 嘅 unique identifier(1–6,staff ID)Machine:machine brand(A / B / C)score:productivity score(Y)
總共 個 observations。
3.1 Worker 雖然係數字,都係 categorical(slides 12–14)
Slide 12 good format:每行 (Worker, Machine, Score)。Worker 用 1–6 表示,但 1、2、3 只係 ID label——Worker 6 唔係「Worker 1 嘅六倍」。如果唔處理,Python 會當 Worker 係 numerical,只 fit 一條 slope(每加 1 號 worker,score 變幾多),完全冇意思。所以要:
machines_data['Worker'] = machines_data['Worker'].astype('category')
Slide 13 transform 後:Worker 6 levels → 5 dummies(W2–W6,Python 叫 T.2–T.6),Machine 3 levels → 2 dummies(MB、MC,即 T.B、T.C)。Baseline = Worker 1、Machine A。
1 (intercept) + + = 8 個 ;Df Model = 7(唔計 intercept);Df Residuals = 。
Slides 12–13 表入面嘅 score(45、65、58…)同 college 例子嘅數字一樣,應該係示範 format 用嘅;slide 16 顯示嘅真實 machines_data 頭幾行係 Worker 1, Machine A:52.0、52.8、53.1。
3.2 Python program(slide 15)
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
machines_data = pd.read_csv("machines.csv")
machines_data['Worker'] = machines_data['Worker'].astype('category') # ID → categorical
model = smf.ols('score ~ Worker + Machine', data=machines_data).fit() # Machine 係 string,自動 categorical
print(model.summary())
3.3 讀 output(slide 16)
| 項目 | 值 |
|---|---|
| No. Observations / Df Residuals / Df Model | 54 / 46 / 7 |
| R-squared / Adj. R-squared | 0.867 / 0.847 |
| F-statistic / Prob (F) | 42.83 / 4.61e-18 |
| AIC / BIC | 284.9 / 300.8 |
| coef | std err | t | P>|t| | [0.025 | 0.975] | |
|---|---|---|---|---|---|---|
| Intercept | 53.6167 | 1.217 | 44.059 | 0.000 | 51.167 | 56.066 |
| Worker[T.2] | −2.9222 | 1.490 | −1.961 | 0.056 | −5.922 | 0.078 |
| Worker[T.3] | 5.2111 | 1.490 | 3.496 | 0.001 | 2.211 | 8.211 |
| Worker[T.4] | −1.3333 | 1.490 | −0.895 | 0.376 | −4.333 | 1.667 |
| Worker[T.5] | 1.8111 | 1.490 | 1.215 | 0.231 | −1.189 | 4.811 |
| Worker[T.6] | −10.3333 | 1.490 | −6.933 | 0.000 | −13.333 | −7.333 |
| Machine[T.B] | 7.9667 | 1.054 | 7.559 | 0.000 | 5.845 | 10.088 |
| Machine[T.C] | 13.9167 | 1.054 | 13.205 | 0.000 | 11.795 | 16.038 |
Slide 16 用紅框圈住 Worker[T.2] … Machine[T.C]——即係 slide 13 嘅 dummy columns,T. = Treatment coding(同 baseline 比較)。
- Intercept 53.6167:baseline 組合(Worker 1 用 Machine A)嘅預測 score。
- Machine[T.B] 7.9667:同一個 worker(holding worker constant),用 Machine B 比 Machine A 平均高 7.97 分; → 顯著。
- Machine[T.C] 13.9167:Machine C 比 A 平均高 13.92 分;C vs B:。
- Worker[T.6] −10.3333:喺同一部機上,Worker 6 平均比 Worker 1 低 10.33 分(顯著,CI 唔包 0)。
- Worker[T.2] −2.9222,:喺 5% level,冇足夠證據話 Worker 2 同 Worker 1 唔同(CI 包 0)。T.4、T.5 亦唔顯著。
- : ✔
Worker 3 用 Machine C:。
Worker 6 用 Machine B:。
Worker 1 用 Machine A:只有 intercept = 53.6167。
Machine 嘅 effect 對所有 workers 一樣(例如 C 永遠比 A 高 13.92,唔理邊個 worker)——即係冇 interaction。如果某啲 worker 特別擅長某部機,就要加 Worker:Machine interaction(見 §5.3)。
4. Polynomials 同 transformations(slides 17–22)
Slide 17:multiple linear regression 假設 linear relationship,但 linear relationship does not always hold。呢個時候可以加原數據嘅 squares、cubes……
Slide 18:用教科書 Python package ISLP 入面嘅 dataset “Auto”。If a transformed value is used, the formula need to be bracketed to avoid confusion:
model = smf.ols('y ~ x + I(x**2)', data=data).fit() # smf 可以用 string formula 指定 model
# Auto 例子(ISLP):
# from ISLP import load_data
# Auto = load_data('Auto')
# model = smf.ols('mpg ~ horsepower + I(horsepower**2)', data=Auto).fit()
“Linear” 指 model 對 parameters 係線性,唔係對 X。將 當成另一個 predictor ,就係普通 multiple linear regression,OLS / 照用。
X 嘅 effect 唔再係常數:。 → 向上彎(U 形); → 向下彎(∩ 形),轉折點喺 。例如(假設數字): 時 Y 最高; 時 X 越大 Y 越大, 之後反而下降。所以唔好單獨解讀 。
4.1 Formula syntax(slides 19–21)
model = smf.ols(formula="y ~ x1 + x2", data=df).fit() # y = dependent;x1, x2 = independent
| Operator | Meaning | Example | Equivalent expansion / result |
|---|---|---|---|
~ | 分隔 dependent(左)同 independent variables(右) | y ~ x1 | |
+ | Add variable | y ~ x1 + x2 | 包括 同 |
- | Remove variable or intercept | y ~ x1 - 1 | 包括 ,without an intercept |
: | Interaction term | y ~ x1:x2 | 只有 product |
* | Main effects and interactions | y ~ x1*x2 | y ~ x1 + x2 + x1:x2 |
Slide 20:I() function 叫 formula processor 將括號入面嘅 operators 當成普通數學運算,唔係 formula operators:
# 包括 x1、x2 同 x2 嘅平方
model = smf.ols(formula="y ~ x1 + x2 + I(x2**2)", data=df).fit()
喺 formula 入面 ** 係 formula operator(interaction 嘅次方),x**2 會被理解成 x 同自己嘅 interaction = 就係 x。我喺 statsmodels 0.15 試過:'y ~ x + x**2' 只 fit 到 Intercept 同 x,冇報錯、冇 。同樣 y ~ x1 + x2 唔等於 y ~ I(x1 + x2):前者兩個 predictors,後者一個(兩者之和)。
Slide 20 Pro-Tip:fit 高次 polynomial(cubic、quartic)寫好多個 I() 好麻煩,可以用 poly() 生成 orthogonal polynomials:
# 自動生成 x2 嘅 linear 同 quadratic terms
model = smf.ols(formula="y ~ x1 + poly(x2, degree=2, raw=True)", data=df).fit()
我喺 statsmodels 0.15(預設用 patsy 處理 formula)試 poly(x, degree=2, raw=True),出 NameError: name 'poly' is not defined。如果你部機都係咁,就照用 I(x**2) + I(x**3)。另外 raw=True 係直接用 ;raw=False 先係 orthogonal polynomials。
C():text strings 或者包喺C()入面嘅 variables 自動當 categorical(one-hot encoded),first alphabetical category dropped as the baseline intercept。例:y ~ C(group);machines 例子可以寫C(Worker)代替astype('category')。- NumPy / custom functions:可以直接喺 formula 用 vectorized functions,例:
np.log(y) ~ x1 + np.sqrt(x2)。 Q():column 名有空格或特殊字符時用,例:Q('dependent var') ~ Q('independent var')。
College 例子:Engineering < Humanity < Science(字母次序)→ Engineering 係 baseline,所以 output 係 College[T.Humanity]、College[T.Science]。Machine:A 係 baseline。想換 baseline:C(College, Treatment('Science')) → 得到 Intercept 58.8、Engineering −2.8、Humanity 2.45(我實際跑過)。
4.2 Beyond polynomials(slide 22)
| Non-linear model | Transform 成 linear | smf formula |
|---|---|---|
| (exponential) | np.log(y) ~ x | |
| (power) | np.log(y) ~ np.log(x) | |
| (reciprocal) | 本身已經對 a、b 線性,predictor 係 | y ~ I(1/x) |
(Slide 寫 ,嗰度嘅 a 其實係 transform 後嘅 intercept 。)
:X 加 1,Y 大約變 (b 細嗰陣)。:X 加 1%,Y 大約變 ——即 elasticity。Log Y 亦常用嚟處理 Module 4 提過嘅 heteroskedasticity(漏斗形 residuals)。注意 log 只適用於正數。
5. Interaction terms(slides 23–28)
5.1 咩係 interaction(slide 23)
數學上,A 同 B 嘅 interaction term 就係 AB(相乘)。Slide 23 用打印機做例子:
單獨一部 printer(冇墨)或者單獨一盒 ink(冇機)價值都好低,合埋先有用——complementary effect。合埋多出嚟嗰部分(the extra)就係 interaction。
| 冇 interaction | 有 interaction | |
|---|---|---|
| Model | ||
| A 對 Y 嘅 effect | (固定,唔理 B 係幾多) | (隨 B 改變) |
| Effect 點樣加埋 | Additive:各自貢獻相加 | 合埋有額外(,complementary)或者抵消() |
。
:A 每加 1,Y 加 2。:A 每加 1,Y 加 。
A = 1、B = 1 時:;如果只係 additive(冇 )就係 15——多出嘅 4 就係 interaction(printer + ink 嘅 “extra”)。
5.2 Continuous × continuous(slide 24)
# 同樣用 ISLP 嘅 Auto dataset;Python 入面 interaction term 寫 A:B
model = smf.ols('y~A+B+A:B', data=data).fit()
# 等同 smf.ols('y~A*B', data=data).fit()
# Auto:smf.ols('mpg ~ horsepower + weight + horsepower:weight', data=Auto).fit()
直覺:horsepower 對 mileage 嘅影響,可能取決於架車有幾重—— 量度呢個「取決於」。Slides 冇提供呢個 model 嘅 output,所以呢度唔列數字。
加 interaction 時通常要保留 main effects(用 A*B 或 A+B+A:B),唔好淨係寫 y ~ A:B——後者只有 product 一項,等於假設 A、B 各自冇 effect。有 interaction 時, 只代表 時 A 嘅 effect,唔可以再講「A 每加 1,Y 變 」。
5.3 Categorical × categorical(slides 25–27)
Slide 25:用 statsmodels.formula.api,
- A 有 3 categories(Eng / Sci / Hum)→ 2 個 dummy variables()
- B 有 4 categories(Year 1 / 2 / 3 / 4)→ 3 個 dummy variables()
- Interaction terms(最少) 6:
- Python output 命名:例如 會顯示成
A[T.S]:B[T.2](slide 寫 “T.S T.2”)
A 有 個 categories、B 有 個:
College × Year:。Machines(Worker × Machine):——即每個 worker-machine 組合有自己嘅平均。每個組合有 3 次重複,所以 residual df = 。
Slides 26–27:同一個 machines data 加 interaction——即係問「Machine 嘅 effect 係咪對每個 worker 都一樣?」
machines_data = pd.read_csv("machines.csv")
# Approach 1:先轉 category,再明確寫 main effects + interaction
machines_data['Worker'] = machines_data['Worker'].astype('category')
model = smf.ols('score ~ Worker + Machine + Worker:Machine', data=machines_data).fit()
model.summary()
# Approach 2:用 C() 同 *,一行搞掂(結果一樣)
model = smf.ols('score ~ C(Worker)*Machine', data=machines_data).fit()
因為每個 worker 喺每部機都做咗 3 次。如果每個組合只得 1 個 observation,18 個 parameters fit 18 個數據點會 perfect fit(residual df = 0),冇辦法估計 error,亦做唔到 test。Slides 冇提供 interaction model 嘅 output。
5.4 Higher-order interactions(slide 28)
y ~ A*B*C # All terms:A + B + C + A:B + A:C + B:C + A:B:C
y ~ A:B:C # Only ABC(只有三者相乘一項)
| 層次 | A, B, C 嘅例子 | 數目 |
|---|---|---|
| Main effects | A, B, C | 3 |
| Second-order interactions | AB, AC, BC | 3 |
| Third-order interactions | ABC | 1 |
| Higher-order interactions | (4 個或以上 variables 相乘) | less important |
Terms 數目增長得好快(categorical 仲要乘 dummy 數),parameters 多咗 → Module 4 講嘅 overfitting、要好多數據;而且三個以上 variables 嘅 interaction 好難解讀。實務上通常只考慮 main effects 同 second-order interactions,再用 AIC / BIC / out-of-sample RMSE 決定留唔留。
自我測驗 Self-test
Q1. Variable “Region” 有 North / East / South / West / Central 5 個 categories。Regression(有 intercept)要幾多個 dummy variables?如果用 one-hot encoding 全部放入去會點?
個。放晒 5 個 + intercept:5 個 dummies 每行加埋都係 1,同 intercept column 一樣 → perfect multicollinearity(dummy variable trap), 冇 inverse,OLS 冇唯一解。Python C(Region) 會自動 drop 第一個 alphabetical category(Central)做 baseline。
Q2. (baseline = Group A)。計三組嘅預測值,並解讀 。B 同 C 差幾多?
A = 70;B = 66;C = 76。:Group B 平均比 baseline Group A 低 4 單位。C − B = 。
Q3. 數據:Group P:10, 14;Group Q:20, 22, 24;Group R:5, 7。用 y ~ C(group) fit,寫出 fitted equation。
組平均:P = 12、Q = 22、R = 6。Baseline = P(字母最前)。。
Q4. 用 machines output(slide 16):預測 Worker 5 用 Machine C 嘅 score;Worker 3 同 Worker 6 用同一部機,差幾多?
Worker 5, C:。Worker 3 − Worker 6 = (冇 interaction,所以任何一部機都係呢個差)。
Q5. Worker[T.2] 嘅 coef = −2.9222,,95% CI 。用 寫結論。
(Worker 2 同 Worker 1 喺同一部機嘅平均 score 一樣)。,CI 包 0 → do not reject :喺 5% level 冇足夠證據話 Worker 2 同 Worker 1 有分別。
Q6. 點解 Worker(數值 1–6)要 astype('category')?唔做會 fit 咗咩 model?
Worker 號碼只係 ID label,冇大小意義。唔轉嘅話會當 numerical,只 fit 一個 slope:,即假設 worker 號碼每加 1,score 線性變 ——毫無意義,亦只用 1 個 parameter 而唔係 5 個。
Q7. 以下 formula 各自 fit 咗咩 terms?(a) y ~ x + x**2 (b) y ~ x + I(x**2) (c) y ~ a*b (d) y ~ a:b - 1
(a) Intercept、x——** 被當 formula operator,冇 !(b) Intercept、x、。(c) Intercept、a、b、a:b。(d) 只有 a × b,冇 intercept。
Q8. 。B = 0 同 B = 4 時,A 加 1 單位 Y 變幾多?呢個 interaction 係 complementary 定係抵消?
A 嘅 effect = 。B = 0:+1.5;B = 4:。:B 越大,A 嘅正面 effect 越細甚至反轉——抵消(唔係 complementary)。
Q9. A = Faculty(4 categories),B = Gender(2),C = Mode(Full-time / Part-time)。y ~ A*B*C 有幾多個 parameters(包括 intercept)?
Dummies:A 3、B 1、C 1。Main:;second-order:,共 7;third-order:。總數 。
Q10. 寫出 嘅 linearised form 同 smf formula。如果 fit 出 slope = 0.8,點解讀?
;smf.ols('np.log(y) ~ np.log(x)', data=df).fit()。:X 增加 1%,Y 大約增加 0.8%(elasticity)。
一頁總結 Cheat sheet
- Categorical X 唔可以直接乘 → dummy variables;k categories → dummies(2→1、3→2、7→6);全 0 嗰個 = baseline。
- One-hot(DM,
pd.get_dummies)= k columns;regression 有 intercept 時放 k 個 → dummy variable trap( singular)。drop_first=True→ 。 - Good format:所有 Y 喺同一 column(long format,
melt())。 - :Eng 56、Hum 61.25、Sci 58.8;intercept = baseline 平均,dummy coef = 同 baseline 嘅差(= 組平均差)。
- Python:
astype('category')或C(var);baseline = first alphabetical;C(x, Treatment('B'))換 baseline;output 名Var[T.level]。 - Machines:
score ~ Worker + Machine,8 params,df 46,;Intercept 53.6167(W1, A);B +7.9667、C +13.9167;W6 −10.3333;W2 唔顯著。 - Polynomial:,對 仍然 linear;
I(x**2)(唔加 I() 會靜靜雞冇咗 );effect = 。 - Operators:
~分 Y/X;+加;-1冇 intercept;:只有 product;*= main + interaction;I()數學運算;Q()特殊 column 名;np.log()可直接用。 - Transform:;; 用
I(1/x)。 - Interaction = AB;complementary effect(printer + ink);;保留 main effects。
- Categorical interaction: terms(3×4 → 6);總 params = ;
Worker + Machine + Worker:Machine≡C(Worker)*Machine。 y~A*B*C= 3 main + 3 second-order + 1 third-order;y~A:B:C只有 ABC;higher-order 較唔重要。