Skip to content

Instantly share code, notes, and snippets.

@tony91782
tony91782 / prinminor2.R
Created July 18, 2011 12:47
A Better Way to Compute All Diagonal Principal Minors
library(gregmisc)
minor2 = function(mat, idx){
little_mat = mat[idx,idx]
return(det(little_mat))
}
index_vec = combinations(nrow(mat),2)
minors = apply(index_vec, MARGIN=1, FUN=function(ix){ minor2(mat,ix)})
@tony91782
tony91782 / minors2.R
Created July 18, 2011 12:20
Bad idea for computing all diagonal-preserving second order principal minors
minors = NULL
for(i in 1:(nrow(mat)-1)){
start = i +1
for(j in start:nrow(mat)){
thisminor = det(mat[c(i,j),c(i,j)])
minors = c(minors, thisminor)
}
}
latlong = aggregate(mydat[,3:4], list(BusID=mydat$BusID),
FUN=mean, na.rm=TRUE)
mycleandat = merge(mydat[,-c(3,4)], latlong)
mycleandat[mycleandat$BusID==1,]
set.seed(209)
N_firms =125
N_periods =25
BusID = 1:N_firms
period = 1:N_periods
long = runif(N_firms, -124, -68.25)
lat = runif(N_firms, 26, 47)
x11 = NULL
for(i in 1:1000){
x11 = c(trick_works(1,1),x11)
}
trick_works = function(num_times=1, num_decks=1){
deck = rep(1:13, each=4,times =num_decks)
facedeck = pmin(deck, 10)
init= 1:10
secretnum = init
stop = length(deck)*num_times
mycards = sample(facedeck, replace=FALSE) ## Shuffle the Deck
data = init
draws = 1:stop
secretnum = 1:10
stop = 150
faceval = round(floor(13*runif(stop))+0.999)
faceval = pmin(10,faceval)
data = secretnum
draws = 1:stop
for(i in draws){
secretnum = secretnum -1
set.seed(209)
myiv = iv(y~x1+x2+p, p~z1+z2, data=mktshare)
myivboot = ivboot(y~x1+x2+p, p~z1+z2, data=mktshare)
mtable(myiv, myivboot)
ivboot <- function(sec,fir,data,boots=500){
dat <- data
sec <- as.formula(sec)
fir <- as.formula(fir)
thisiv <- iv(sec, fir,data)
booter <- function(dat,i){
coef(iv(sec,fir,data=dat[i,]))
}
library(tonymisc)
library(boot)
data(mktshare)