<jupyter_start><jupyter_text>Workbook follows Section 3.2 of [Faraway 2002](https://cran.r-project.org/doc/contrib/Faraway-PRA.pdf)Economic data taken from [the accompanying R package](https://cran.r-project.org/web/packages/faraway/)Originally from Belsley, Kuh, and Welsch (1980)<jupyter_code># global change of figure sizes
# following: http://blog.revolutionanalytics.com/2015/09/resizing-plots-in-the-r-kernel-for-jupyter-notebooks.html

library(repr)

options(repr.plot.width = 4, repr.plot.height = 3)<jupyter_output><empty_output><jupyter_text>**3.2.1. F test on all predictors.** test against null hypothesis that $\left\{\beta_i\right\} = 0$<jupyter_code>data(savings)
savings
# sr: aggregate personal saving / disposable income
# pop15(75): % of population under 15 (75)
# dpi: per-capita disposable income
# ddpi: % change in dpi
summary(savings) # summary statistics
# fitting linear model
gfit <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = savings)
summary(gfit)
# computing F-statistic and p-value 'by hand'
RSS_alt <- sum(gfit$residuals^2)
RSS_nul <- sum((savings$sr - mean(savings$sr))^2)

F_statistic <- ((RSS_nul - RSS_alt) / 4) / (RSS_alt / 45)
F_statistic
1 - pf(F_statistic, 4, 45)<jupyter_output><empty_output><jupyter_text>**3.2.2. F test on one predictors.** test against null hypothesis that one predictor $\beta_i = 0$<jupyter_code>names(savings)
g2 <- lm(sr ~ pop75 + dpi + ddpi, data = savings)
summary(g2)
# compare two models (ANOVA)
RSS_nul <- sum(g2$res^2)
F_statistic <- ((RSS_nul - RSS_alt) / 1) / (RSS_alt / 45)
RSS_nul
F_statistic
1 - pf(F_statistic, 1, 45)
# alternative method for ANOVA
anova(g2, gfit)
# can use a t-statistic for each predictor, which is
# beta_hat / SE(beta_hat)
# note that pop15 has a t-statistic of -0.46 / 0.14 ~ -3.189
# and that the corresponding probability (t-distribution, n-p df)
# is 0.0026
#
# note that, here,
# F-statistic = (t-statistic)**2
# so approaches are identical<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13938165230613278, 'fraction_numerical': 0.04004054738976178, 'mean_word_length': 4.840236686390533, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '653557', 'license': 'MIT', 'n_tokens_mistral': 826, 'n_tokens_neox': 741, 'n_words': 188, 'path': 'Faraway s3.2 - Hypothesis Testing.ipynb', 'repo_name': 'clin-projects/faraway-regression-R-workbooks', 'size': '1973'}