Um blog sobre nada

Um conjunto de inutilidades que podem vir a ser úteis

Using Matrix Multiplication to calculate the Intercept and the Slope

Posted by Diego em Fevereiro 3, 2015


 

A different way to calculate the Intercept and slope of a function is to use Matrix Multiplication. Let’s try that with the dataset defined here. It’s a very simple dataset with one prediction (X) and one outcome (Y) where we know, from this post, that the Intercept is -1.336847 and the slope is 2.065414.

 

The first step is to derive 1 matrix and 1 vector from our data (which we will call X and Y). The matrix, X, will contain a column full of 1s and a column for each one of the predictions, in this example we only have 1, so it will be a 20 X 2 matrix (20 is the number of observations we have). The vector, Y, will contains only the outcomes, so it will be a 20 X 1 ”matrix”. Here is the R code to load them:

X = matrix(c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20), nrow=20, ncol=2)


Y = matrix(c(5,6,7,8,9,10,11,12,13,14,15,20,25,33,34,35,36,37,38,39), nrow=20, ncol=1)


X
[,1] [,2]
[1,]    1    1
[2,]    1    2
[3,]    1    3
[4,]    1    4
[5,]    1    5
[6,]    1    6
[7,]    1    7
[8,]    1    8
[9,]    1    9
[10,]    1   10
[11,]    1   11
[12,]    1   12
[13,]    1   13
[14,]    1   14
[15,]    1   15
[16,]    1   16
[17,]    1   17
[18,]    1   18
[19,]    1   19
[20,]    1   20

Y
[,1]
[1,]    5
[2,]    6
[3,]    7
[4,]    8
[5,]    9
[6,]   10
[7,]   11
[8,]   12
[9,]   13
[10,]   14
[11,]   15
[12,]   20
[13,]   25
[14,]   33
[15,]   34
[16,]   35
[17,]   36
[18,]   37
[19,]   38
[20,]   39

 

                Once we have those two objects, all we have to do is apply the formula bellow, which for the sake of simplicity, I did in 4 steps:

clip_image002

 

Multiply X by its transpose (the function “t” in R calculates the transpose of a matrix – Some matrix Algebra in R here):

A = t(X) %*% X


Calculate the inverse of A using the function “solve”:

B = solve(A)

 

Multiply the result by the transpose of X

C = B %*% t(X)

 

Multiply the result by the vector Y:

D = C %*% Y

 

The result will be a matrix containing the intercept and all the slopes:

D
          [,1]
[1,] -1.336842
[2,]  2.065414

 

 

 

On a slightly more complex example, let’s use the mtcars R dataset predict mpg using number of cylinders, horsepower and weight. First of all let’s run the regular lm function to see if we can match the values:

 

Library(mtcars)

lm(mpg ~ cyl + hp + wt, mtcars)

 

Call:

lm(formula = mpg ~ cyl + hp + wt, data = mtcars)

 

Coefficients:

(Intercept)          cyl           hp           wt 

   38.75179     -0.94162     -0.01804     -3.16697

 

So the function we are looking for is

y = 38.75179 -0.94162cyl – 0.01804hp -3.16697wt

 

#we are going to need the dplyr library to call the mutate function

library(dplyr)

#1)Create a copy of the mtcars dataset

aux = mtcars

#2)Extract just the columns we want:

aux = aux[,c("cyl","hp","wt")]

#3)Add a column with “one”

aux = mutate(aux, i=1)

#4)Convert into a matrix

X = data.matrix(aux[,c("i","cyl","hp","wt")])

5)Get our Y column from mtcars – which is the predicted values

Y = data.matrix(mtcars[,c("mpg")])

 

#6) At last, we can call the above formula in one go:

(solve(t(X) %*% X) %*% t(X)) %*% Y

          [,1]

i   38.7517874

cyl -0.9416168

hp  -0.0180381

wt  -3.1669731

 

And we can check that the values are the ones expected.

Deixe uma Resposta

Preencha os seus detalhes abaixo ou clique num ícone para iniciar sessão:

Logótipo da WordPress.com

Está a comentar usando a sua conta WordPress.com Terminar Sessão / Alterar )

Imagem do Twitter

Está a comentar usando a sua conta Twitter Terminar Sessão / Alterar )

Facebook photo

Está a comentar usando a sua conta Facebook Terminar Sessão / Alterar )

Google+ photo

Está a comentar usando a sua conta Google+ Terminar Sessão / Alterar )

Connecting to %s

 
%d bloggers like this: