library(haven)
Album_Sales <- read_sav("https://github.com/jvanster/Psy_300_Datasets/raw/refs/heads/main/SPSS_Datasets/Album%20Sales.sav")Correlation
Data for Correlation
Use the following code to download the dataset entitled Album Sales.
The library haven is used to import SPSS datasets, another statistical software package. The web address is to my github repository and makes it easy to download directly to your computer.
Correlation
One of the first tests we can use to look at the relationship between two variables is correlation. Correlation usually requires two continuous numerical variables of some kind.
Variance
The basic method for comparison between the two variables is variance, which we learned when looking at descriptive statistics. Remember that variance is a measurement of data dispersion or spread. More specifically it refers to the average amount that data varies from the mean. So what correlation ultimately analyses is whether 2 variables vary in a similar way from their respective means. Remember here’s the formula for variance.
\[ variance(s^2)=\frac{\Sigma(x-\bar x)^2}{N-1} \]
Covariance
Since correlation is based on how two variables vary, naturally we are interested in covariance. Covariance is simply variance for two variables. Notice here in the formula that we’ve modified the original variance formula and included our second variable y. So we’ve modified the exponent in the formula for x - the mean and incorporated y.
\[ covariance(x,y)=\frac{\Sigma(x-\bar x)(y-\bar y)}{N-1} \] x stands for our first variable and y stands for our second variable, so we are analyzing variance over two variables rather than just one. How does one variable deviate from the mean in comparison to how another variable deviates from the mean? If they deviate (vary) from the mean in a similar way they will be expected to be highly correlated. When there are two variables we can multiply the deviation for one variable by the deviation from the second variable. If both deviations are either positive or negative this will give us a positive value, which tells us that the deviations are in the same direction (positive correlation). If the deviations go in opposite directions (one positive and one negative) this will give us a negative value (negative correlation). Multiplying deviations of one variable by a second variable gets us the cross-product deviation.
At this point the covariance is dependent upon the types of units used to calculate the number. However, we want to standardize this number, which basically means that the number will be in units that are the same across different experiments and the tools they use to measure their variables. In this case to standardize our covariance we use a calculation discussed in descriptive statistics, the standard deviation.
Pearson’s r
This gets us what’s known as Pearson’s r, which was named after the person who developed the calculation, Karl Pearson with Florence Nightingale David doing a lot of the harder mathematical calculations. So here is the formula for Pearson’s r \[ r = \frac {cov_{xy}}{s_{x}s_{y}}=\frac{\Sigma(x-\bar x)(y-\bar y)}{(N-1)s_{x}s_{y}} \]
Notice that the formula is the covariance of x and y divided by the standard deviation of x and y, then this is elaborated in the longer version following the second equal sign where the standard deviation for x and y is added to our original formula for covariance.
Correlation Coefficient
Pearson’s r or the correlational coefficient always varies between +1 and -1. As your r value moves closer to +1 that means that both variables are varying in the same direction either both increasing or both decreasing. When your r value is moving closer to -1 that indicates that both scores are varying in opposite directions or as one variable increases the other decreases.
Correlation only describes relationships between variables, not causal relationships between variables. It’s not possible to decipher which variable is doing the causal work or which variable is the independent variable and which is the dependent variable. However, correlation can still provide helpful information about variables and we can still use them to test certain types of hypotheses.
Exploring Correlation in a Dataset
Let’s explore the Album Sales dataset.
head(Album_Sales)# A tibble: 6 × 4
Adverts Sales Airplay Image
<dbl> <dbl> <dbl> <dbl>
1 10.3 330 43 10
2 986. 120 28 7
3 1446. 360 35 7
4 1188. 270 33 7
5 575. 220 44 5
6 569. 170 19 5
Album Sales Dataset
This is a dataset for a record company looking at various variables related to album sales.
- Adverts - Money spent on advertising
- Sales - Sales for various albums
- Airplay - how much airplay each album received
- Image - How attractive each band was on a 1-10 scale
Next let’s look at the types of variables we have
str(Album_Sales)tibble [200 × 4] (S3: tbl_df/tbl/data.frame)
$ Adverts: num [1:200] 10.3 985.7 1445.6 1188.2 574.5 ...
..- attr(*, "label")= chr "Advertsing budget (thousands)"
..- attr(*, "format.spss")= chr "F8.2"
$ Sales : num [1:200] 330 120 360 270 220 170 70 210 200 300 ...
..- attr(*, "label")= chr "Album sales (thousands)"
..- attr(*, "format.spss")= chr "F8.0"
$ Airplay: num [1:200] 43 28 35 33 44 19 20 22 21 40 ...
..- attr(*, "label")= chr "No. of plays on radio"
..- attr(*, "format.spss")= chr "F8.0"
$ Image : num [1:200] 10 7 7 7 5 5 1 9 7 7 ...
..- attr(*, "label")= chr "Band image rating (0-10)"
..- attr(*, "format.spss")= chr "F8.0"
Notice that each of the variables are continuous, either numeric (num) or integer (int), so this is a great dataset for doing correlations.
How would you expect attractiveness of the band to be related to album sales? Unless you’ve been living under a rock or don’t know anything about rock ‘n’ roll, attractiveness is a huge part of album sales. Whether you’re Dua Lipa or BTS, looks matter if you’re in the entertainment industry. So we should expect that as an artist’s looks increase, their album sales should increase as well.
Scatterplot - Band Attractiveness & Album Sales
Let’s use a graph to first look at this data using a ggplot we learned earlier called a scatterplot. We’ll do a basic one first.
ggplot(Album_Sales, mapping = aes
(x = Image, y = Sales)) +
geom_point() +
geom_jitter()
So notice that dots lower on the attractiveness scale tend to be associated with less sales, whereas when you look at the dots higher on the attractiveness scales there are more dots that are higher on the sales scale. It’s also helpful to notice where on the scatterplot different dots are NOT located. So there are no dots located in the upper left quadrant; there are no bands who scored low on the image rating, but had high album sales.
Regression Line
Another tool to help us analyze this relationship is a regression line. This is a line drawn on the graph that is closest to as many dots or points as possible. You add it like this.
ggplot(Album_Sales, mapping = aes
(x = Image, y = Sales)) +
geom_point() +
geom_jitter() +
geom_smooth(method = 'lm', se = FALSE)`geom_smooth()` using formula = 'y ~ x'

