Skip to content

Instantly share code, notes, and snippets.

@ianjohns
ianjohns / regression.cpp
Created March 22, 2023 17:22
R Regression C++ Function
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace arma;
// [[Rcpp::export]]
arma::mat lm_rcpp(arma::mat X, arma::vec y)
{
arma::vec b_hat;
b_hat = (X.t() * X).i() * X.t() * y;
return (b_hat);
@ianjohns
ianjohns / lightGBM.R
Created June 30, 2022 17:28
lightGBM Light Gradient Boosting Machine
library(lightgbm)
library(caret)
library(fastDummies)
#load("~/R_Cars_19/Data/cars_19.Rdata")
title <- "light gbm"
tmp <- cars_19[, c(4, 6, 7, 9:12)]
tmp1 <- dummy_cols(tmp)
tmp1 <- tmp1[,8:36]
d <- data.frame(cars_19[, c(1:3, 5, 8)], tmp1)
@ianjohns
ianjohns / xgboost.R
Created November 29, 2020 18:05
R XGBoost Regression
#cars_19 data set
#extreme gradient boosting
#raw data
#https://www.fueleconomy.gov/feg/epadata/19data.zip
library(xgboost)
library(caret)
library(dummies)
library(DiagrammeR)
@ianjohns
ianjohns / neural_network_tensorFlow.R
Created September 22, 2019 17:30
R TensorFlow Deep Neural Network
#cars_19 data set
#neural network with 2 hidden layers (7 neurons and 3 neurons)
#raw data
#https://www.fueleconomy.gov/feg/epadata/19data.zip
library(tfestimators)
library(caret)
#load("~/R_Cars_19/Data/cars_19.Rdata")
@ianjohns
ianjohns / neural_network.R
Created September 7, 2019 20:46
R neuralnet package
#cars_19 data set
#neural network with 2 hidden layers (7 neurons and 3 neurons)
#raw data
#https://www.fueleconomy.gov/feg/epadata/19data.zip
library(neuralnet)
library(caret)
#load("~/R_Cars_19/Data/cars_19.Rdata")
@ianjohns
ianjohns / r_tensorflow_mlr.R
Created August 28, 2019 04:27
R TensorFlow Multiple Linear Regression
library(tfestimators)
library(caret)
#input_fn for a given subset of data
cars_19_input_fn <- function(data, num_epochs = 1) {
input_fn(
data,
features = colnames(cars_19[c(2:12)]),
response = "fuel_economy_combined",
batch_size = 64,
@ianjohns
ianjohns / svm.R
Last active July 23, 2022 20:49
Support Vector Machine
#cars_19 data set
#support vector machine regression
#raw data
#https://www.fueleconomy.gov/feg/epadata/19data.zip
library(e1071)
library(caret)
r <- function(data) {round(data, 1)}
#raw data
# https://www.fueleconomy.gov/feg/epadata/19data.zip
library(gbm)
library(caret)
r <- function(data) {round(data, 1)}
set.seed(123)
indices <- sample(nrow(cars_19), size = .75 * nrow(cars_19))
@ianjohns
ianjohns / random_forest.R
Last active June 12, 2019 21:54
Random Forest Predicting MPG for 2019 Vehichles
#raw data
# https://www.fueleconomy.gov/feg/epadata/19data.zip
library(rpart) # regression trees
library(rpart.plot) # plotting regression trees
library(ipred) # bagging
library(caret) # bagging
library(e1071)
library(randomForest) #random forests
@ianjohns
ianjohns / birthday.R
Created March 25, 2019 22:17
R: Birthday Problem
# n= amount of people in room
# assumes non leap year
# returns PDF of n
birthday <- function(n) {
return(p <- 1 - choose(365, 365 - n) * factorial(n) / 365 ^ n)
}