This homework will use the data set Mentalimp, which is comprised observations from 40 individuals and has three variables. The three variables are as follows:

  1. Impair - Incorporates measures of anxiety and depression.
  2. Life - Combines the number and severity of stressful life events.
  3. SES - An index of socioeconomic status.

Problem #1: Begin by examining the three variables by creating a scatterplot matrix and comment briefly on the relationship between each pair of variables. (15 pts)

The following code is used complete this problem to bring the Companion to Applied Regression (car) program out of the library, which will be used to create a Scatterplot Matrix. We will then create the dataset variable mentalimp using the read.csv function.

The function ScatterplotMatrix is then used to create a scatterplot matrix of the three variables. First I will load the tidyverse package because it is very useful.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.2
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
mentalimp <-read.csv("C:\\Users\\tuq29185\\Downloads\\mentalimp.csv")
scatterplotMatrix(~impair+ses+life, data=mentalimp)

The data for each of these variables seems to be approximately normal, which makes sense because the number of participants in the study is high enough to meet the threshold.

The correlation between the Impair variable and the SES variable seems to be the strongest and is slightly negative.

There also seems to be a slightly positive correlation between the variables Impair and Life.

There is a weak positive correlation seen between the SES and Life variables.

Question #2: Construct a QQ Plot and a boxplot of the variable impair and briefly comment on whether the variable is normally distributed. (15 pts)

The following code will give me a qqplot and a boxplot which can be examinded to determine normaility.

qqnorm(mentalimp$impair,main = "Impair Normality",ylab = "Impair Value", xlab = "Theoretical Quantiles") 
qqline(mentalimp$impair, col = "steelblue", lwd =2)

ggplot(data = mentalimp, aes( x =impair))+ geom_boxplot(notch = TRUE) 

It can be seen from the boxplot that this data is approximately normal as each quartile is roughly equivalent, and from the qqplot that the data is approximately normal as the plots form an approximate line.

Question #3: Construct a histogram and a density plot of impair. Try three different bin widths for the histogram and comment on which ones you think show enough important attributes of the data without showing random noise. (15 pts) Hint: use option midpoints to control the bin width. For example, histogram / midpoints=18 to 42 by 5; returns a histogram range from 18 to 42 with a bin width of 5.

The following code will be used to create a histogram and density plot of the impair variable.

ggplot(data = mentalimp, aes(x = impair)) + geom_histogram(binwidth = 0.5, fill = "blue", color = "black") + labs(title = "Histogram of Impair Variable ", x = "Impair Value", y = "Frequency")

ggplot(data = mentalimp, aes(x = impair)) + geom_histogram(binwidth = 1, fill = "green", color = "red") + labs(title = "Histogram of Impair Variable ", x = "Impair Value", y = "Frequency")

ggplot(data = mentalimp, aes(x = impair)) + geom_histogram(binwidth = 3, fill = "orange", color = "purple") + labs(title = "Histogram of Impair Variable ", x = "Impair Value", y = "Frequency")

Based on my interpretation, the bin width of 3 shows the most accurate data without having large gaps. I would choose this bin width to best show the data.

The following code will be used to create a density plot for the Impair variable

ggplot(data = mentalimp, aes (x = impair)) + geom_density(alpha = 0.5) + labs (Title = "Density plot of Impair Variable", x = "Impair Value", y = "Density")