Skip to content

Instantly share code, notes, and snippets.

View pietersv's full-sized avatar

Pieter Sheth-Voss pietersv

View GitHub Profile
@pietersv
pietersv / refactorgrid.js
Created November 17, 2023 19:22
Refactor a grid question inferred from SPSS metadata
protobi.refactorGrid = function(key,ref) {
let dim = protobi.getDimension(key);
let result = {}
let refDim = ref ? this.getDimension(ref) : null
let refKids = refDim?.getChildren()
_.each( dim.getChildren(), function(kid, kidIdx) {
_.each( kid.getChildren(), function(gkid, gkidIdx) {
let token1 = ': '
let token2 = ' - '
let token3 = ' - '
@pietersv
pietersv / gist:fefd28b46f5ce24b249280f4986ecdcf
Created November 6, 2023 21:50
Selectively change set element to weighted/unweighted depending on currently local or global crosstab banner element
/**
* This code will set selected elements to unweighted if crosstabbed by particular banner elements.
*/
{
//TL, 2023-11-02
//This code is from the QUNIT: Car sales demo
//Add code that unweighs data when Specialty is used as a crosstab
let BANNER_THAT_SHALL_BE_UNWEIGHTED = 'quotaquestion'
SELECT session_id, statement_id, node_name, LEFT(query,100), query_start, user_name, is_executing FROM v_monitor.query_profiles WHERE is_executing = 't';
SELECT INTERRUPT_STATEment('_protobi_db_node0001-412069:0xc0c1e9',1);
SELECT
REGEXP_SUBSTR(column_name,'^[a-zA-Z]*',1) AS base,
REGEXP_SUBSTR(data_type,'^[a-zA-Z]*',1) AS type,
COUNT(*)
FROM v_catalog.columns
WHERE table_schema = 'myschema' AND table_name = 'mytable'
GROUP BY 1,2
ORDER BY 1
// --------------------------------------------------
// This enables the generation of a XLSX pivot table
// with several restrictions
//
// Last updated: 2023-07-18-13:18
// --------------------------------------------------
// -------------------------------------------------------------------------------------
@pietersv
pietersv / fmtPercent.js
Created May 31, 2022 17:57
Alternative function to display fractions as percents
// this method will display null values as n-dashes, and round anything below 5 down,
// so that 0.22499999999999998 yields 22% not 23%
protobi.widgetFactory.views.BasicElement.prototype.fmtPercent = function (val) {
return (val == null || isNaN(val)) ? '—' : ((val * 100).toFixed(protobi.globals.precisionPercent || 0)) + (protobi.globals.showPercentSymbol === false ? '' : '%');
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@pietersv
pietersv / us-state-names.tsv
Created April 27, 2021 02:16 — forked from amartone/us-state-names.tsv
US State Names
id code name
01 AL Alabama
02 AK Alaska
04 AZ Arizona
05 AR Arkansas
06 CA California
08 CO Colorado
09 CT Connecticut
10 DE Delaware
11 DC District of Columbia
@pietersv
pietersv / ptlookup.md
Created July 2, 2020 16:45
Memoized functions to quckly convert z-value and t-values to tables

Memoized functions to quckly convert z-value and t-values to tables

t-value to p-value

    // memoized table of p values for t values, for df < 30 and t values between 0.1 and 5
    // the first index is the number of drgr
    var _ttable = [null,{"-2.3":0.532,"-1.6":0.563,"-1.2":0.593,"-0.9":0.621,"-0.7":0.648,"-0.5":0.672,"-0.4":0.694,"-0.2":0.715,"-0.1":0.733,"-0.0":0.75,"0.1":0.765,"0.2":0.779,"0.3":0.803,"0.4":0.813,"0.5":0.831,"0.6":0.846,"0.7":0.859,"0.8":0.869,"0.9":0.879,"1.0":0.891,"1.1":0.901,"1.2":0.909,"1.3":0.918,"1.4":0.926,"1.5":0.933,"1.6":0.937},{"-2.3":0.535,"-1.6":0.57,"-1.2":0.604,"-0.9":0.636,"-0.7":0.667,"-0.5":0.695,"-0.4":0.722,"-0.2":0.746,"-0.1":0.768,"-0.0":0.789,"0.1":0.807,"0.2":0.823,"0.3":0.852,"0.4":0.864,"0.5":0.884,"0.6":0.901,"0.7":0.915,"0.8":0.926,"0.9":0.935,"1.0":0.946,"1.1":0.955,"1.2":0.962,"1.3":0.969,"1.4":0.974,"1.5":0.979,"1.6":0.981},{"-2.3":0.537,"-1.6":0.573,"-1.2":0.608,"-0.9":0.642,"-0.7":0.674,"-0.5":0.705,"-0.4":0.733,"-0.2":0.759,"-0.1":0.783,"
@pietersv
pietersv / rim_weights.md
Last active June 12, 2020 12:44
Calculate rim weights

Rim weighting algorithm

The function below calculates a single respondent-level weight variable such that when applied as a weight, the marginal distribution of multiple elements yields the desired proportions. This can be useful to ensure that the results of a survey sample reflect the population distribution.

Algorithms

Add this function within a Protobi data process:

/**
 * Calculate a single respondent-level  weight such that when weighting is applied 
 * multiple variables yield the desired marginal distribution