library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(infer)
library(statsr)
## Loading required package: BayesFactor
## Loading required package: coda
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## ************
## Welcome to BayesFactor 0.9.12-4.4. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
## 
## Attaching package: 'statsr'
## 
## The following object is masked from 'package:infer':
## 
##     rep_sample_n
## 
## The following objects are masked from 'package:openintro':
## 
##     calc_streak, evals, nycflights, present
data('hfi', package='openintro')

Exercise 1

What are the dimensions of the dataset?

dim(hfi)
## [1] 1458  123

Exercise 2

What type of plot would you use to display the relationship between the personal freedom score, pf_score, and one of the other numerical variables? Plot this relationship using the variable pf_expression_control as the predictor. Does the relationship look linear? If you knew a country’s pf_expression_control, or its score out of 10, with 0 being the most, of political pressures and controls on media content, would you be comfortable using a linear model to predict the personal freedom score?

Looking at the scatterplot, I would say the relationship looks linear and thus a linear model can predict the personal freedom score.

hfi %>%
  summarise(cor(pf_expression_control, pf_score, use = "complete.obs"))
plot(hfi$pf_score ~ hfi$pf_expression_control, 
     xlab = "Expression control", ylab = "Pf score")

Exercise 3

Looking at your plot from the previous exercise, describe the relationship between these two variables. Make sure to discuss the form, direction, and strength of the relationship as well as any unusual observations.

hfi1 <- hfi[c("pf_score", "pf_expression_control")] %>% 
   filter(!is.na(pf_expression_control)|!is.na(pf_score))

# Drop NAs
hfi1 <- drop_na(hfi1)
row.names <- NULL
DATA606::plot_ss(x = hfi1$pf_expression_control, y = hfi1$pf_score)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##      4.6171       0.4914  
## 
## Sum of Squares:  952.153

Practically, there are some massive outliers, but for the most part it appears to be a positive linear relationship. The strength appears to be high, as they are very tightly correlated.

Exercise 4

Using plot_ss, choose a line that does a good job of minimizing the sum of squares. Run the function several times. What was the smallest sum of squares that you got? How does it compare to your neighbors?

plot_ss(x = pf_expression_control, y = pf_score, data = hfi1)

## Click two points to make a line.
                                
## Call:
## lm(formula = y ~ x, data = pts)
## 
## Coefficients:
## (Intercept)            x  
##      4.6171       0.4914  
## 
## Sum of Squares:  952.153

The Smallest sum of square I received was 952!

Exercise 5

Fit a new model that uses pf_expression_control to predict hf_score, or the total human freedom score. Using the estimates from the R output, write the equation of the regression line. What does the slope tell us in the context of the relationship between human freedom and the amount of political pressure on media content?

reverse <- lm(hf_score ~ pf_expression_control, data = hfi)
summary(reverse)
## 
## Call:
## lm(formula = hf_score ~ pf_expression_control, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6198 -0.4908  0.1031  0.4703  2.2933 
## 
## Coefficients:
##                       Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           5.153687   0.046070  111.87   <2e-16 ***
## pf_expression_control 0.349862   0.008067   43.37   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.667 on 1376 degrees of freedom
##   (80 observations deleted due to missingness)
## Multiple R-squared:  0.5775, Adjusted R-squared:  0.5772 
## F-statistic:  1881 on 1 and 1376 DF,  p-value: < 2.2e-16

The slope tells us that human freedom and the amount of political pressure are directly related! The Equation is “4.61707 + 0.349862 * (pf_expression_control)

Exercise 6

If someone saw the least squares regression line and not the actual data, how would they predict a country’s personal freedom school for one with a 6.7 rating for pf_expression_control? Is this an overestimate or an underestimate, and by how much? In other words, what is the residual for this prediction?

target <- 6.7

result <- 4.61707 + 0.349862 * target
result
## [1] 6.961145
hfi %>%
  group_by(pf_score) %>%
  filter(pf_expression_control > 6.50) %>%
  filter(pf_expression_control < 7.00)

