Skip to content

Instantly share code, notes, and snippets.

@gbluma
Last active August 29, 2015 14:15
Show Gist options
  • Save gbluma/9e9564859aeec24c3c65 to your computer and use it in GitHub Desktop.
Save gbluma/9e9564859aeec24c3c65 to your computer and use it in GitHub Desktop.
#lang racket
(require 2htdp/batch-io) ; provides read-csv-file
(require srfi/1) ; provides reduce function
(require plot) ; provides plot function
;; helper functions
(define (sum xs) (reduce + 0 xs))
(define (square x) (* x x))
; define our regression model
; x : list of numbers
; y : list of numbers
(define (linear-regression x y)
(let* ([mean_x (/ (sum x) (length x))]
[mean_y (/ (sum y) (length x))]
; Σ(x_i - mean_x)(y_i - mean_y)
[numerator (sum (map (λ(xi yi) (* (- xi mean_x) (- yi mean_y))) x y))]
; Σ(x_i - mean_x)
[denominator (sum (map (λ(xi) (square (- xi mean_x))) x))]
; beta = [Σ(x_i - mean_x)(y_i - mean_y)] / [Σ(x_i - mean_x)]
[beta (/ numerator denominator)]
; alpha = mean_y - beta * mean_x
[alpha (- mean_y (* beta mean_x))])
; return both the slope and itercept as a tuple
(list beta alpha)))
;; start working on the data -------------------------
; some more helpful names
(define sepal-length (compose string->number first))
(define sepal-width (compose string->number second))
(define (get-columns row) (list (sepal-length row)
(sepal-width row) ))
(define setosa-points (map get-columns (read-csv-file "setosa.txt")))
; save the results of our linear model
(define lm (linear-regression
(map first setosa-points)
(map second setosa-points)))
; show the results
(plot (list
; plot the data points
(points setosa-points #:color "red")
; also plot the calculated regression line
(function
(λ(x) (+ (* (first lm) x) (second lm))) ; f(x) = m * x + b where (m,b) = lm
)))
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
5.0,3.4,1.6,0.4,Iris-setosa
5.2,3.5,1.5,0.2,Iris-setosa
5.2,3.4,1.4,0.2,Iris-setosa
4.7,3.2,1.6,0.2,Iris-setosa
4.8,3.1,1.6,0.2,Iris-setosa
5.4,3.4,1.5,0.4,Iris-setosa
5.2,4.1,1.5,0.1,Iris-setosa
5.5,4.2,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.0,3.2,1.2,0.2,Iris-setosa
5.5,3.5,1.3,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
4.4,3.0,1.3,0.2,Iris-setosa
5.1,3.4,1.5,0.2,Iris-setosa
5.0,3.5,1.3,0.3,Iris-setosa
4.5,2.3,1.3,0.3,Iris-setosa
4.4,3.2,1.3,0.2,Iris-setosa
5.0,3.5,1.6,0.6,Iris-setosa
5.1,3.8,1.9,0.4,Iris-setosa
4.8,3.0,1.4,0.3,Iris-setosa
5.1,3.8,1.6,0.2,Iris-setosa
4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
5.0,3.3,1.4,0.2,Iris-setosa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment