Skip to content

Instantly share code, notes, and snippets.

View eduardszoecs's full-sized avatar

Eduard Szöcs eduardszoecs

View GitHub Profile
<base href="{% if root_url != '' %}{{ root_url }}{% else %}/{% endif %}" />
require(plyr)
require(reshape2)
require(MASS)
require(ggplot2)
### Power
### Simulate data
# no of simulated datasets
nsims <- 100
# sample sizes
### Type1 Error
require(plyr)
require(reshape2)
require(MASS)
require(ggplot2)
nsims <- 100
# sample sizes
N <- c(3,6,9,12)
ctrl <- 2^(1:10)
# both as grid
@eduardszoecs
eduardszoecs / plt.R
Created December 3, 2015 18:41
plot data & model
plt <- function(df, mod){
# predict only for range in site
sites <- levels(df$site)
value_0p <- c(sapply(sites, function(y) seq(min(df$value_0[df$site == y]),
max(df$value_0[df$site == y]),
length.out = 333)))
# use the mean for init
pdat <- data.frame(value_0 = value_0p,
site = rep(sites, each = 333),
init = mean(df$init)
@eduardszoecs
eduardszoecs / derivSimulCI.R
Created July 27, 2016 13:58 — forked from gavinsimpson/derivSimulCI.R
First derivatives and simultaneous confidence intervals for GAM spline terms
`derivSimulCI` <- function(mod, n = 200, eps = 1e-7, newdata, term,
samples = 10000) {
stopifnot(require("MASS"))
if(inherits(mod, "gamm"))
mod <- mod$gam
m.terms <- attr(terms(mod), "term.labels")
if(missing(newdata)) {
newD <- sapply(model.frame(mod)[, m.terms, drop = FALSE],
function(x) seq(min(x), max(x) - (2*eps), length = n))
names(newD) <- m.terms
@eduardszoecs
eduardszoecs / Deriv.R
Created July 27, 2016 13:58 — forked from gavinsimpson/Deriv.R
R functions to compute and plot the first derivative of a spline term in a GAM(M) fitted by `gam()` or `gamm()` in package mgcv
################################################
## Functions for derivatives of GAM(M) models ##
################################################
Deriv <- function(mod, n = 200, eps = 1e-7, newdata, term) {
if(inherits(mod, "gamm"))
mod <- mod$gam
m.terms <- attr(terms(mod), "term.labels")
if(missing(newdata)) {
newD <- sapply(model.frame(mod)[, m.terms, drop = FALSE],
function(x) seq(min(x), max(x), length = n))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# get data ----------------------------------------------------------------
library(Ecdat)
data("CRANpackages")
df <- rbind(CRANpackages,
data.frame(Version = c("3.2", "3.3") ,
Date = as.Date(c("2016-04-27", "2016-12-22")),
Packages = c(8329, 9687),
Source = c("Andrie de Vries", "Eduard Szöcs")
)
# plain sample
foo1 <- function(x, n){
s <- sample(c(-1, 1), n, replace = TRUE)
min(which(cumsum(s) == 0))
}
# addaptive sample length
foo2 <- function(nmax, step = 10000, start = 0) {
s <- sample(c(-1, 1), step, replace = TRUE)
library(tidyverse)
## Something ------------------------------------