This file contains hidden or 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
| # waveform | |
| library(animation) | |
| library(firatheme) # devtools::install_github(vankesteren/firatheme) | |
| n <- 20 | |
| mu <- seq(-5, 5, len = n) | |
| sg <- rgamma(20, 1.5, 0.5) | |
| xx <- seq(-10, 10, len = 1000) | |
| fills <- paste0(firaPalette(n), "88") |
This file contains hidden or 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
| # Try-out of Hastie, Tibshirani, & Friedman (ESL, 2009) p.659 | |
| # Generate high-dimensional dataset | |
| beta <- -100:100/100 | |
| X <- matrix(rnorm(100*201), 100) | |
| y <- X%*%beta + rnorm(100, 0, sqrt(crossprod(beta))) | |
| # Ridge estimates | |
| bhat_ridge <- solve(crossprod(X) + diag(rep(.1, 201)), crossprod(X, y)) |
This file contains hidden or 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
| # | |
| # Copyright (C) 2018 University of Amsterdam | |
| # | |
| # This program is free software: you can redistribute it and/or modify | |
| # it under the terms of the GNU General Public License as published by | |
| # the Free Software Foundation, either version 2 of the License, or | |
| # (at your option) any later version. | |
| # | |
| # This program is distributed in the hope that it will be useful, | |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
This file contains hidden or 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
| view_html <- function(path, height = "maximize") { | |
| tf <- tempfile(fileext = ".html") | |
| file.copy(path, tf) | |
| rstudioapi::viewer(tf, height = height) | |
| } |
This file contains hidden or 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
| pcks <- c("ISLR", "tidyverse", "haven", "readxl", "MASS", "glmnet", "splines", | |
| "class", "pROC", "rpart", "rpart.plot", "randomForest", "ca", | |
| "igraph") | |
| ip <- rownames(installed.packages()) | |
| if (!all(pcks %in% ip)) { | |
| to_install <- pcks[!pcks %in% ip] | |
| message("Package(s) missing. \n", | |
| "Please install the following packages: ", |
This file contains hidden or 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(tidyverse) | |
| lights_dat <- read_csv("https://ckan.dataplatform.nl/dataset/83402c68-1c05-4aa5-ab28-2e99d2bc2261/resource/dc10e0ac-351a-49b6-b3db-d0152c29dc02/download/paal-20180906.csv") | |
| pp <- | |
| lights_dat %>% | |
| filter(latitude > 50) %>% | |
| ggplot(aes(x = longitude, y = latitude)) + | |
| geom_point(alpha = 0.03, fill = "#FAFAAB", stroke = 0, pch = 21, size = 1.6) + | |
| geom_point(alpha = 0.8, fill = "#FAFAAB", stroke = 0, pch = 21, size = 0.2) + |
This file contains hidden or 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
| # Use the great rayshader package (www.rayshader.com) | |
| library(rayshader) | |
| # Download data | |
| zip_loc <- tempfile() | |
| download.file("http://geodata.nationaalgeoregister.nl/ahn1/extract/ahn1_100m/ahn1_100.tif.zip", | |
| zip_loc) | |
| local_tif <- raster::raster(unzip(zip_loc, "ahn_100.tif")) | |
| unlink(zip_loc) |
This file contains hidden or 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
| # Randomly generate some data | |
| S <- rWishart(1, 10, diag(10))[,,1] / 10 | |
| X <- MASS::mvrnorm(100, rep(0, 10), S) | |
| b <- runif(10, -1, 1) | |
| y <- X %*% b + rnorm(100, sd = sqrt(b %*% S %*% b)) | |
| # all the interesting regression quantities! | |
| n <- nrow(X) # sample size | |
| p <- ncol(X) # parameters in model matrix |
This file contains hidden or 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
| # convolutions | |
| convolve <- function(x, kernel) { | |
| size <- length(kernel) | |
| x_pad <- c(rep(0, size - 1), x) | |
| out <- rep(0, length(x)) | |
| for (i in 1:length(x)) { | |
| out[i] <- x_pad[i:(i + (size - 1))] %*% kernel | |
| } | |
| return(out) | |
| } |
This file contains hidden or 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
| # Gradient descent with autodiff for linear regression | |
| using Zygote | |
| # Data | |
| X = randn(1000, 10) | |
| b = (1:10) | |
| y = X * b + randn(1000) | |
| # MSE for linear model | |
| function mse(bhat) |