Seeing as there no exact 6.7 value, lets use a close value from Guyana

Residual <- 6.954978    - result
Residual
## [1] -0.0061674

The prediction overestimated by 0.0062.

Exercise 7

Is there any apparent pattern in the residuals plot? What does this indicate about the linearity of the relationship between the two variables?

m1 <- lm(pf_score ~ pf_expression_control, data = hfi)

ggplot(data = m1, aes(x = .fitted, y = .resid)) +
  geom_point() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  xlab("Fitted values") +
  ylab("Residuals")

ggplot(data = m1, aes(x = .resid)) +
  geom_histogram(binwidth = 0.5) +
  xlab("Residuals")

ggplot(data = m1, aes(sample = .resid)) +
  stat_qq()

There appears to be no pattern in the residuals, so its likely its a linear relationship.

Exercise 8

Based on the histogram and the normal probability plot, does the nearly normal residuals condition appear to be met?

Yes, the chart demonsratres that the distrubutions are nearly normal.

Exercise 9

Based on the residuals vs. fitted plot, does the constant variability condition appear to be met?

The residuals vs. fitted plot shows the points scattered around zero, appearing to meet the variability condition!

Exercise 10

Choose another freedom variable and a variable you think would strongly correlate with it.. Produce a scatterplot of the two variables and fit a linear model. At a glance, does there seem to be a linear relationship?

ggplot(data = hfi, aes(x = year, y = hf_rank)) +
  geom_point() +
  stat_smooth(method = "lm", se = FALSE)
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 80 rows containing non-finite values (stat_smooth).
## Warning: Removed 80 rows containing missing values (geom_point).

The relationship between year and hf_rank is a positive linear relationship.

Exercise 11

How does this relationship compare to the relationship between pf_expression_control and pf_score? Use the R2 values from the two model summaries to compare. Does your independent variable seem to predict your dependent one better? Why or why not?

target <- lm(year ~ hf_rank, data = hfi)
summary(target)
## 
## Call:
## lm(formula = year ~ hf_rank, data = hfi)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3787 -2.1659 -0.0095  2.1083  4.2053 
## 
## Coefficients:
##              Estimate Std. Error   t value Pr(>|t|)    
## (Intercept) 2.012e+03  1.380e-01 14574.330  < 2e-16 ***
## hf_rank     4.171e-03  1.552e-03     2.687  0.00729 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.563 on 1376 degrees of freedom
##   (80 observations deleted due to missingness)
## Multiple R-squared:  0.005221,   Adjusted R-squared:  0.004498 
## F-statistic: 7.222 on 1 and 1376 DF,  p-value: 0.007289

So my R-Squared value is significantly lower than R-Squared value between pf_expression_control and pf_score?

Given the low correlation, my independent variable does not predict my dependent one well.

Exercise 12

What’s one freedom relationship you were most surprised about and why? Display the model diagnostics for the regression model analyzing this relationship.

legal <- lm(hfi$year ~ hfi$ef_legal_protection)

summary(legal)
## 
## Call:
## lm(formula = hfi$year ~ hfi$ef_legal_protection)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3014 -2.1748 -0.1263  1.9067  3.9571 
## 
## Coefficients:
##                           Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)             2012.35942    0.24023 8376.823   <2e-16 ***
## hfi$ef_legal_protection   -0.03408    0.04127   -0.826    0.409    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.547 on 1287 degrees of freedom
##   (169 observations deleted due to missingness)
## Multiple R-squared:  0.0005295,  Adjusted R-squared:  -0.0002471 
## F-statistic: 0.6818 on 1 and 1287 DF,  p-value: 0.4091

Honestly, I’m finding the relationships of freedom relationships and time to be rather interesting. I really like the idea that legal protections are time independent, but at the same time I was hoping that they would be improving (indicating a positive linear relationship)

This model has an R-Square value akin to my previous time based metric, which seems to indicate that freedom and time do not have a relationship.