Skip to content

Instantly share code, notes, and snippets.

View EconometricsBySimulation's full-sized avatar

Francis Smart EconometricsBySimulation

View GitHub Profile
@EconometricsBySimulation
EconometricsBySimulation / ebola.R
Last active August 29, 2015 14:06
Analysis of Ebola data
# First let's load some libraries
require('XML'); require('reshape2'); require('ggplot2')
require('magrittr') # See
# http://goo.gl/Wj5F87
# I have borrowed Andrie's code from stackoverflow
# http://goo.gl/noYVo7
source("http://goo.gl/w64gfp")
# Load in the google spreadsheet
@EconometricsBySimulation
EconometricsBySimulation / readGoogleSheet.R
Last active August 29, 2015 14:06
Read Google Spreadsheet into R
# This set of functions comes entirely form Andrie on stackoverflow
# http://stackoverflow.com/questions/22873602/importing-data-into-r-from-google-spreadsheet
readGoogleSheet <- function(url, na.string="", header=TRUE){
stopifnot(require(XML))
# Suppress warnings because Google docs seems to have incomplete final line
suppressWarnings({
doc <- paste(readLines(url), collapse=" ")
})
if(nchar(doc) == 0) stop("No content found")
@EconometricsBySimulation
EconometricsBySimulation / gist:d0af8273f15b6ccb85a4
Last active September 12, 2023 04:23
Rapidly sample from arbitrary pdf
I recently found myself in need of a function to sample randomly from an arbitrarily defined probability density function. An excellent post by Quantitations shows how to accomplish this using some of Rs fairly sophisticated functional approximation tools such as integrate and uniroot. The only problem with this excellent post was that the machine cost was enormous with samples of 1000 draws taking 10 seconds on my machine and repeated samples of 100,000+ draws (which I was after) clearly being unworkable.
Thus I decided to take my own crack at it. First let us review the basics of drawing random variables from non-uniform distributions. The standard method I think most algorithms use works as follows:
Assumptions
1. You can draw pseudo-random uniform variable u
2. You can integrate the pdf to construct a cdf
$$p = F(x) = \int_{-\infty}^\infty f(x) dx$$
3. You can invert the cdf in order to solve for p
$$G(F(x))=F^{-1}(F(x))=F^{-1}(p)=x$$
@EconometricsBySimulation
EconometricsBySimulation / gist:0b54b14d3040d3e89709
Created July 20, 2014 02:33
Graphical Anlaysis of Trends in Name Data
require(plyr)
require(ggplot2)
require(scales)
# Download data from:
# http://www.ssa.gov/oact/babynames/names.zip
setwd("C:/Data/SS-names/")
files<-list.files()
files<-files[grepl(".txt",files)]
<NotepadPlus>
<UserLang name="Julia" ext="jl">
<Settings>
<Global caseIgnored="no" />
<TreatAsSymbol comment="no" commentLine="yes" />
<Prefix words1="no" words2="no" words3="no" words4="no" />
</Settings>
<KeywordLists>
<Keywords name="Delimiters">&quot;00&quot;00</Keywords>
<Keywords name="Folder+">begin function type for while if</Keywords>
@EconometricsBySimulation
EconometricsBySimulation / gist:9958775
Created April 3, 2014 17:20
List of Stata packages/commands
_grmedf,_gstd01,actest,addest,addtxt,adjust,adodev,adoedit,adolist,adoupdate,aipe,akdensity,allpossible,alt,amcmc,an16_1,anovacontrast,anovalator,apc,apcd,apch,areastack,arimafit,array,art,ashell,atkplot,atsinst,attrib,avplot3,backrasch,backup,bayesmlogit,bcii,bcoeff,betafit,betaprior,bic,bicdrop1,binary_mediation,bincoverage,biplotvlab,birthsim,blist,blogit2,bmjcip,bpmedian,bronch,bs4rw,bspline,bstut,bsweights,bugsdat,bxmodel,bygap,bynote,bys,bystore,byvar,calibest,canred,catdev,catenate,cb2html,ccinter,cctable,cellgr,cem,cf2,chardef,charlist,charlson,checkrob,checkvar,chidemo,china_map,china_spatdwm,ci_marg_mu,cidemo,cidemo2,cij,circular,cistat,ciw,ckvar,clarify,cleanchars,cleanlog,clrbound,clstop_lbt,clt,clustpop,clustsens,cmp,cns,cnsrsig,codci,codebook2,coefplot,cohend,collapse2,collapseunique,collin,colortrans,compado,conddens,condivreg,confirmdir,contrast,copypkg,coranal,cordemo,corrsq,corrtex,cpr,cprplots,cqiv,crib10,crib11,crib12,crib13,crib9,csgof,csinter,csjl,cstable,csvconvert,cureregr,datelist,de2
@EconometricsBySimulation
EconometricsBySimulation / gist:9757105
Created March 25, 2014 08:09
Excel summary exporter
####### Excel summary exporter #######
## Modified from Smart, Francis ##
## Jean P. Gibert, 2014 ##
lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncoll <- ncol(co)
# R - Doodling in R
# I am working on creating some functions that will be capable of creating shapes and plots that look hand drawn.
# I have made some progress in this goal.
# In that process I have also discovered that I can make some doodles that look hand drawn as well.
# In order to accomplish the goal of simulating hand drawing I want to simulate the momentum of hand writing.
@EconometricsBySimulation
EconometricsBySimulation / lmOut
Last active November 22, 2022 09:12
A simple command to grab coefficients, t-stats, p-values, f-stats, etc from a regression and export them as an easy to use spreadsheet.
lmOut <- function(res, file="test.csv", ndigit=3, writecsv=T) {
# If summary has not been run on the model then run summary
if (length(grep("summary", class(res)))==0) res <- summary(res)
co <- res$coefficients
nvar <- nrow(co)
ncol <- ncol(co)
f <- res$fstatistic
formatter <- function(x) format(round(x,ndigit),nsmall=ndigit)
@EconometricsBySimulation
EconometricsBySimulation / gist:6235945
Created August 14, 2013 21:39
2013-07-30 A bit of code showing how to do Structural Equation Modelling
clear
set obs 4000
gen id = _n
gen eta1 = rnormal()
gen eta2 = rnormal()
* Generate 5 irrelevant factors that might affect each of the