<jupyter_start><jupyter_text>Small Assignment 1 Solutions  Part 1. We are going to explore the nuts and bolts of regressions by doing one by hand. *Whether you are using* **R** *or excel, please do not use canned functions to calculate sample variances and covaraiances and to estimate regression coefficients.*Start by reading in the `energy.csv` file.<jupyter_code>#Read in the data 
df <-read.csv('energy.csv', header=T)<jupyter_output><empty_output><jupyter_text>**(a)** Create two new variables, GDP (\$) per capita and energy use (KT oil equivalent) per capita. *Hint: use the* `mutate` *function.*<jupyter_code>#Load tidyverse
library(tidyverse)
#Generate per capita variables
df <- mutate(df, gdp_pc= gdp/pop, energy_pc=energy_use/pop)
#head(df)<jupyter_output>Warning message:
"As of rlang 0.4.0, dplyr must be at least version 0.8.0.
* dplyr 0.7.8 is too old for rlang 0.4.5.
* Please update dplyr to the latest version.
* Updating packages on Windows requires precautions:
  <https://github.com/jennybc/what-they-forgot/issues/62>"
-- [1mAttaching packages[22m --------------------------------------- tidyverse 1.2.1 --

[32mv[39m [34mggplot2[39m 3.1.0     [32mv[39m [34mpurrr  [39m 0.2.5
[32mv[39m [34mtibble [39m 2.0.1     [32mv[39m [34mdplyr  [39m 0.7.8
[32mv[39m [34mtidyr  [39m 0.8.2     [32mv[39m [34mstringr[39m 1.3.1
[32mv[39m [34mreadr  [39m 1.3.1     [32mv[39m [34mforcats[39m 0.3.0

-- [1mConflicts[22m ------------------------------------------ tidyverse_conflicts() --
[31mx[39m [34mdplyr[39m::[32mfilter()[39m masks [34mstats[39m::filter()
[31mx[39m [34mdplyr[39m::[32mlag()[39m    masks [34mstats[39m::lag()

Warning message:
"The `printer` argument is deprecated as of rlang 0.3.0.
[90mThi[...]<jupyter_text>**(b)** Generate a scatter plot comparing energy use per capita (on the y-axis) to GDP per capita (on the x-axis) for the countries in your sample. *Hint: Use the* `plot` *function.*<jupyter_code>#Simple scatterplot
plot(df$gdp_pc, df$energy_pc, xlab='GDP per capita', ylab='Energy use per capita')<jupyter_output><empty_output><jupyter_text>**(c)** Calculate the sample mean of GDP per capita, sample mean of energy per capita,covariance of GDP per capita and energy use per capita, and variance of GDP percapita.<jupyter_code>#Get means
xbar <-mean(df$gdp_pc)
ybar <-mean(df$energy_pc)


#Create new columns with residuals (x_i-\bar{x}) and (y_i-\bar{y}) as part of df 
df <- mutate(df, xres= gdp_pc-xbar, yres= energy_pc-ybar) 
#Create new columns with _i*y_i and x_i^2
df <- mutate(df, xy= xres*yres, xx=xres^2)

#Get covariance and variance by summing up xy and xx and dividing by n-1. 
covxy<- sum(df$xy)/(nrow(df)-1)
varx<- sum(df$xx)/(nrow(df)-1)

#Display results
c(xbar, ybar, covxy, varx) #note the use of c to create a vector!<jupyter_output><empty_output><jupyter_text>**(d)** Suppose $y_i$ is energy use per capita in country $i$ and $x_i$ is GDP per capita in country $i$. Suppose your statistical model is $$y_i=\beta_0+\beta_1x_i+u_i.$$ Estimate $\hat{\beta}_0$ and $\hat{\beta}_1$ using your results in part **(c)**.<jupyter_code>#Solve for \hat\beta_1 and \hat\beta_0
b1hat<- covxy/varx
b0hat<- ybar-xbar*b1hat
#Display
c(b0hat,b1hat)<jupyter_output><empty_output><jupyter_text>**(e)** Plot a line which indicates the predictions from your estimated model, $$\hat{y}_i=\hat{\beta}_0+\hat{\beta}_1x_i.$$ *Hint: use the* `curve` *function.*<jupyter_code>#plot the line
curve(b0hat+b1hat*x, min(df$gdp_pc), max(df$gdp_pc), xlab='GDP per capita', ylab = 'Energy use per capita')
#overlay with data points
points(df$gdp_pc,df$energy_pc)<jupyter_output><empty_output><jupyter_text>**(f)** Interpret your estimates $\hat{\beta}_0$ and $\hat{\beta}_1$. $\hat\beta_0=$0.00003 means that the model predicts that a country with 0 GDP would have energy use equivalent to .0003 KT of oil. $\hat\beta_1=$1.9e-07 means that model predicts that each additional dollar of GDP leads to 1.9e-07 addition KT of oil equivalent energy use.   Part 2.Suppose you estimate a weekly wage equation which suggests $$ \log(wage_i)=5.45+.09*Ed_i+u_i$$ where $Ed_i$ is the number of years of education completed by person $i$. **(a)** Predict the wage for a person with 12 years of education (that is a person who completes high school).<jupyter_code>exp(5.45+.09*12)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1628172879030414, 'fraction_numerical': 0.04779327692659501, 'mean_word_length': 4.747700394218134, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '482918', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1579, 'n_words': 483, 'path': 'Small Assignments/Small Assignment 1/Small Assignment 1 (w. solutions).ipynb', 'repo_name': 'ds-modules/ENVECON-118-FA20', 'size': '4373'}