Skip to content

Instantly share code, notes, and snippets.

Avatar

Josep Espasa JosepER

View GitHub Profile
@JosepER
JosepER / benchmarking_weighted_Atkinson_functions.R
Created Sep 9, 2021
Benchmarking Weighted Atkinson functions
View benchmarking_weighted_Atkinson_functions.R
library(tidyverse)
library(magrittr)
library(bench)
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)]
end
v = v/Statistics.mean(v)
@JosepER
JosepER / julia_weighted_atkinson.R
Created Sep 9, 2021
An R function wrapping over Julia code for weighted Atkinson index
View julia_weighted_atkinson.R
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)]
@JosepER
JosepER / weighted_inequality_indicators.jl
Last active Sep 8, 2021
Weighted inequality indicators
View weighted_inequality_indicators.jl
##### Atkinson index #####
"""
atkinson(v, w, ϵ)
Computes the weighted Atkinson index given an inequality adversion parameter ϵ.
"""
function wtd_atkinson(v::Vector, w::Vector, ϵ::Real, skipmissing::Bool=false) ::Float64
if skipmissing
w = w[findall(!ismissing, v)]
@JosepER
JosepER / wtd_quantile.R
Created Aug 29, 2021
An R function wrapping over Julia's 'StatsBase.quantile()'
View wtd_quantile.R
#' 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.
View backward_string_by_word.py
import re
def backward_string_by_word(text: str) -> str:
spaces = re.findall(" +", text)
words = text.split()
if len(words) == 0:
@JosepER
JosepER / LISSY_R_code_for_example_plots
Last active Jun 15, 2020
LISSY - R code for example plots
View 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)))
View LISSY_Lorenz_curve_example.R
# Prepare session
library(rlang)
library(glue)
library(fs)
library(dplyr)
library(magrittr)
library(purrr)
library(ggplot2)
@JosepER
JosepER / gini function
Last active May 25, 2020
Compute the gini index with (optional) weights
View gini function
#' 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) {
View recode_nts_outputs.cpp
#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++){
View produce_balance_stats
# 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