Categorical covariates

A covariate is a a part of a Linear models that is used as a predictor for the response variable. These can be numeric covariates or categorical covariates.

Categorical covariates are different discrete categories which can be used in the model. In R, this is usually a text field in the data. If you use the as.factor() function on this field, R will know to interpret it as categorical data. This affects the way it gets plotted and treated in a linear model (using the lm() function).

Generating data in R

By using rnorm() three times

In the lecture notes, they used some R code to simulate sampling data from three distributions using three categories (urban, forest, agriculture). The response variable y was generated with:

Doing this in R:

# categorical covariates
x.raw = c("urban","urban","urban","forest","forest","forest","agriculture","agriculture","agriculture","agriculture")

# sample outcomes
y = c(rep(0,10))
# If the associated x with this y is urban, assign these three numbers
y[x.raw=="urban"] = 1 + rnorm(3,mean = 0,sd=1)
y[x.raw=="forest"] = 5 + rnorm(3,mean = 0,sd=1)
y[x.raw=="agriculture"] = 3 + rnorm(4,mean = 0,sd=1)

Matrix and vector of fixed effects

Another way to do this in a more math-y way is with matrix multiplication. I think we don't really need to worry about this too much.

Create a matrix with three columns (one for each category). In each row, set the column associated with that category to 1 and the others to zero. You'll end up with a matrix like:

X=[100100100010010010001001001001]

The columns are urban, forest and agriculture. The first three rows are urban, the next three are forest, and the last four are agriculture.

Next we define a matrix of fixed effects (the constant added to each observation): β=[β1β2β3]=[153]

We do matrix multiplication to multiply these together, which looks like:

Xβ=[X11X12X13⋮⋱⋮Xn1Xn2Xn3][β1β2β3]=[X11β1+X12β2+X13β3⋮Xn1β1+Xn2β2+Xn3β3]

That is, mutliplying an n×3 matrix (X) by a 3×1 vector (β), we end up with a n×1 vector made of the sums of each element. Since in our matrix X we only had zeros and ones, we all the other terms just go to zero. So really what happens is we just assign the associated βi value to each row in a bigger vector:

Xβ=[X11β1+X12β2+X13β3⋮Xn1β1+Xn2β2+Xn3β3]=[1β1+0β2+0β3⋮0β1+0β2+1β3]=[1⋮3]

In R

You can do this in R with:

# Matrix of "covariates"
X = matrix(0, nrow=10, ncol=3)
X[x.raw=="urban",1] = 1
X[x.raw=="forest",2] = 1
X[x.raw=="agriculture",3] = 1


# The beta (=fixed effects) vector
beta = as.vector(c(1,5,3))

# (optional) Take a look at the result of matrix multiplication:
X %*% beta
##      [,1] 
## [1,]    1 
## [2,]    1 
## [3,]    1 
## [4,]    5 
## [5,]    5 
## [6,]    5 
## [7,]    3 
## [8,]    3 
## [9,]    3 
## [10,]   3

# Looks as expected. Now if we use this with rnorm() we can generate our data:
y = X %*% beta + rnorm(10,mean = 0,sd=1)

This gives us similar data to the other way, but it's more efficient if you have more categories.

Building a model

Using the factor() function on our x.raw vector of text strings allows R to read it as a categorical variable and build a linear model:

X.factor = factor(x.raw)
  
model = lm( y ~ 1 + X.factor )
summary(model)

The results of the summary are in the lecture notes.