Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@timcdlucas
timcdlucas / deep_interactions.R
Created May 14, 2021 10:43
Deep interactions
library(ggplot2)
Xs <- 10
N <- 200
d <- data.frame(matrix(rnorm(Xs * N), nrow = N))
d$prod <- apply(d, 1, function(x) prod(x[1:5]))
@timcdlucas
timcdlucas / gam.R
Last active March 26, 2021 10:46
categorical vars in gam
library(mgcv)
# From the man page without categorical.
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
summary(b)
n = 100
x = runif(n)
y = 1 + 2*x + rnorm(n)
yp = predict(lm(y~x))
plot(y=yp, x=y)
abline(lm(yp~y), col="blue")
abline(a=0, b=1, col="red", lty="dashed")
set.seed(10)
d <- data.frame(x = sample(c('a', 'b'), 30, replace = T),
y = rnorm(30))
anova(lm(y~x, d))
t.test(y~x, d, var.equal = TRUE)
set.seed(10)
d <- data.frame(gender = factor(sample(c('male', 'female'), 20, replace = TRUE)),
height = factor(sample(c('tall', 'short'), 20, replace = TRUE)))
d$gender_dummy <- as.numeric(d$gender) - 1
d$height_dummy <- as.numeric(d$height) - 1
d$inter_dummy <- (as.numeric(d$gender) - 1)*(as.numeric(d$height) - 1)
library(MASS)
N <- 60
x <- rep(c('a', 'b'), N)
x_dummy <- as.numeric(x == 'a')
y <- x_dummy + rt(N, 2)
data <- data.frame(x = x, y = y)
@timcdlucas
timcdlucas / nnet_seed.R
Created April 8, 2020 10:54
Does having a weird seed mess up neural networks
library(nnet)
library(dplyr)
set.seed(20200408)
diamonds <- diamonds %>%
select(carat, price, depth, table, x, y, z) %>%
scale
@timcdlucas
timcdlucas / seeds.R
Created April 8, 2020 10:27
look at seeds
norm <- rep(NA, 1e4)
unif <- rep(NA, 1e4)
for(i in seq(1e4)){
set.seed(i)
norm[i] <- rnorm(1)
set.seed(i)
unif[i] <- runif(1)
set.seed(17022020)
# "inside" people and countries
names_in <- c('lisa', 'suzanne', 'rohan')
countries_in <- c('germany', 'austria-hungary', 'italy', 'france')
# "outside"
names_out <- c('tim', 'jen')
@timcdlucas
timcdlucas / get_preds.R
Created February 14, 2020 16:07
Get the out of sample predictions from the best set of hyperparameters
get_preds <- function(t){
stopifnot(inherits(t, 'train'))
oos <- t$pred %>% arrange(rowIndex)
row_matches <- sapply(1:length(t$bestTune), function(x) oos[, names(t$bestTune)[x]] == t$bestTune[[x]])
best_rows <- rowMeans(row_matches) == 1
d <- oos[best_rows, ]