1
# but cannot handle categorical variables
my_lm <- function(explanatory_matrix, response_vec) {
  exp_mat <- as.matrix(explanatory_matrix)
  intercept <- rep(1, nrow(exp_mat))
  exp_mat <- cbind(exp_mat, intercept)
  solve(t(exp_mat) %*% exp_mat) %*% (t(exp_mat) %*% response_vec)
}

The above code will not work when there are categorical variables in the explanatory_matrix. How can I implement that?

2 Answers2

0

Here is an example for a data set with one categorical variable:

set.seed(123)

x <- 1:10
a <- 2
b <- 3

y <- a*x + b + rnorm(10)
# categorical variable
x2 <- sample(c("A", "B"), 10, replace = T)
# one-hot encoding
x2 <- as.integer(d$x2 == "A")


xm <- matrix(c(x, x2, rep(1, length(x))), ncol = 3, nrow = 10)
ym <- matrix(y, ncol = 1, nrow = 10)

beta_hat <- MASS::ginv(t(xm) %*% xm) %*% t(xm) %*% ym
beta_hat

This gives (note the order of coefficients - it matches the order of the predictor columns):

      [,1]
[1,]  1.9916754
[2,] -0.7594809
[3,]  3.2723071

which is identical to the output of lm:

d <- data.frame(x = x,
                x2 = x2,
                y = y)
lm(y ~ ., data = d)

Output

# Call:
#   lm(formula = y ~ ., data = d)
# 
# Coefficients:
#   (Intercept)            x           x2  
# 3.2723       1.9917      -0.7595  
slava-kohut
  • 4,203
  • 1
  • 7
  • 24
0

For categorical handling you should use one-hot encoding. Do something like

  formula <- dep_var ~ indep_var
  exp_mat <- model.matrix(formula, explanatory_matrix)
  solve(t(exp_mat) %*% exp_mat) %*% (t(exp_mat) %*% response_vec)

avalencia
  • 33
  • 8