Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@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)
@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
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)
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)
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)
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")
@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)
@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]))