Skip to content

Instantly share code, notes, and snippets.

@JosepER
JosepER / benchmarking_weighted_Atkinson_functions.R
Created September 9, 2021 07:07
Benchmarking Weighted Atkinson functions
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 / wtd_atkinson.jl
Created February 13, 2022 14:41
Weighted Atkinson optimized
##### Atkinson index #####
"""
wtd_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
norm_mean!(v)
w = w/sum(w)
# Benchmarking a call to 'Inequality.gini' without weights,
# and with a vector of 1s as weights.
using BenchmarkTools
using Inequality
v = rand(1:10000, 300)
w = repeat([1], 300)
@benchmark gini($v) evals=15
v <- c(8,5,1,3,5,6,7,6,3) # income
w <- seq(0.1, 0.9, 0.1) # weights
w <- w[order(v)]
v <- sort(v)
# compute πₖ
import cppyy
import numpy as np
# 'translation' of the Python function below done by ChatGPT
cppyy.cppdef("""
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
@JosepER
JosepER / example_api_to_oecd_nas.R
Created March 8, 2023 11:32
Example of functions to get OECD National Account data
#' Download the OECD detailed National Accounts
#'
#' @description
#' Uses the API to the OECD tables to download the National Accounts.
#'
#' Downloads all variables for all years for the selected countries.
#'
#' @details
#' The OECD API takes countries with ISO3. The function uses the METIS 'country'
#' table to convert ISO2 to ISO3.
library(rlang)
library(dplyr)
it14ih <- data.frame(hid = 1:6,
hi11 = c(3601, 13190, 0, 11479, 0, 4713),
hi13 = c(973, 0, 0, 478, 815, 399),
hi12 = c(0, 0, 0, 0, 0, 842),
hicapital = c(40, 41, 0, 100, 0, 83),
hpopwgt = c(567.1, 5308.3, 666.7,
692.2, 254.2, 632.6)
library(tidyverse)
national_accounts_df <- tibble::tibble(country = c("ITA", "ITA", "ITA", "ITA", "ITA"),
variable = c("NFD41R", "NFD11P", "NFB3GR", "NFD42R", "NFD45R"),
sector = c("S14", "S14", "S14", "S14", "S14"),
year = c("2016", "2016", "2016", "2016", "2016"),
value = c(27593.2, 84325810, 227852.9, 119509.5, 1034.7))
estimates_from_microdata_df <- tibble::tibble(indicator = c("D11P", "B3GR+D41R+D42R+D45R"),
value = c(84328052, 894699.3))
library(tidyverse)
library(haven)
#' Download the OECD detailed National Accounts
#'
#' @description
#' Uses the API to the OECD tables to download the National Accounts.
#'
#' Downloads all variables for all years for the selected countries.
#'
# Summer workshop - Data preparation in R --------
# COPY TO LISSY: ----------------------------------------------------------
library(tidyverse)
library(magrittr)
# install.packages("Hmisc")