This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First look at a linear model fit to the housing data | |
# details about dataset available http://archive.ics.uci.edu/ml/datasets/Housing | |
housing <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")[, c(6, 14)] | |
names(housing) <- c("num.rooms", "median.values") | |
housing.lm <- lm(median.values ~ num.rooms, data=housing) | |
plot(housing) | |
abline(housing.lm) | |
summary(housing.lm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of randomly chosen lines | |
plot(housing) | |
abline(0, 5, col="red") | |
abline(-50, 10, col="blue") | |
# Create the loss function | |
loss <- function(intercept, slope) sum(((intercept + (slope * housing[, "num.rooms"])) - housing[, "median.values"])^2)/2 | |
# Create some data for a given line and compute the loss | |
loss(0, 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load data and initialize values | |
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv") | |
alpha <- 0.01 | |
m <- nrow(data) | |
x <- matrix(c(rep(1,m), data$area), ncol=2) | |
y <- matrix(data$price, ncol=1) / 1000 | |
# Z-Score the feature | |
x.scaled <- x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Matrix addition | |
matrix(c(1, 2, 3, 0, 5, 1), ncol=2) + matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2) | |
# Matrix multiplication | |
3 * matrix(c(1, 2, 3, 0, 5, 1), ncol=2) | |
# Matrix-Vector Multiplication | |
matrix(c(1, 4, 2, 3, 0, 1), ncol=2) %*% c(1, 5) | |
# Matrix-Mector Multiplication |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# details about dataset available http://archive.ics.uci.edu/ml/datasets/Housing | |
housing <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data") | |
names(housing) <- c("CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "LSTAT", "MEDV") | |
# Subset the data for our model | |
housing <- housing[, c("CRIM", "RM", "PTRATIO", "LSTAT", "MEDV")] | |
plot(housing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load data and initialize values | |
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv") | |
num.iterations <- 1000 | |
x <- data[, c("area", "bedrooms")] | |
y <- matrix(data$price, ncol=1) / 1000 # Divide by a thousand so that numbers are in $1000's | |
# Function to standardize input values | |
zscore <- function(x, mean.val=NA) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv") | |
x <- as.matrix(cbind(intercept=rep(1, m), data[, c("area", "bedrooms")])) | |
theta <- solve(t(x) %*% x) %*% t(x) %*% y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot the sigmoid function | |
library(ggplot2) | |
qplot(-10:10, 1/(1 + exp(-(-10:10))), geom="line", xlab="z", ylab="sigmoid function") | |
# Download South African heart disease data | |
sa.heart <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1) | |
# Pretty plot | |
pairs(sa.heart[1:9],pch=21,bg=c("red","green")[factor(sa.heart$chd)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num.iterations <- 1000 | |
# Download South African heart disease data | |
sa.heart <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data", sep=",",head=T,row.names=1) | |
x <- sa.heart[,c("age", "ldl")] | |
y <- sa.heart$chd | |
plot(x, pch=21, bg=c("red","green")[factor(y)]) | |
# Function to standardize input values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot the data | |
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)]) | |
# Use linear discriminant analysis | |
iris.lda <- lda(Species ~ ., data = iris) | |
summary(iris.lda) | |
# Use a multinomial logistic regression model | |
library(VGAM) | |
iris.vglm <- glm(Species ~ , family=multinomial, data=iris) |
OlderNewer