Skip to content

Instantly share code, notes, and snippets.

View daob's full-sized avatar

Daniel Oberski daob

  • Utrecht Unversity, The Netherlands
  • Amsterdam, The Netherlands
View GitHub Profile
library(tidyverse)
library(metamicrobiomeR)
set.seed(3421)
# Sample size (fixed!)
n_CFS <- 50
n_control <- 50
# How many metabolites to be sent for biological screening
library(tidyverse)
library(polycor) # for hetcor
set.seed(7567)
N <- 1e4 # Sample size
J <- 100 # Number of groups
ρ <- 0.7 # Within-group correlation
τ <- c(-Inf, -2, 0, 1, Inf) # Thresholds for categories
K <- length(τ) - 1 # Number of categories
# A small simulation to investigate bagging
# 2020-10-30 DLO
set.seed(4232)
# Root mean squared error metric
rmse <- function(y_pred, y) {
r <- y_pred - y
sqrt(mean(r^2))
}
@daob
daob / beta.R
Last active August 16, 2020 09:33
# Function that takes desrired mean, distance, and probability, and outputs
# another function to be optimized.
get_objective <- function(desired_mean, desired_dist, desired_mass) {
# Return a function that can be passed to optim()
function(shape1) {
# Enforce desired mean:
shape2 <- shape1 * ((1 / desired_mean) - 1)
# Use R internals to get the definite integral:
@daob
daob / oberski-satorra-lavaan.R
Created August 24, 2019 08:31
Implementation of Oberski, D. L., & Satorra, A.. (2013). Measurement error models with uncertainty about the error variance. Structural equation modeling, 20, 409-428. doi:10.1080/10705511.2013.797820. See also daob.nl/publications
# This code is a lavaan implementation of the standard error correction found in
#
# Oberski, D. L., & Satorra, A. (2013).
# Measurement error models with uncertainty about the error variance.
# Structural equation modeling, 20, 409-428.
# DOI:10.1080/10705511.2013.797820
#
# Author: Daniel Oberski
# Date 27 november 2018
# License: MIT (https://opensource.org/licenses/MIT)
@daob
daob / polca_entropy_r2.R
Created March 18, 2019 16:11
Calculate entropy R2 for poLCA model
# MIT license
# Author: Daniel Oberski
# Input: result of a poLCA model fit
# Output: entropy R^2 statistic (Vermunt & Magidson, 2013, p. 71)
# See: daob.nl/wp-content/uploads/2015/07/ESRA-course-slides.pdf
# And: https://www.statisticalinnovations.com/wp-content/uploads/LGtecnical.pdf
machine_tolerance <- sqrt(.Machine$double.eps)
entropy.R2 <- function(fit) {
library(lubridate)
library(rjson)
library(tidyverse)
library(ISOcodes)
library(ggplot2)
library(ggthemes)
setwd("~/Dropbox/ERC/templates/")
@daob
daob / bvr_polca.R
Created August 1, 2017 18:26
Calculate Bivariate Residuals (BVRs) for latent class models in R (poLCA)
# Author: Daniel Oberski
# Date: 2017-08-01
# Bivariate residual statistic for latent class analysis
# Calculate the BVR for poLCA objects
# Argument: a poLCA object
# Value: a dist object with BVRs
# Example: bvr(fit)
@daob
daob / tensorflow-latent-class-model.ipynb
Created March 4, 2016 15:53
An implementation of a finite mixture model with covariates ("latent class model") in TensorFlow
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.