<jupyter_start><jupyter_text>EEP/IAS 118 Confidence Intervals for Prediction Example July 30, 2019<jupyter_code>library(tidyverse)
library(haven)
library(stargazer)

sleepdata <- read_dta("sleep75.dta") %>%
    mutate(sleep_nightly = sleep/7/60)
head(sleepdata)


reg <- lm(sleep_nightly ~ educ + exper + hrwage , data = sleepdata)
summary(reg)
#stargazer(reg, type = "html")<jupyter_output>Registered S3 methods overwritten by 'ggplot2':
  method         from 
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang
── [1mAttaching packages[22m ─────────────────────────────────────── tidyverse 1.2.1 ──
[32m✔[39m [34mggplot2[39m 3.1.0     [32m✔[39m [34mpurrr  [39m 0.3.2
[32m✔[39m [34mtibble [39m 2.1.3     [32m✔[39m [34mdplyr  [39m 0.8.1
[32m✔[39m [34mtidyr  [39m 0.8.3     [32m✔[39m [34mstringr[39m 1.4.0
[32m✔[39m [34mreadr  [39m 1.3.1     [32m✔[39m [34mforcats[39m 0.4.0
── [1mConflicts[22m ────────────────────────────────────────── tidyverse_conflicts() ──
[31m✖[39m [34mdplyr[39m::[32mfilter()[39m masks [34mstats[39m::filter()
[31m✖[39m [34mdplyr[39m::[32mlag()[39m    masks [34mstats[39m::lag()

Please cite as: 

 <NAME> (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.2. https://CRAN.R-project.org/package=stargazer<jupyter_text>Dependent variable:sleep_nightlyeduc-0.011(0.019)exper0.004(0.004)hrwage-0.012(0.013)Constant7.880***(0.286)Observations532R20.007Adjusted R20.002Residual Std. Error1.025 (df = 528)F Statistic1.297 (df = 3; 528)Note:*p**p***p__Residual Std. Error__ reports $\frac{\sum \hat u^2_i}{n-k-1} = \frac{SSR}{n-k-1}$. To see this:<jupyter_code>resid <- reg$residuals
ssr <- sum(resid^2)
paste0("SSR = ", ssr)
var_u <- ssr/(532 - 3 - 1)
se_u <- sqrt(var_u)
paste0("sigma_hat = ", round(se_u,3))<jupyter_output><empty_output><jupyter_text>Predict Average hours slept per night when $educ = 10$, $exper = 4$, and $hrwage = 15$<jupyter_code>yhat <- reg$coefficients[1] + reg$coefficients[2]*10 + reg$coefficients[3]*4 + reg$coefficients[4]*15
paste0("Predicted Average Y when educ is 10, exper is 4, and hourly wage is 15 equals ", yhat)

sleepdata <- sleepdata %>%
    mutate(educ_adj = educ - 10,
          exper_adj = exper - 4,
          wage_adj = hrwage - 15)

reg_adj <- lm(sleep_nightly ~ educ_adj +exper_adj +wage_adj, data = sleepdata)
summary(reg_adj)<jupyter_output><empty_output><jupyter_text>You can grab the value of the residual standard error from your `lm` object using the `sigma()` function.<jupyter_code>sigma(reg_adj)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19571865443425077, 'fraction_numerical': 0.095565749235474, 'mean_word_length': 5.290865384615385, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 19, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '519074', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1077, 'n_words': 205, 'path': 'Summer-19/Simulations/Confidence Interval for Prediction.ipynb', 'repo_name': 'sberto2019/ENVECON-118', 'size': '2623'}