Skip to content

Instantly share code, notes, and snippets.

View Laurae2's full-sized avatar

Laurae Laurae2

View GitHub Profile
@Laurae2
Laurae2 / linear_regression.R
Last active February 24, 2017 19:02
Linear Regression demo using R
# Setting up random matrix
data <- as.matrix(data.frame(Intercept = rep(1, 6),
a = c(3, 4, 5, 6, 7, 8),
b = c(1, 4, 3, 7, 10, 12),
c = c(6, 5, 1, 9, 18, 21)))
# Setting up the (perfect) linear relationship
preds <- 2 + (data[, 2] * 2) + (data[, 3] * 3) + (data[, 4] * 4)
# Plotting data to understand what we have
@Laurae2
Laurae2 / linear_regression_gradient.R
Created February 24, 2017 14:51
Gradient Descent for linear regression demo.
# Generate random values
set.seed(11111)
x <- rnorm(n = 50)
y <- 10 * x + rnorm(n = 50)
# Add intercept
x <- cbind(1,x)
# Assume random null parameters
param <- c(0,0)
@Laurae2
Laurae2 / linear_regression_gradient_plot.R
Created February 24, 2017 15:00
Gradient Descent levelplot for linear regression
# Generate random values
set.seed(11111)
x <- rnorm(n = 50)
y <- 10 * x + rnorm(n = 50)
# Add intercept
x <- cbind(1,x)
# Assume random null parameters
param <- c(0,0)
@Laurae2
Laurae2 / polynomial_regression.R
Created February 24, 2017 19:03
Polynomial Regression demo using R
# Setting up random matrix
set.seed(11111)
data <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
preds <- 2 + (data[, 1] * 2) + (data[, 2] * 3) + (data[, 3] * 4) + (data[, 3] ^ 2) + (data[, 1] * data[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / polynomial_exploding_gradient.R
Last active February 25, 2017 12:26
Polynomial Regression with Exploding Gradient in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
y <- 2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / aic_bic.R
Created February 24, 2017 19:55
Akaike Information Criterion and Bayesian Information Criterion
lm_model <- lm(preds ~ ., data = data)
my_AIC <- AIC(lm_model)
my_AIC <- nrow(data) * (log(2 * pi) + 1 + log((sum(lm_model$residuals ^ 2) / nrow(data)))) + ((length(lm_model$coefficients) + 1) * 2)
my_BIC <- AIC(lm_model, k = log(nrow(data)))
my_BIC <- nrow(data) * (log(2 * pi) + 1 + log((sum(lm_model$residuals ^ 2) / nrow(data)))) + ((length(lm_model$coefficients) + 1) * log(nrow(data)))
@Laurae2
Laurae2 / stepwise_regression_forward.R
Created February 24, 2017 20:55
Forward Stepwise Regression in R without step/lm
# Setting up random matrix
set.seed(11111)
data <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
preds <- 2 + (data[, 1] * 2) + (data[, 2] * 3) + (data[, 3] * 4) + (data[, 3] ^ 2) + (data[, 1] * data[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / L1_regularization_regression.R
Last active February 25, 2017 20:21
L1 Regularizaion Regression example in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
y <- 2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / L2_regularization_regression.R
Last active February 25, 2017 20:20
L2 Regularizaion Regression example in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
y <- 2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / elastic_net.R
Last active February 25, 2017 20:19
Elastic Net Regularizaion example in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
y <- 2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])
# Setting up polynomial features