F Correlation and Regression code
Video link: https://youtu.be/8lNF6PfkS2Y
################################################################################
# Correlation and Regression
################################################################################
#Load the full dataset
data <- mtcars
#Look at the included variables
head(data)
#Load tidyverse package
library(tidyverse)
#Only keep 3 variables: mpg, hp, and wt
data1 <- data %>%
select(c(mpg, hp, wt))
#Check once more that our data is how we'd like it
head(data1)
#Load ggplot package
library(ggplot2)
#mpg-hp scatterplot
ggplot(data1, aes(x = mpg, y = hp)) +
geom_point()
#hp-wt scatterplot
ggplot(data1, aes(x = hp, y = wt)) +
geom_point()
#mpg-wt scatterplot
ggplot(data1, aes(x = mpg, y = wt)) +
geom_point()
#Creating multiple scatterplots
pairs(data1[, c("mpg", "hp", "wt")])
#Run the test
cor.test(data1$mpg, data1$hp)
#Load the Hmisc package
library(Hmisc)
#Run the test; saving out results
c <- rcorr(as.matrix(data1))
#View results
c
#Load the package
library(correlation)
#Run the correlation
correlation(data1)
###### REGRESSION ######
#Assign our data to a dataframe
df_t <- trees
#Look at the data
head(df_t)
#ggplot2 is already loaded from above, so we don't need to do it again
ggplot(data = df_t, aes(x = Height, y = Girth)) +
geom_point()
#Make the linear regression model
model <- lm(Girth ~ Height, data = df_t)
#Look at model output
model
#Ask for more model information
summary(model)