library(learnr) library(learnSEM) knitr::opts_chunk$set(echo = FALSE)
This assignment is a quick supplement to Dr. Buchanan's Structural Equation Modeling course to get a judgment of your R skills. You should be able to complete this assignment without too much trouble to feel comfortable in the full SEM course. You will get plenty of coding practice, and it's ok if you need to Google some of the answers.
This course assumes you have the following skill sets:
If you need more help, here are some resources:
You can use vignette("lecture_introR", "learnSEM")
to view these notes in R.
In this next section, you will answer questions using the R code blocks provided. Be sure to use the solution
option to see the answer if you need it!
Please enter your name for submission. If you do not need to submit, just type anything you'd like in this box.
question_text( "Student Name:", answer("Your Name", correct = TRUE), incorrect = "Thanks!", try_again_button = "Modify your answer", allow_retry = TRUE )
Remember that vectors are similar to one row or one column of data. They must be all the same data type! In this section, create a vector of data using the rep
or seq
function.
# Here are some examples, there are many right answers rep(1:5, times = 4) seq(from = 1, to = 5, by = 1)
Use the concatenate function c()
to join together several values to create a vector.
# Here are some examples, there are many right answers c(1, 2, 5, 6) c("dogs", "cats", "birds", "squirrels")
First, include your vector from the last exercise. You will need to save your vector as a variable. Then, use the length()
function to figure out how long your vector is.
# Save your vector # Use the length() function
# Save your vector animals <- c("dogs", "cats", "birds", "squirrels") # Use the length() function length(animals)
Matrices are objects in R that have rows and columns. Remember, they also must be all the same type of data, like numbers or characters. Type in code that makes a matrix of 5 rows and 5 columns. Be sure to save your matrix as a variable. Then include code to subset or grab the value in row 2 column 3. This exercise helps you understanding slicing and how to grab only small parts of a set of data you are interested in.
# Save your matrix # Row 2, Column 3
# Save your matrix myMatrix <- matrix(data = 1:25, nrow = 5, ncol = 5) # Row 2, Column 3 myMatrix[2, 3]
R has several datasets that are included in packages, which we can use for learning how to work with dataframes. Dataframes are like matrices in that they have rows and columns; however, they do not have to be all the same type (each column can be a different form of data). Each column does have to have the same number of values (i.e., all columns must have the same number of rows, unlike lists).
Use the head()
function to look at the embedded USArrests
dataset. Then use the summary()
function to view the overall statistics for USArrests
.
head(USArrests) summary(USArrests)
Next, we will import data provided directly with this package. This dataset includes data from a study our research team did on typing, looking at how pleasant people rated words based on the way a word is typed on a QWERTY keyboard, which hand the participant wrote with, and more. Type in head(introR)
to see the dataframe.
library(learnSEM) data(introR)
library(learnSEM) data(introR) head(introR)
Now we will work on editing the dataset. It has a lot of bad values, so we want to clean that data for our use in the next exercise. Let's create a summary()
of the whichhand
column to first view what the issue is. Summary may not help us out if the column is a character vector, so you can also try the table()
function to view character values in the whichhand
column.
summary(introR$whichhand) table(introR$whichhand)
We can see that there are a bunch of different ways that people have listed which hand they wrote with. You should recode all the values so they are LEFT
and RIGHT
. There are many ways to handle this problem, but let's focus on slicing and subsetting. Below you can see an example of how to fix the left values. You should add code to fix the right values. The table()
function has been included so you can see if your code worked. Be sure to fill in the _____
with the dataframe name!
# Left introR$whichhand[introR$whichhand == "left"] <- "LEFT" introR$whichhand[introR$whichhand == "Left"] <- "LEFT" introR$whichhand[introR$whichhand == "right_left"] <- "BOTH" # Right # Check table(_____$whichhand)
# Left introR$whichhand[introR$whichhand == "left"] <- "LEFT" introR$whichhand[introR$whichhand == "Left"] <- "LEFT" introR$whichhand[introR$whichhand == "right_left"] <- "BOTH" # Right introR$whichhand[introR$whichhand == "right"] <- "RIGHT" introR$whichhand[introR$whichhand == "Right"] <- "RIGHT" introR$whichhand[introR$whichhand == "right hand"] <- "RIGHT" introR$whichhand[introR$whichhand == "Right."] <- "RIGHT" # Check table(introR$whichhand)
Next, let's practice using the subset()
function. Make a dataset of just the left handed participants. Use the head()
function to print out the top couple rows.
# Left introR$whichhand[introR$whichhand == "left"] <- "LEFT" introR$whichhand[introR$whichhand == "Left"] <- "LEFT" introR$whichhand[introR$whichhand == "right_left"] <- "BOTH" # Right introR$whichhand[introR$whichhand == "right"] <- "RIGHT" introR$whichhand[introR$whichhand == "Right"] <- "RIGHT" introR$whichhand[introR$whichhand == "right hand"] <- "RIGHT" introR$whichhand[introR$whichhand == "Right."] <- "RIGHT"
lefties <- subset(DF, whichhand == "LEFT") head(lefties)
Last, one more exercise on subsetting, but this time by column. First, exclude all the NA
values in your original dataset. Then, calculate a correlation table of the following variables only: LR_switch, finger_switch, rha, word_length, letter_freq
.
# Omit na values # Correlation table
# Omit na values introR_nomiss <- na.omit(introR) # Correlation table cor(introR_nomiss[ , c("LR_switch", "finger_switch", "rha", "word_length", "letter_freq")])
Last, let us figure out how to pull information directly from saved output. First, a regression of the number of finger switches (i.e., the number of times you had to switch fingers when typing a word on a QWERTY keyboard) was used to predict the pleasantness rating of a word. It's assumed that things that are easier to type (more switches) should be more pleasant to us. Be sure to fill in the original dataframe name!
Next, you should use the coef()
function to find the coefficients from the regression. The intercept is listed first, and the slope is listed second. Can you create code to just get the slope value?
# Regression output <- lm(rating ~ finger_switch, data = introR) # Get only the slope
# Regression output <- lm(rating ~ finger_switch, data = DF) # Get only the slope coef(output)[2]
On this page, you will create the submission for your instructor (if necessary). Please copy this report and submit using a Word document or paste into the text window of your submission page. Click "Generate Submission" to get your work!
encoder_logic()
encoder_ui()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.