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) | |
library(dplyr) | |
library(tidyr) | |
plot_centroids_table <- function(kmeans_object) { | |
n_clusters <- nrow(kmeans_object$centers) | |
plot_df <- data.frame(t(kmeans_object$centers)) | |
names(plot_df) <- paste("Cluster", 1:n_clusters) | |
plot_df$feature_name <- rownames(plot_df) |
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(arules) | |
transaction2df <- function(transation_obj) { | |
if (!inherits(transation_obj, "transactions")) { | |
error_msg <- paste0( | |
"\ntransactions objects only pls\n", | |
"\ncheck your object with: class(", | |
substitute(transation_obj), | |
")" | |
) |
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
# From: https://otexts.com/fpp3/diagnostics.html#portmanteau-tests-for-autocorrelation | |
# | |
# We suggest using l = 10 for non-seasonal data and l = 2 * m for seasonal data, where m is the period of | |
# seasonality. However, the test is not good when l is large, so | |
# if these values are larger than T / 5 then use l = T / 5. | |
# Translating into R function: | |
# * n_obs - number of observations in the series (referred to as T in the text) | |
# * n_seasonal_periods - NA if non-seasonal; 4 if quarterly; 12 if monthly; etc. |
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
prettify_discretize_labs <- function(x, sep = " to ") { | |
all_labs <- levels(x) | |
split_labs <- strsplit(all_labs, ",") | |
old_options <- options(scipen = 999) | |
pretty_labs <- vapply(split_labs, function(el) { | |
num_chrs <- gsub("\\[|\\]|\\(|\\)", "", el) | |
nums <- as.numeric(num_chrs) | |
pretty_lab <- paste(nums[1], nums[2], sep = sep) | |
}, character(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
import webview | |
from dash import Dash, html | |
def run_native_dash_app(dash_app: Dash, window_title: str = None) -> None: | |
"""Run dash app as native web app | |
Use PyWebView to run a dash app as a native web app | |
* install with `pip install pywebview` | |
* project home page: https://pywebview.flowrl.com/ |
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
# install.packages('sqldf') | |
library(sqldf) | |
data("mtcars") | |
# Select all | |
sqldf("SELECT * | |
FROM mtcars") | |
# single column |
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
/* | |
A base class to create p5js sketches as React components. | |
Requires p5js: `npm install p5` | |
## Usage | |
### Extend Sketch and create a p5js sketch | |
``` |
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
import numpy as np | |
from scipy import stats | |
# --------------------------- | |
# Independent samples ------- | |
# --------------------------- | |
def cles_ind(x1, x2): | |
"""Calc common language effect size |
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
# An extension of help / `?` to work with roxygen documentation in a | |
# Pythonic / docstring style for non-packaged functions. | |
.fun_body = function(fun) { | |
#' Get body of function (including comments) as a character vector | |
#' | |
#' @param fun name of function obj to return body of | |
#' | |
#' @return character vector of function body (1 element per line) |
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
# Modified from https://www.science-emergence.com/Articles/How-to-plot-a-normal-distribution-with-matplotlib-in-python-/ | |
import numpy as np | |
import scipy.stats | |
import matplotlib.pyplot as plt | |
def plot_area(mean, std, pt1, pt2, fill): | |
plt.plot([pt1, pt1], [0.0, scipy.stats.norm.pdf(pt1, mean, std)], color='black') | |
plt.plot([pt2, pt2], [0.0, scipy.stats.norm.pdf(pt2, mean, std)], color='black') |
NewerOlder