September 2015
Beginner to intermediate
608 pages
13h 43m
English
First, let's remind ourselves how we would fit a straight line using Incanter's linear-model function. We want to extract the x5 and x6 columns from the dataset and apply them (in that order: x6, the year, is our predictor variable) to the incanter.stats/linear-model function.
(defn ex-9-3 []
(let [data (d/get-dataset :longley)
model (s/linear-model (i/$ :x5 data)
(i/$ :x6 data))]
(println "R-square" (:r-square model))
(-> (c/scatter-plot (i/$ :x6 data)
(i/$ :x5 data)
:x-label "Year"
:y-label "Population")
(c/add-lines (i/$ :x6 data)
(:fitted model))
(i/view))))
;; R-square 0.9879The preceding code generates the following chart:
While the straight line is a close fit to the data—generating an R2 of over 0.98—it ...
Read now
Unlock full access