home.bias = function(own.home.assets, own.all.assets, others.home.assets, others.all.assets){ 1 - (1-own.home.assets/own.all.assets)/(1-others.home.assets/others.all.assets) } home.bias.subtract = function(own.home.assets, own.all.assets, others.home.assets, others.all.assets){ own.home.assets/own.all.assets - others.home.assets/others.all.assets } home.bias.permutation = function(type = "state", M = 1000, skip.biases = FALSE, category = "assets"){ if(type == "state"){ prefix = "" id.var = "state" first.colnum.of.patterns = 10 }else if(type == "committee"){ prefix = "scb." id.var = "sic.code.bunch" first.colnum.of.patterns = 11 }else if (type == "contributions"){ first.colnum.of.patterns = 11 prefix = "pia." id.var = "pac.bunch" } # get data and get rid of people with no trades a.all = read.csv(paste(type, "_full_matrix_", category, ".csv", sep = ""), header = T) # this is in summary_stats a = a.all[which(a.all$total.weight > 0), ,] # grab the names of the patterns as they appear in the columnames of a pattern.names = colnames(a)[first.colnum.of.patterns:ncol(a)] #### # we calculate the home bias by pattern (state, pac bunch, committee bunch) and member once -- what the home bias would be for this portfolio/member for each state/etc considered. biases = matrix(NA, nrow = nrow(a), ncol = length(pattern.names)) # the holder: every guy and pattern for which we counted assets. colnames(biases) = pattern.names # eg "NY", scb.1 rownames(biases) = a$name # eg. Barney Frank # biases.subtract = biases cat("Calculating the biases under the various scenarios. For", nrow(biases), "members.\n") # get holdings and biases by pattern for (i in 1:nrow(biases)){ # for each member if(i %% 10 == 0){cat(i, "")} for (j in 1:ncol(biases)){ if(skip.biases == TRUE){next} # for each column -- one per bundle of connections for which we have calculated assets own.colname = pattern.names[j] # paste(prefix, a[[id.var]][j], sep = "") # the colname of the connected pattern for this iteration -- eg. "NY", "scb.12", "pia.12" # cat("Own.colname is", own.colname, "\n") own.in = a[i, own.colname] # the value in the input for this guy on that column all.in = sum(a[, own.colname]) # the value for everyone on that column biases[i,j] = home.bias(own.in, a[i,"total.weight"], all.in - own.in, sum(as.numeric(a$total.weight[-i]))) # biases.subtract[i,j] = home.bias.subtract(own.in, a[i,"total.weight"], all.in - own.in, # sum(as.numeric(a$total.weight[-i]))) } } # this takes a little while. # now we permute the patterns n = nrow(biases) # number of guys out = matrix(NA, ncol = n, nrow = M) # storage matrix for permutations colnames(out) = a$name out.subtract = out permutation = a[[id.var]] # the column with the assignments, which we permute cat("Going through", M, "permutations.\n") for (m in 1:M){ # through permutations if (m %% 10 == 0){cat(m, " ")} if (m > 1){permutation = sample(a[[id.var]])} # we do a thought experiment of shuffling the patterns for (i in 1:n){ # cycling through guys this.column = paste(prefix, permutation[i], sep = "") # the counterfactual bundle drawn by the i'th guy in this permutation out[m,i] = biases[i,this.column] # take the bias of the i'th guy given the counterfactual bundle from this permutation. # out.subtract[m,i] = biases.subtract[i,this.column] # take the bias of the i'th guy given the counterfactual bundle from this permutation. } } print("Done, motherfucker.") bias = data.frame(id = a$id, name = a$name, bias = t(t(out[1,]))) # bias.subtract = data.frame(id = a$id, name = a$name, bias = t(t(out.subtract[1,]))) list(sims = out, type = type, M = M, bias = bias) # , bias.subtract = bias.subtract) sims.subtract = bias.subtract, } # just two columns: the member's in-group avg and everyone else's average in that group home.bias.simple = function(type = "state", M = 1000, skip.biases = FALSE, category = "assets"){ if(type == "state"){ prefix = "" id.var = "state" first.colnum.of.patterns = 10 }else if(type == "committee"){ prefix = "scb." id.var = "sic.code.bunch" first.colnum.of.patterns = 11 }else if (type == "contributions"){ first.colnum.of.patterns = 11 prefix = "pia." id.var = "pac.bunch" } # get data and get rid of people with no trades a.all = read.csv(paste(type, "_full_matrix_", category, ".csv", sep = ""), header = T) # contributions a = a.all[which(a.all$total.weight > 0), ,] # grab the names of the patterns as they appear in the columnames of a # pattern.names = as.character(a[[id.var]]) # colnames(a)[first.colnum.of.patterns:ncol(a)] # if(type == "state"){pattern.names = as.character(a$state)} #### # we calculate the home bias by pattern (state, pac bunch, committee bunch) and member once -- what the home bias would be for this portfolio/member for each state/etc considered. biases = matrix(NA, nrow = nrow(a), ncol = 4) # the holder: for every guy we get the sum in and out, for self and others colnames(biases) = c("in.self", "total.self", "in.others", "total.others") # pattern.names # eg "NY", scb.1 rownames(biases) = a$name # eg. Barney Frank cat("Calculating the in and out sums, for self and others, for", nrow(biases), "members.\n") # get holdings and biases by pattern for (i in 1:nrow(biases)){ # for each member if(i %% 10 == 0){cat(i, "")} own.colname = paste(prefix, a[[id.var]][i], sep = "") # pattern.names[i] # the colname of the connected pattern for this iteration -- eg. "NY", "scb.12", "pia.12" own.in = a[i, own.colname] # the value in the input for this guy on that column all.in = sum(a[, own.colname]) # the value for everyone on that column own.total = a[i, "total.weight"] all.total = sum(as.numeric(a$total.weight[-i])) biases[i,] = c(own.in, own.total, all.in, all.total) } biases } loc.bias = function(affiliation.index, own.holdings.index, df = a, house.inside = "state"){ # get own associations ai = affiliation.index # aff = df[affiliation.index,c(5, 7, 8)] chamber = as.character(df[ai, "chamber"]) state = as.character(df[ai, "state"]) district = as.character(df[ai, "district"]) # identify the columns for which the holdings are inside, given this affiliation index if(house.inside == "state"){ # can eventually do district bias own.inside.columns = grep(state, names(df)) } #print("got to here 1") # print(own.inside.columns) own.inside.holdings = sum(df[own.holdings.index, own.inside.columns]) own.total.holdings = sum(df[own.holdings.index, 9:ncol(df)]) #print("got to here 2") others.inside.holdings = sum(df[-own.holdings.index, own.inside.columns]) others.total.holdings = sum(df[-own.holdings.index, 9:ncol(df)]) #print("got to here") home.bias(own.inside.holdings, own.total.holdings, others.inside.holdings, others.total.holdings) }