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) | |
library(RSQLite) | |
tmp <- tibble(x = rnorm(100), | |
y = 1.8*x + rnorm(100)) | |
mod <- lm(y~x, tmp) | |
mod$fitted.values <- NULL | |
mod$residuals <- NULL | |
mod$effects <- NULL |
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) | |
library(tsibble) | |
library(feasts) | |
tmp <- tibble(id = 1:100, x = rnorm(100)) %>% | |
mutate(x = cumsum(x), | |
y = 1.2 + 2.45*lag(x, 2) + rnorm(100)) %>% | |
as_tsibble(index = id) | |
tmp %>% CCF(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
library(tidyverse) | |
l = .90 # controls the maximum probability | |
k = -0.03 # controls the steepnees of the curve | |
m = 50 # sets the midpoint of the curve | |
tmp <- tibble(x = 1:200) %>% | |
mutate(y = l / (1 + exp(k*(x - m)))) | |
ggplot(tmp) + geom_line(aes(x, y)) + | |
geom_vline(xintercept = tmp$x[round(tmp$y, 1)==.5][1], alpha = .5, color = "red") + |
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
cpred <- function(dv, pr, id=NULL, threshold=.5){ | |
df <- tibble(id, dv, pr) %>% | |
mutate(pr = ifelse(pr > threshold, 1, 0)) %>% | |
drop_na() | |
if(!is.null(id)){ | |
df <- df %>% group_by(id) | |
} | |
df <- df %>% | |
summarize(P = sum(dv), | |
N = sum(dv == 0), |
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) | |
tmp <- tibble(mod = "effect", est = 2.2, se = .75) | |
ggplot(tmp) + | |
geom_segment(aes(x = est - (2*se), | |
xend = est + (2*se), | |
y = mod, | |
yend = mod, | |
color = "gray", |
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) | |
library(broom) | |
data <- nest(mtcars, -cyl) | |
data100 <- mutate(mtcars, disp = 100) %>% nest(-cyl, .key = data100) | |
data200 <- mutate(mtcars, disp = 200) %>% nest(-cyl, .key = data200) | |
data <- full_join(data, data100, by = "cyl") | |
data <- full_join(data, data200, by = "cyl") |
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) | |
data <- tibble(id = letters[1:10], x = rnorm(10), y = rnorm(10)) | |
g1 <- ggplot(data) + geom_point(aes(x, y, color = id)) | |
# matrix rotation | |
theta <- .9 | |
rmat <- matrix(c(cos(theta), -sin(theta), |
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
# sample | |
n <- 1000 | |
x <- rnorm(n, 25, 1) | |
y <- rnorm(n, 25.4, 1) | |
# unpaired | |
t.test(y, x) |
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
tibble(x = c(0,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,0)) %>% | |
mutate(count = ave(x, cumsum(x==0), FUN=cumsum), | |
phase = cumsum(ifelse(count == 1, 1, 0)), | |
phase = ifelse(x == 0, 0, phase)) |
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) | |
# training data, model has no error | |
data <- tibble(x0 = 1, | |
x1 = rnorm(100), | |
y = 0.97 + 1.3*x1) | |
# ols model | |
summary(lm(y ~ x1, data)) |
NewerOlder