If the line has a noticeable slope, that suggests the variables may be correlated: upward for positive, downward for negative. A flat line suggests little or no linear relationship. However, the steepness of the line doesn’t tell you how strong the correlation is, and you can’t compare steepness across graphs with different variables. Strength is shown by how tightly the dots cluster around the line, and that’s what Pearson’s r measures.
To better understand the relationship, calculate the Pearon’s r value
cor(Album_Sales$Sales, Album_Sales$Image)[1] 0.3261111
So the correlation between Sales and Attractiveness is .33, which suggests a positive correlation between Sales and Image; as the Image rating increases so does the Sales variable. This would seem to be the easiest explanation since and increase in sales wouldn’t make you more attractive. However, correlations cannot specify a causal relationship between two variables, only that the two variables are related to each other.
How to use correlations to test hypotheses
To formally use correlation to test a hypothesis, let’s look at another variable in the Album Sales dataset, airplay. This is how often a song gets played on the radio (FYI, the radio is an ancient device that old people used to listen to random music played using mysterious spooky radio waves). So your hypothesis would look something like:
There is a correlation between the airplay an album receives and the sales for the album.
The null hypothesis would simply negate the primary hypothesis:
There is no correlation between the airplay an album receives and the sales for the album.
To test the hypothesis we use the “cor.test” command and then specify each of the variables we are analyzing, so it looks like this:
cor.test(Album_Sales$Sales, Album_Sales$Airplay)
Pearson's product-moment correlation
data: Album_Sales$Sales and Album_Sales$Airplay
t = 10.524, df = 198, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.5018814 0.6810668
sample estimates:
cor
0.5989188
Then write the results using the output including the correlation coefficient, degrees of freedom (df), 95% confidence interval, and the p value.
Results
There was a significant correlation between the airplay an album received and the sales for the album, r (198) = .60, 95% CI [.50, .68], p < .001.
Let’s unpack these results a bit. The correlation is pretty high at .60. Remember that the closer a correlational coefficient is to either +/- 1 the stronger the correlation. We’ve also listed the confidence interval, which estimates the population correlation. Thus, we are 95% confident that the population correlation coefficient is between .50 and .68.
An important point here is to check to see if the CI range includes 0, which would mean that it’s possible that the correlation is zero, thus we would fail to reject our primary hypothesis. The narrower this range, the more accurate our correlation estimate and the closer to 1 or -1, the stronger the correlation. Finally, our p value is very low, so this shows us that the finding is significant.
Finally, we want to include a scatterplot graph with the regression line to show the relationship we found.
ggplot(Album_Sales, mapping = aes(x = Airplay, y = Sales)) +
geom_point() +
geom_smooth(method = 'lm', se = FALSE)`geom_smooth()` using formula = 'y ~ x'

Here’s a second version with a few more bells and whistles. I made a different color for the scatter dots by inserting a color command and changed the size to make them a bit bigger. I did something similar with the line to make it thicker by adjusting the size and removed the CIs with the “se = FALSE” command. I also added labels through the “labs” command, make sure to use quotations around your titles and x and y descriptions.
ggplot(Album_Sales, mapping = aes(x = Airplay, y = Sales)) +
geom_point(size = 1.5, color = "cornflower blue") +
geom_smooth(method = 'lm', se=FALSE, linewidth = 1.5) +
labs(title = "Scatterplot of the Relationship between Airplay
and Sales", y = "Album Sales (thousands)",
x = "Number of Plays on the Radio")`geom_smooth()` using formula = 'y ~ x'
