Skip to content

Instantly share code, notes, and snippets.

@druedin
druedin / plzcanton-tools.R
Created February 1, 2014 20:23
Two simple helper functions to use in conjuction with the postcode to canton converters at https://gist.github.com/ruedin/6690720. The first converts cantonal ID to their abbreviated labels, the second checks if a number is a vald Swiss postcode.
convid <- function(ID) {
# function to convert cantonal ID, v.1.0 (12 Nov 2013) didier.ruedin@wolfson.oxon.org
clabels <- c("ZH", "BE", "LU", "UR", "SZ", "OW", "NW", "GL", "ZG", "FR", "SO", "BS","BL", "SH", "AR", "AI", "SG", "GR", "AG", "TG", "TI", "VD", "VS", "NE", "GE", "JU")
id <- ifelse(is.numeric(ID), clabels[ID], match(ID, clabels))
return(id)
}
plzvalid <- function(PLZ) {
# check if a number is a valid postcode; v.1.1 (1 Feb 2013) didier.ruedin@wolfson.oxon.org
return(!is.na(plzcanton(PLZ)))
@druedin
druedin / splitworddocuments
Created April 12, 2014 20:28
This is a modified VBA script for splitting MS Word documents. The original is from http://www.extendoffice.com/documents/word/966-word-split-document-into-multiple-documents.html apparently from http://www.vbaexpress.com/kb/getarticle.php?kb_id=922 (user lucas). I am unable to determine a licence, and share the code here to highight the few cha…
Attribute VB_Name = "Module1"
Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections. Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
@druedin
druedin / fade-coefplot.R
Last active August 29, 2015 14:17
Shading Coefficient Plots in R
library(denstrip)
fade <- function(x, labels=names(coef(x)), expo=FALSE, xlab="", ylab="", bty="n", ...) {
# argument: a regression, additional arguments passed on to plot() and text()
coe <- summary(x)$coefficients[,1] # extract coefficients
cse <- summary(x)$coefficients[,2] # standard errors
len <- length(coe) # how many coefficients (without intercept)
if(expo == TRUE) { # exponential form
coe <- exp(coe)
cse <- exp(cse)
}
@druedin
druedin / gist:91d0e6d2fc45444336da
Created April 23, 2015 07:57
Solarized (Dark) syntax highlighting for Pandoc/Markdown in Notepad++ (add this to userDefineLang.xml); can't recall which source file I modified... happy to give credit!
<NotepadPlus>
<UserLang name="Markdown" ext="markdown mdown mkdn mdwn mkd md Rmd" udlVersion="2.1">
<Settings>
<Global caseIgnored="yes" allowFoldOfComments="no" foldCompact="no" forcePureLC="2" decimalSeparator="0" />
<Prefix Keywords1="yes" Keywords2="yes" Keywords3="no" Keywords4="yes" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
</Settings>
<KeywordLists>
<Keywords name="Comments">00# 00## 00### 00#### 00##### 00###### 01 02 03[ 03![ 03&lt;!-- 04] 04] 04--&gt;</Keywords>
<Keywords name="Numbers, prefix1"></Keywords>
<Keywords name="Numbers, prefix2"></Keywords>
@druedin
druedin / R
Last active October 30, 2023 21:03
That quadruple sapply()
# Hi Glen. Here it comes:
countries <- unique(country)
everything <- sapply(countries, function(cy) {
parties <- unique(party[country==cy]) # all parties
this <- sapply(parties, function(z) sapply(1990:2013, function(y) mav(as.numeric(sapply(seq(y-3,y+3), function(x) experts.raw[country==cy & party==z & year==x])))))
rownames(this) <- 1990:2013
colnames(this) <- parties
return(this)
})
print(everything )
# library
library(lmtest) # library for Breusch-Pagan
library(stargazer) # for easy comparion of regression models
# create data
dv1 = runif(15)
dv2 = runif(15)
dv3 = runif(15)
ev1 = runif(15)
ev2 = runif(15)
ev3 = as.factor(c(rep("A", 10), rep("B", 5)))