Hand Coding a Linear Model function
In yesterday’s post we developed a method for constructing a multivariate linear model with an intercept.
Today we will turn the collection of loose commands into an integrated and easy to use function
.
A small recap from yesterday. We start by loading data and assigning our variables to objects
We now construction our linear model, the fastest way of doing this is using the QR decomposition.
In R
functions are treated as objects, which means that we ascribe our function to a name, the same way we ascribed our variables to a name (e.g. x1 <- Petal.Length
).
In order to create a function, we use the construction function (a function to construct an object of the type function).
Logically, but perhaps also somewhat confusingly, this constructor function is called function()
.
Lets start with creating a function, called squaring
, that takes one numeric argument (parameter) and returns it squared.
We can also use negatives of course.
Note that in the above call we did not even specify that the input (-3
) had to be assigned to the argument a
,
this was automatically deduced by R
.
We can also construction a function that takes two arguments and multiplies them.
Note that because this function was a bit longer, I put the code on a new line.
When doing this, the code has to be enclosed in curly brackets ({
and }
).
It is now time to apply these insights to our linear model. The first step is to define our function which takes the arguments y
and X
.
We can now simply insert our sollution for beta.
We can now estimate our model.
Note that the argument (e.g. X
) and the assigned object (XI
) do not have to have the same name.
At this point we still need to manually include our incept using cbind(x1, x2, 1)
.
We can add a small piece to our code, which will allow the function to take care of this.
The first piece intercept=TRUE
creates the argument and sets the default to TRUE
.
That means that, unless me manually specify default=FALSE
, an intercept will be included.
The second piece if (intercept) X <- cbind(X, 1)
does the following.
- check if
intercept == TRUE
- if so, overwrite
X
withcbind(X, 1)
We can now use this function as follows.
That gives us a pretty complete linear model.