# Understanding Linear Regressions with Least Squares

I am currently trying to understand the linear regression fit by least squares for my machine learning homework, where I implement it and have to plot the result:

I have given two data sets, containing each a matrix $X$ and a vector $y$.

The goal is to predict the output $\hat Y$ for arbitrary data, isn't it? For this

In order to compute optimal parameters $\beta$ which will be used as coefficients:

$$\hat Y = \beta_0 + \sum_{j = 1}^p (X_j \cdot \beta_j)$$

For an easier computation I add the constant variable $1$ into each row of the matrix $X$, this way the prediction vector $\hat Y$can be calculated by:

$\hat Y = X^T \beta$

This way I compute $\beta = (X^T X)^{-1} X^T y$

Have I understand this correctly, $y$ is the training data?

For this exercise I have to generate a test data along a grid and collect it in a matrix $Z$. Then I should compute the prediction vector by $\hat y = Z \beta$. This works fine, but now I should plot it.

Here is my question, what, respectively how is this data plotted?

(I have 1-dim test data $\rightarrow X^{100 \times 1}$ and 2-dim test data $\rightarrow X^{100 \times 2}$)

My guesses so far:

1. The function I received through this method $f(x) = \beta_0 + x \cdot \beta_1$

2. The generated test data $Z$ and the prediction vector, but how is this plotted? How do you plot 1-dim data? What are exactly the $(x,y)$ points I should plot?

I tried for the coordinates, $x$ from the test data matrix $Z$ and $y$ from the prediction vector. This way all points will be put along the function $f(x)$ I don't know whether this is right, I would have thought that the points are scattered around the line.

-
Some examples: here, here and here. –  dtldarek Apr 22 '12 at 11:12