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
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])) |
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
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) |
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
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") |
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
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) |
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
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) | |
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
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) |
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
library(nnet) | |
library(dplyr) | |
set.seed(20200408) | |
diamonds <- diamonds %>% | |
select(carat, price, depth, table, x, y, z) %>% | |
scale |
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
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) |
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
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') |
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
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, ] | |
NewerOlder