Using linear regression for prediction

Once we've determined the coefficients of a linear regression model, we can use these coefficients to predict the value of the dependent variable of the model. The predicted value is defined by the linear regression model as the sum of the products of each coefficient and the value of its corresponding independent variable.

We can easily define the following generic function, which when supplied with the coefficients and values of independent variables, predicts the value of the dependent variable for a given formulated linear regression model:

(defn predict [coefs X]
  {:pre [(= (count coefs)
            (+ 1 (count X)))]}
  (let [X-with-1 (conj X 1)
        products (map * coefs X-with-1)]
    (reduce + products)))

In the preceding function, we use a precondition to assert the number of coefficients and the values of independent variables. This function expects that the number of values of the independent variables is one less than the number of coefficients of the model, as we add an extra parameter to represent an independent variable whose value is always 1. The function then calculates the product of the corresponding coefficients and the values of the independent variables using the map function, and then calculates the sum of these product terms using the reduce function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.216.175