# please do not alter this code chunk knitr::opts_chunk$set(echo = TRUE, message = FALSE, error = TRUE) library(broom) library(tidyverse) library(reprores) # install the class package reprores to access built-in data # devtools::install_github("psyteachr/reprores-v2) # or download data from the website # https://psyteachr.github.io/reprores/data/data.zip
personality_scores
datasetLoad the dataset reprores::personality_scores.
data("personality_scores", package = "reprores")
Use ggplot2 to visualise the relationship between extraversion (Ex
) on the horizontal axis and neuroticism (Ne
) on the vertical axis.
ggplot(personality_scores, aes(x = Ex, y = Ne)) + geom_density_2d_filled(show.legend = FALSE, alpha = 0.5) + geom_smooth(method = lm, formula = y~x)
Run a regression model that predicts neuroticism from extraversion, and store the model object in the variable personality_mod
. End the block by printing out the summary of the model.
personality_mod <- lm(Ne ~ Ex, data = personality_scores) summary(personality_mod) #print out the model summary
Make a histogram of the residuals of the model using ggplot2.
residuals <- residuals(personality_mod) ggplot() + geom_histogram(aes(residuals), color = "black", binwidth = 0.25)
Write code to predict the neuroticism score for the minimum, mean, and maximum extraversion scores. Store the vector of predictions in the variable personality_pred
.
scores <- data.frame( Ex = c( min(personality_scores$Ex, na.rm = TRUE), mean(personality_scores$Ex, na.rm = TRUE), max(personality_scores$Ex, na.rm = TRUE) ), # adding the row names makes the output of predict easier to read row.names = c("min", "mean", "max") ) personality_pred <- predict(personality_mod, newdata = scores) personality_pred # print the predicted values
NOTE: You can knit this file to html to see formatted versions of the equations below (which are enclosed in $
characters); alternatively, if you find it easier, you can hover your mouse pointer over the $
in the code equations to see the formatted versions.
Write code to randomly generate 10 Y values from a simple linear regression model with an intercept of 3 and a slope of -7. Recall the form of the linear model:
$Y_i = \beta_0 + \beta_1 X_i + e_i$
The residuals ($e_i$s) are drawn from a normal distribution with mean 0 and variance $\sigma^2 = 4$, and $X$ is the vector of integer values from 1 to 10. Store the 10 observations in the variable Yi
below. (NOTE: the standard deviation is the square root of the variance, i.e. $\sigma$; rnorm()
takes the standard deviation, not the variance, as its third argument).
X <- 1:10 err <- rnorm(10, sd = 2) Yi <- 3 - 7 * X + err Yi # print the values of Yi
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.