```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Lesson 6 \& 7

## Checking Assumptptions

**6.1 Load the following packages: `broom`, `dplyr`, `furniture` and `car`. Then import the GSS dataset.**
```{r}

```

**6.2 assign a multiple regression predicting `income06` from `educ` with `age` and `degree` as covariates to an object. Produce the summary statistics, which predictors are statistically significant?**

```{r, message=FALSE}

```

**Plot the residuals using the `plot()` function to look for violations of assumptions**

```{r}

```


Which, if any, assumptions appear to be violated? Is there evidence of any influential points?

**6.4 Because this model includes covariates, it's important to ensure that none of them are collinear, as this violates one of the assumptions of regression. We can check this by examining the VIF.**

```{r}

```

Is there evidence of multicollinearity?

**6.5 Sometimes datasets are coded with values of 98 or 99 to indicate missing data. This appears to be the case for our `educ` variable for observation 260 (as seen in the residuals plot). We can covert this point to an *NA* using the `furniture::washer()` function.**

```{r}

```

**6.6 Now that the influential point has been dealt with, let's rerun our multiple regression. Produce the summary statistics.**

```{r}

```

How has the model changed from the original?

**6.7 Plot the residuals for the new model using the `plot()` function.**

```{r}

```

Do there appear to be any obvious violations of the assumptions? Do any influential points remain?

**6.8 Finally, use the `car::vif()` function to check for multicollinearity**

```{r}

```

Did removal of the influential point change the VIF values? Is there now evidence of multicollinearity?



## Missing Data and Other Real-World Problems

**8.1. Install (if need be) and load the following packages: `mice`, `dplyr`, `rio`, `lavaan` and `furniture`. Then import the GSS dataset.**


```{r, message=FALSE}

```

**8.2. Create a subset of GSS to contain only income, education and age, assign to a new object (call it whatever you like).**

```{r}

```

**8.3. Use the furniture::tableF() function to examine the distribution of values for education. Is there anything odd there?**

```{r}

```

**8.4. Hopefully you noticed that the years of education goes from 20, to 98 and 99. It is common for survey research to code missing data as 98 and 99. We need to recode it as NA so that R doesn't treat them as real numeric values. You can do so by reassigning 98 and 99 to NA.**

`GSS2$educ[GSS2$educ==99] <- NA`

`GSS2$educ[GSS2$educ==98] <- NA`

**Do this and then run tableF() again. What shows up in it's place now?**

```{r}

```

**8.5. Now use tableF() to view the distributions of age and income, do either of these variables have missing data?**

Click [here](https://kahoot.it/) to answer in Kahoot!

```{r}

```

**8.6. Before we can decide to impute the missing values, we need to determine whether the data appear to be MAR. Use the mice::md.pattern() function to examine the patterns of missing data for your GSS object**

What does it mean for data to be missing at random? Answer in Kahoot

```{r}

```

**8.6. The pattern of missing data is shown by the blue (present) and pink (missing) squares. There is a column headed by each variable name. The left axis shows the number of observations for each pattern, the left axis shows the number of variables for which data was missing in that pattern. a) How do we interpret this missing data plot? b) Is there evidence to suggest the data are not MAR?**

a) Write a brief interpretation below, and share with a partner. Did you come up with the same intrepretation?

b) Answer in Kahoot


```{r}

```

**8.7.1. Let's try inputing the missing data using FIML. This is done with the lavaan package. Use the following code to build a model where income is predicted by age and education:**

`model <- '`

`# Regression Model`

`income06 ~ b1*age + b2*educ`

`# Variances`

`age ~~ age
educ ~~ educ`

`# Covariance/correlation`

`age ~~ educ
'`

```{r}

```

**8.7.2.Now run a SEM with missing set to 'fiml' to impute the missing data values.**

`fit <- sem(model, GSS2, missing = 'fiml', meanstructure = T, fixed.x = FALSE)`
           
```{r}

```

          
**8.7.3. Do you get an error? What assumptions does FIML rely on?**

Answer in Kahoot

```{r}

```

**8.7.4. Plot the income variable using the hist() function, does it have a normal distribution? How might we correct a skewed distribution?**

Answer in Kahoot

```{r}

```

**8.7.5. Create a new variable in your GSS object that is a log transformation of income. Try the hist() plot again, it should now look roughly normal.**

`GSS2$logincome <- log(GSS2$income)`

```{r}

```


**8.7.6. Change your model to reflect the log-transformed income variable, then re-run your sem(). This time, you should not get any errors.**

```{r}

```

**8.7.7. Obtain the summary statistics by applying the summary() function to your sem model**

```{r}

```

**8.8.1. Use the mice() function to impute the missing data using multiple imputation. You can play around with the paramaters used:**

m = the number of imputed data sets

maxit = the number of interations to compute the missing data values

method = method of imputation, pmm = predictive mean matching

`impute_GSS <- mice(GSS2, m = 5, maxit = 50, method = 'pmm', seed = 500)`

```{r}

```

**8.8.2 Assign your model to an object, and then consolidate your 5 (or however many) imputed data sets.**

`fit <- with(data = impute_GSS, exp = lm(logincome ~ age + educ))`

`combine <- pool(fit)`

```{r}

```


**8.8.3. Run the summary() function to obtain the summary statistics for your pooled model. How do the results compare to the FIML model?**

Answer in Kahoot

```{r}

```



