Skip to content

Instantly share code, notes, and snippets.

Mode <- function(x, tie = c("missing", "error"), ignore_categories = NULL) {
if(!is.null(ignore_categories)){
x <- x[!(x %in% ignore_categories)]
}
# produce balance statistics -------
## version to pass unquoted
balance_stats_var <- function(data_, treatment_, variable_){
treatment_ <- enquo(treatment_)
variable_ <- enquo(variable_)
temp_means <- group_by(data_, !!treatment_) %>% ## make it possible to change the 'treatment' var
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector recode_outcomes(IntegerVector x) {
int n = x.size();
IntegerVector y(n);
for(int i=0; i<n; i++){
@JosepER
JosepER / gini function
Last active May 25, 2020 14:57
Compute the gini index with (optional) weights
#' Compute gini index.
#'
#' Compute the gini index with (optional) weights.
#'
#' @param x A numeric vector with the data.
#' @param weights A numeric vector with sample weights.
#' @return res An atomic vector with the gini index.
gini <- function(x, weights = NULL, na.rm = FALSE) {
# Prepare session
library(rlang)
library(glue)
library(fs)
library(dplyr)
library(magrittr)
library(purrr)
library(ggplot2)
@JosepER
JosepER / LISSY_R_code_for_example_plots
Last active June 15, 2020 19:51
LISSY - R code for example plots
library(readr)
library(dplyr)
library(magrittr)
library(purrr)
library(ggplot2)
all_lissyrtools_scripts <- fs::dir_ls("/media/user/lissyrtools/")
invisible(purrr::map(all_lissyrtools_scripts, ~ source(.x)))
import re
def backward_string_by_word(text: str) -> str:
spaces = re.findall(" +", text)
words = text.split()
if len(words) == 0:
@JosepER
JosepER / wtd_quantile.R
Created August 29, 2021 16:13
An R function wrapping over Julia's 'StatsBase.quantile()'
#' Weighted quantiles
#'
#' Wrap over Julia's 'StatsBase.quantile()' function.
#'
#' @param x A numeric vector for which the quantiles should be computed.
#' @param weights A numeric vector with the weights.
#' @param probs A numeric vector with the quantiles to compute.
#' @param na.rm a logical value indicating whether NA values should be stripped before the computation proceeds.
#'
#' @return A numeric vector with the weighted quantiles.
@JosepER
JosepER / weighted_inequality_indicators.jl
Last active February 13, 2022 14:39
Weighted inequality indicators
##### Atkinson index #####
"""
atkinson(v, w, ϵ)
Computes the weighted Atkinson index given an inequality adversion parameter ϵ.
"""
function wtd_atkinson(v::Array{<:Real,1}, w::Array{<:Real,1}, ϵ::Real) ::Float64
v = v/Statistics.mean(v)
w = w/sum(w)
@JosepER
JosepER / julia_weighted_atkinson.R
Created September 9, 2021 07:04
An R function wrapping over Julia code for weighted Atkinson index
weighted_atkinson_jl <- function(x, weights, epsilon, na.rm = FALSE){
JuliaCall::julia_call("weighted_atkinson", x, weights, epsilon, na.rm)
}
julia_weighted_atkinson_function <- JuliaCall::julia_eval("function weighted_atkinson(v, w, ϵ::Real, skipmissing::Bool)
if skipmissing
w = w[findall(!ismissing, v)]
v = v[findall(!ismissing, v)]