0% found this document useful (0 votes)
5 views1 page

Lab 03 Correlation - Student Name

The document outlines Lab 3 activities focused on correlation analysis using R programming. It includes exercises for calculating correlations, visualizing data with ggplot2, and interpreting results from various datasets, including a World Happiness Report dataset. Additionally, it discusses the implications of correlation coefficients and the potential influence of confounding variables.

Uploaded by

kbmills2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Lab 03 Correlation - Student Name

The document outlines Lab 3 activities focused on correlation analysis using R programming. It includes exercises for calculating correlations, visualizing data with ggplot2, and interpreting results from various datasets, including a World Happiness Report dataset. Additionally, it discusses the implications of correlation coefficients and the potential influence of confounding variables.

Uploaded by

kbmills2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Lab 3 Correlation Bell Mills

Bell Mills
3/8/2025

Lab 3 Lab Manual Exercise


x <- c(1,3,2,5,4,6,5,8,9)
y <- c(6,5,8,7,9,7,8,10,13)
cor(x,y)

## [1] 0.76539

library(ggplot2)

# create data frame for plotting


my_df <- data.frame(x,y)

# plot it
ggplot(my_df, aes(x=x,y=y))+
geom_point()+
geom_text(aes(label = round(cor(x,y), digits=2), y=12, x=2 ))

x<-rnorm(40,0,1)
y<-rnorm(40,0,1)
conditions<-rep(c("A","B","C","D"), each=10)

all_df <- data.frame(conditions, x, y)

ggplot(all_df, aes(x=x,y=y))+
geom_point()+
facet_wrap(~conditions)

x<-rnorm(10*20,0,1)
y<-rnorm(10*20,0,1)
conditions<-rep(1:20, each=10)

all_df <- data.frame(conditions, x, y)

ggplot(all_df, aes(x=x,y=y))+
geom_point()+
geom_smooth(method=lm, se=FALSE)+
facet_wrap(~conditions)+
theme_classic()

library(data.table)
whr_data <- fread("https://raw.githubusercontent.com/CrumpLab/statisticsLab/master/data/WHR2018.csv")

library(summarytools)
view(dfSummary(whr_data))

cor(whr_data$`Freedom to make life choices`,


whr_data$`Confidence in national government`)

## [1] NA

ggplot(whr_data, aes(x=`Freedom to make life choices`,


y=`Confidence in national government`))+
geom_point()+
theme_classic()

library(dplyr)

smaller_df <- whr_data %>%


select(country,
`Freedom to make life choices`,
`Confidence in national government`) %>%
filter(!is.na(`Freedom to make life choices`),
!is.na(`Confidence in national government`))

cor(smaller_df$`Freedom to make life choices`,


smaller_df$`Confidence in national government`)

## [1] 0.4080963

# select DVs and filter for NAs

smaller_df <- whr_data %>%


select(country,
`Freedom to make life choices`,
`Confidence in national government`) %>%
filter(!is.na(`Freedom to make life choices`),
!is.na(`Confidence in national government`))

# calcualte correlation

cor(smaller_df$`Freedom to make life choices`,


smaller_df$`Confidence in national government`)

## [1] 0.4080963

# plot the data with best fit line

ggplot(smaller_df, aes(x=`Freedom to make life choices`,


y=`Confidence in national government`))+
geom_point(alpha=.5)+
geom_smooth(method=lm, se=FALSE)+
theme_classic()

# select DVs and filter for NAs

smaller_df <- whr_data %>%


select(country,
`Positive affect`,
`Negative affect`) %>%
filter(!is.na(`Positive affect`),
!is.na(`Negative affect`))

# calcualte correlation

cor(smaller_df$`Positive affect`,
smaller_df$`Negative affect`)

## [1] -0.3841123

# plot the data with best fit line

ggplot(smaller_df, aes(x=`Positive affect`,


y=`Negative affect`))+
geom_point(alpha=.5)+
geom_smooth(method=lm, se=FALSE)+
theme_classic()

Lab 3 Generalization exercises


x <- runif(n=10, min=0, max=10)
y <- runif(n=10, min=0, max=10)
cor(x,y)

## [1] 0.008574957

saved_value <- c() #make an empty variable


for (i in 1:100){
x <- runif(n=10, min=0, max=10)
y <- runif(n=10, min=0, max=10)
saved_value[i] <- cor(x,y)
}

min(saved_value)

## [1] -0.685088

max(saved_value)

## [1] 0.7341346

x <- runif(n=100, min=0, max=10)


y <- runif(n=100, min=0, max=10)
cor(x,y)

## [1] 0.001495143

saved_value <- c() #mane an empty variable


for (i in 1:100){
x <- runif(n=100, min=0, max=10)
y <- runif(n=100, min=0, max=10)
saved_value[i] <- cor(x,y)
}

min(saved_value)

## [1] -0.2945057

max(saved_value)

## [1] 0.241343

Lab 3 Written answer question


1. The correlation could have happened completely by chance, or it could have been influenced by a confounding variable.
2. The difference between r=.3 and r=.7 is that the correlation of r=.7 represents a stronger correlation in its data set compared to
the data set with a correlation of r=.3.
3. The sign of a correlation variable represents the type of relationship the variables have. If the correlation is positive, the
variables are increasing with each other, but if the correlation is negative, one variable increases as the other decreases.

You might also like