Get startedGet started for free

Categorical Predictors I

So far we have given participants money, smiled at them, but haven't said anything - a bit creepy, right?

Let's include a variable called talk. We either said something neutral ("Nice weather today"), rude ("You smell"), or polite ("You are great"). We coded these responses as 1, 2, and 3, respectively. We have to tell R that these values (1, 2, and 3) are categories and not numbers! We do this by making them into factors using the function as.factor(). For example: as.factor(variable). This tells R that 'data' is categorical.

Let's make talk into a categorical variable, and add it to our regression analysis!

This exercise is part of the course

Inferential Statistics

View Course

Exercise instructions

  • In your script, add talk to your regression model as a predictor.
  • In your regression mode, use the function as.factor() on talk so that the model knows that the variable talk is a factor (category).
  • Hit "Submit" and look at the outcome!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Vector containing the amount of money you gave participants (predictor)
money <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

# Vector containing how much you smiled (predictor)
smile <- c(0.6, 0.7, 1.0, 0.1, 0.3, 0.1, 0.4, 0.8, 0.9, 0.2)

# Vector containing the amount the participants liked you (response)
liking <- c(2.2, 2.8, 4.5, 3.1, 8.7, 5.0, 4.5, 8.8, 9.0, 9.2)

# Vector containing what you said to participants (predictor)
talk <- c(1, 2, 3, 2, 3, 1, 2, 1, 3, 1)

# Add "talk" to your regression model as a factor predictor
mod <- lm(liking ~ smile + money)

# Obtain regression coefficients from "mod"
summary(mod)
Edit and Run Code