#needs bs_gr read from csv, sh  24.2.2019
bs_gr<-read.csv(file="C:/Users/simon/Dropbox/PCP/paper_EF_NG_SH/data_BS/Mitglieder_GR_BS_activities.csv")
summary(bs_gr)
names(bs_gr)
bs_gr$id<-NA
bs_gr$id<-NA
(act_2017+act_2016-act_2013-act_2012)
as.year(paste(bs_gr$BirthDate))
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>0)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
}
bs_gr$id

bs_gr$tl<-0
bs_gr$cont<-0
bs_gr$tl[c(grep("atici",bs_gr$Name,ignore.case=T),
grep("benz",bs_gr$Name,ignore.case=T),
grep("casagrande",bs_gr$Name,ignore.case=T),
grep("cramer",bs_gr$Name,ignore.case=T),
grep("hafner",bs_gr$Name,ignore.case=T),
grep("e_haller",bs_gr$Name,ignore.case=T),
grep("inglin",bs_gr$Name,ignore.case=T),
grep("konig",bs_gr$Name,ignore.case=T),
grep("rutschmann",bs_gr$Name,ignore.case=T),
grep("tobit",bs_gr$Name,ignore.case=T),
grep("ungricht",bs_gr$Name,ignore.case=T),
grep("vitelli",bs_gr$Name,ignore.case=T))]<-1

bs_gr$cont[c(
grep("ckermann",bs_gr$Name,ignore.case=T),
grep("atici",bs_gr$Name,ignore.case=T),
grep("auderset",bs_gr$Name,ignore.case=T),
grep("benz",bs_gr$Name,ignore.case=T),
grep("ernasconi",bs_gr$Name,ignore.case=T),
grep("ertschi",bs_gr$Name,ignore.case=T),
grep("ochsler",bs_gr$Name,ignore.case=T),
grep("urckhardt",bs_gr$Name,ignore.case=T),
grep("rigger",bs_gr$Name,ignore.case=T),
grep("amlibel",bs_gr$Name,ignore.case=T),
grep("casagrande",bs_gr$Name,ignore.case=T),
grep("cramer",bs_gr$Name,ignore.case=T),
grep("rdogan",bs_gr$Name,ignore.case=T),
grep("ymann",bs_gr$Name,ignore.case=T),
grep("allacchi",bs_gr$Name,ignore.case=T),
grep("gander",bs_gr$Name,ignore.case=T),
grep("oepfert",bs_gr$Name,ignore.case=T),
grep("reuter",bs_gr$Name,ignore.case=T),
grep("roflin",bs_gr$Name,ignore.case=T),
grep("rossenbacher",bs_gr$Name,ignore.case=T),
grep("hafner",bs_gr$Name,ignore.case=T),
grep("e_haller",bs_gr$Name,ignore.case=T),
grep("hofer",bs_gr$Name,ignore.case=T),
grep("inglin",bs_gr$Name,ignore.case=T),
grep("koechlin",bs_gr$Name,ignore.case=T),
grep("kaufmann",bs_gr$Name,ignore.case=T),
grep("konig",bs_gr$Name,ignore.case=T),
grep("achenmeier",bs_gr$Name,ignore.case=T),
grep("lindner",bs_gr$Name,ignore.case=T),
grep("luethi",bs_gr$Name,ignore.case=T),
grep("etzger",bs_gr$Name,ignore.case=T),
grep("meyer",bs_gr$Name,ignore.case=T),
grep("mury",bs_gr$Name,ignore.case=T),
grep("pfeifer",bs_gr$Name,ignore.case=T),
grep("einhard",bs_gr$Name,ignore.case=T),
grep("rutschmann",bs_gr$Name,ignore.case=T),
grep("schafer",bs_gr$Name,ignore.case=T),
grep("tobit",bs_gr$Name,ignore.case=T),
grep("schmid",bs_gr$Name,ignore.case=T),
grep("soland",bs_gr$Name,ignore.case=T),
grep("strahm",bs_gr$Name,ignore.case=T),
grep("ueberwass",bs_gr$Name,ignore.case=T),
grep("ungricht",bs_gr$Name,ignore.case=T),
grep("vischer",bs_gr$Name,ignore.case=T),
grep("vitelli",bs_gr$Name,ignore.case=T),
grep("falkenstein",bs_gr$Name,ignore.case=T),
grep("wartburg",bs_gr$Name,ignore.case=T),
grep("wanner",bs_gr$Name,ignore.case=T),
grep("wenk",bs_gr$Name,ignore.case=T),
grep("thrich",bs_gr$Name,ignore.case=T),
grep("wyss",bs_gr$Name,ignore.case=T),
grep("zappal",bs_gr$Name,ignore.case=T))]<-1

krks<-read.csv("C:/Users/simon/Dropbox/PCP/CH_Candidates_Regional/Daten_BFS_(DO_NOT_USE_YET)/kw_kandidaten.csv",sep=";")

krks_bs<-krks[krks$kt=="BS"&krks$year>2011&krks$elected==1,]
krks_bs
str(krks_bs)
unique(krks_bs$name)
krks_bs_2016<-krks[krks$kt=="BS"&krks$year>2012&krks$elected==1,]
krks_bs_2012<-krks[krks$kt=="BS"&krks$year==2012&krks$elected==1,]
names(krs_bs)
head(krs_bs)
krks_bs_2016_2012<-merge(krks_bs_2016,krks_bs_2012,by="nam")
krks_bs_2016_2012)
names(krks_bs_2016)
names(krks_bs_2012)
krks$fullname<-paste(krks$first_name,krks$name,sep=" ")
krks$fullname
krks_bs_2016<-krks[krks$kt=="BS"&krks$year>2012&krks$elected==1,]
krks_bs_2012<-krks[krks$kt=="BS"&krks$year==2012&krks$elected==1,]
krks_bs_2016_2012<-merge(krks_bs_2016,krks_bs_2012,by="fullname")
str(krks_bs_2016_2012)
krks_bs_2016_2012<-merge(krks_bs_2016,krks_bs_2012,by="fullname",all.y=T,all.x=T)
str(krks_bs_2016_2012)
krks_bs<-krks_bs_2016_2012
#krks_bs$Name<-paste(krks_bs$first_name,krks_bs$name)

library(XML)
##### for bs data
#check 20120627_AbstTabelle.xml
#####
abst_list<-list.files(path="C:/Users/simon/Dropbox/PCP/CH_BS_votes/")
abst_list<-abst_list[grep("xml",abst_list)]
abst_list_v<-array(NA,c(length(abst_list)))
i=2
for (i in 1:length(abst_list)){
#if (i!=2) 
doc<-xmlTreeParse(paste("C:/Users/simon/Dropbox/PCP/CH_BS_votes/",abst_list[i],sep=""))
r<-xmlRoot(doc)
#if (i!=2&i!=24) 
if (i!=24) abst_list_v[i]<-(xmlSize(r[[2]][[1]][[5]]))
if (i==24) abst_list_v[i]<-(xmlSize(r[[5]][[1]][[5]]))
}

votes<-array(NA,c(100,max(abst_list_v,na.rm=t),length(abst_list)))
row_names<-array(NA,dim=c(length(abst_list),max(abst_list_v,na.rm=t)))
for (k in 1:length(abst_list)){
if (k!=2) doc<-xmlTreeParse(paste("C:/Users/simon/Dropbox/PCP/CH_BS_votes/",abst_list[k],sep=""))
r<-xmlRoot(doc)
for (i in 1:100){
if (k!=24) for (j in 1:(abst_list_v[k])){
votes[i,j,k]<-(xmlValue(r[[2]][[1]][[i+4]][[j]]))
if (j==1)row_names[k,j]<-c("No")
if (j==2)row_names[k,j]<-c("Name")
#if (j>2)row_names[k,j]<-paste((xmlValue(r[[2]][[1]][[4]][[3]]))
if (j>2)row_names[k,j]<-paste(substr(abst_list[k],1,20)
,(xmlValue(r[[2]][[1]][[4]][[j+1]])))
}

if (k==24) for (j in 1:(abst_list_v[k])){
votes[i,j,k]<-(xmlValue(r[[5]][[1]][[i+4]][[j]]))
if (j==1)row_names[k,j]<-c("No")
if (j==2)row_names[k,j]<-c("Name")
#if (j>2)row_names[k,j]<-paste((xmlValue(r[[5]][[1]][[4]][[3]]))
if (j>2)row_names[k,j]<-paste(substr(abst_list[k],1,20)
,(xmlValue(r[[5]][[1]][[4]][[j]])))
}
}
}

votes1_df<-data.frame(votes[,c(1:abst_list_v[1]),1])
names(votes1_df)<-row_names[1,c(1:abst_list_v[1])]


for (i in 2:(min(grep("\\(",votes[1,2,]))-1)){
votes1_df_add<-data.frame(votes[,c(1:abst_list_v[i]),i])
names(votes1_df_add)<-row_names[i,c(1:abst_list_v[i])]

votes1_df<-merge(votes1_df,votes1_df_add[,-c(1)], by=c("Name"),all.y=T,all.x=T)
}

votes_df<-votes1_df

votes1_df<-data.frame(votes[,c(1:abst_list_v[(min(grep("\\(",votes[1,2,])))]),(min(grep("\\(",votes[1,2,])))])
names(votes1_df)<-row_names[(min(grep("\\(",votes[1,2,]))),c(1:abst_list_v[(min(grep("\\(",votes[1,2,])))])]

for (i in (min(grep("\\(",votes[1,2,]))+1):(max(grep("\\(",votes[1,2,]))-1)){
votes1_df_add<-data.frame(votes[,c(1:abst_list_v[i]),i])
names(votes1_df_add)<-row_names[i,c(1:abst_list_v[i])]
votes1_df<-merge(votes1_df,votes1_df_add[,-c(1)], by=c("Name"),all.y=T,all.x=T)
}

votes1_df$Party<-NA
for (l in 1:nrow(votes1_df)){
votes1_df$Party[l]<-unlist(strsplit(unlist(strsplit(paste(votes1_df$Name[l])," \\("))[2],"\\)"))
levels(votes1_df$Name)<-c(levels(votes1_df$Name),paste(unlist(strsplit(paste(votes1_df$Name[l])," \\("))[1]))
votes1_df$Name[l]<-unlist(strsplit(paste(votes1_df$Name[l])," \\("))[1]
}

votes1_df<-merge(votes_df,votes1_df[-c(2)], by=c("Name"),all.y=T,all.x=T)

str(votes1_df)
str(votes1_df_add)
names(votes1_df)

# BEWARE removes votes outside june-june period
votes1_df<-votes1_df[,-c(grep("201602",names(votes1_df)),
grep("201603",names(votes1_df)),
grep("201604",names(votes1_df)),
grep("201605",names(votes1_df)),
grep("2017062",names(votes1_df)),
grep("201709",names(votes1_df)))]

vt_bs<-votes1_df
vt_bs$Name<-gsub("Ã¶","ö",vt_bs$Name)
vt_bs$Name<-gsub("Ã©","é",vt_bs$Name)
vt_bs$Name<-gsub("Ã¼","ü",vt_bs$Name)
vt_bs$Name<-gsub("Ã¤","ä",vt_bs$Name)
str(vt_bs)
str(krks_bs)
str(vt_bs$Name)
krs_vt_bs<-merge(vt_bs,krks_bs,by.x="Name",by.y="fullname",all.x=T,all.y=T)
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==1]<-c("FDP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==2]<-c("CVP/EVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==3]<-c("SP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==4]<-c("SVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==5]<-c("LDP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==7]<-c("CVP/EVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==13]<-c("GB")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.x==31]<-c("GLP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.y==41]<-c("CVP/EVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.y==2]<-c("CVP/EVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&krs_vt_bs$parteinummer.y==4]<-c("SVP")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&is.na(krs_vt_bs$parteinummer.y)==T]<-c("fraktionslos")
krs_vt_bs$Party[is.na(krs_vt_bs$Party)&is.na(krs_vt_bs$parteinummer.x)==T]<-c("fraktionslos")
str(krs_vt_bs)
krs_vt_bs$tl_2016<-0
krs_vt_bs$tl_2016[c(grep("OskarAtici",krs_vt_bs$Name),
grep("Benz",krs_vt_bs$Name),
grep("Burtkchardt",krs_vt_bs$Name),
grep("Casagrande",krs_vt_bs$Name),
grep("Cramer",krs_vt_bs$Name),
grep("Gysin",krs_vt_bs$Name),
grep("Hafner",krs_vt_bs$Name),
grep("Haller",krs_vt_bs$Name),
grep("Herzig",krs_vt_bs$Name),
grep("Inglin",krs_vt_bs$Name),
grep("nig-L",krs_vt_bs$Name),
grep("Mall",krs_vt_bs$Name),
grep("gelin",krs_vt_bs$Name),
grep("Nanni",krs_vt_bs$Name),
grep("Rutschmann",krs_vt_bs$Name),
grep("Schäfer",krs_vt_bs$Name),
grep("Stöcklin",krs_vt_bs$Name),
grep("Stolz",krs_vt_bs$Name),
grep("Ungricht",krs_vt_bs$Name),
grep("Vitelli",krs_vt_bs$Name),
grep("gtli",krs_vt_bs$Name),
grep("Wirz",krs_vt_bs$Name))]<-1
krs_vt_bs$nloy<-0
krs_vt_bs$loy<-0
i=3
i=i+1
for (i in 3:max(grep("2013",names(krs_vt_bs)))){
 for (j in 1:nrow(krs_vt_bs)){
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))&krs_vt_bs[j,i]=="N")krs_vt_bs$nloy[j]<-krs_vt_bs$nloy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="N")krs_vt_bs$nloy[j]<-krs_vt_bs$nloy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))==length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))==length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="N")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
}
}
krs_vt_bs$loy_2012<-krs_vt_bs$loy
krs_vt_bs$nloy_2012<-krs_vt_bs$nloy
krs_vt_bs$nloy<-0
krs_vt_bs$loy<-0
i=3
i=i+1
for (i in (1+max(grep("2013",names(krs_vt_bs)))):max(grep("2017",names(krs_vt_bs)))){
 for (j in 1:nrow(krs_vt_bs)){
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))&krs_vt_bs[j,i]=="N")krs_vt_bs$nloy[j]<-krs_vt_bs$nloy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))>length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="N")krs_vt_bs$nloy[j]<-krs_vt_bs$nloy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))==length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="J")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
if(is.na(krs_vt_bs$parteinummer.x[j])==F&is.na(krs_vt_bs[j,i])==F)if(length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="N"))==length(which(krs_vt_bs[krs_vt_bs$parteinummer.x==krs_vt_bs$parteinummer.x[j],i]=="J"))&krs_vt_bs[j,i]=="N")krs_vt_bs$loy[j]<-krs_vt_bs$loy[j]+1
}
}
krs_vt_bs$loy_2016<-krs_vt_bs$loy
krs_vt_bs$nloy_2016<-krs_vt_bs$nloy
summary(lm (krs_vt_bs$loy_2016/(krs_vt_bs$loy_2016+krs_vt_bs$nloy_2016)~ krs_vt_bs$tl_2016))
summary(lm (loy/(loy+nloy)-loy_2012/(loy_2012+nloy_2012)~ tl_2016+as.factor(listenbezeichnung),data=krs_vt_bs))
krs_vt_bs$share_absent<-0
krs_vt_bs$share_abstain<-0
for (i in 1: nrow(krs_vt_bs)){
krs_vt_bs$share_absent[i]<-length(which(krs_vt_bs[i,c(3:max(grep("2013",names(krs_vt_bs))))]=="A"))/
 length(is.na(krs_vt_bs[i,c(3:max(grep("2013",names(krs_vt_bs))))])==F)
krs_vt_bs$share_abstain[i]<-length(which(krs_vt_bs[i,c(3:max(grep("2013",names(krs_vt_bs))))]=="E"))/
 length(is.na(krs_vt_bs[i,c(3:max(grep("2013",names(krs_vt_bs))))])==F)
}
krs_vt_bs$share_absent_2012<-krs_vt_bs$share_absent
krs_vt_bs$share_abstain_2012<-krs_vt_bs$share_abstain
krs_vt_bs$share_absent<-0
krs_vt_bs$share_abstain<-0
for (i in 1: nrow(krs_vt_bs)){
krs_vt_bs$share_absent[i]<-length(which(krs_vt_bs[i,c((1+max(grep("2013",names(krs_vt_bs)))):max(grep("2017",names(krs_vt_bs))))]=="A"))/
 length(is.na(krs_vt_bs[i,c((1+max(grep("2013",names(krs_vt_bs)))):max(grep("2017",names(krs_vt_bs))))])==F)
krs_vt_bs$share_abstain[i]<-length(which(krs_vt_bs[i,c((1+max(grep("2013",names(krs_vt_bs)))):max(grep("2017",names(krs_vt_bs))))]=="E"))/
 length(is.na(krs_vt_bs[i,c((1+max(grep("2013",names(krs_vt_bs)))):max(grep("2017",names(krs_vt_bs))))])==F)
}
krs_vt_bs$age_2016<-2016-as.numeric(substr(krs_vt_bs$BirthDate,7,10))
bs_gr_krs_vt$reelec<-0
bs_gr_krs_vt$reelec[grep("2.2013",bs_gr_krs_vt$Membership)]<-1
bs_gr_krs_vt$reelec<-0
bs_gr_krs_vt$reelec[grep("10.2012",bs_gr_krs_vt$Membership)]
bs_gr_krs_vt$reelec[grep("2012",bs_gr_krs_vt$Membership)]
names(bs_gr_krs_vt)
krs_vt_bs$BirthDate
names(bs_gr_krs_vt)bs_gr$id<-NA
(act_2017+act_2016-act_2013-act_2012)
as.year(paste(bs_gr$BirthDate))
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]))==1)bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)[grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])]
}

for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
#if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)
}
summary(lm((act_2017+act_2016-act_2013-act_2012)~tl,data=bs_gr[bs_gr$cont==1,]))
names(bs_gr)
head(bs_gr$V3)
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1) print(paste(i,grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])))
}
i
length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1
bs_gr$V3[i]
krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]
grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1) 
print(paste(i,grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])))
}
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1) print(paste(i,grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])))
}
krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]
grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])
length(grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]))
krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]
grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])
krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)][grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])]
grep(bs_gr$Name[i],krs_vt_bs$Name)
grep(bs_gr$Name[i],krs_vt_bs$Name)[grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])]
for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]))==1)bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)[grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])]
}
bs_gr$id
table(bs_gr$id,bs_gr$tl)
table(bs_gr$tl[is.na(bs_gr$id)==F])
table(bs_gr$tl[is.na(bs_gr$cont)==F])
table(bs_gr$tl[is.na(bs_gr$id)==F])
bs_gr$id
summary(bs_gr$id)
length(bs_gr$id)
summary(lm((act_2017+act_2016-act_2013-act_2012)~tl,data=bs_gr[is.na(bs_gr$id)==F,]))
rownames(krs_vt_bs)
bs_gr$id
as.numeric(rownames(krs_vt_bs))
krs_vt_bs$id<-as.numeric(rownames(krs_vt_bs))
bs_gr_krs_vt<-merge(krs_vt_bs,bs_gr,by.x="Name",by.y="Name",all.x=T,all.y=T)
bs_gr_krs_vt<-merge(krs_vt_bs,bs_gr,by.x="id",by.y="id",all.x=T,all.y=T)

bs_gr_krs_vt$age_2016<-2016-as.numeric(substr(bs_gr_krs_vt$BirthDate,7,10))
bs_gr_krs_vt$reelec<-0
bs_gr_krs_vt$reelec[grep("2.2013",bs_gr_krs_vt$Membership)]<-1

summary(lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$loy)==F&is.na(bs_gr_krs_vt$loy_2012)==F,]))
summary(bs_act<-lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[-c(grep("bis",bs_gr_krs_vt$Membership),which(is.na(bs_gr_krs_vt$id)==T)
),]))
names(bs_gr_krs_vt)
table(bs_gr_krs_vt$year.x)
table(bs_gr_krs_vt$year.y)
summary(lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs2<-lm(-share_absent_2012+share_absent~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abst2<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(bs_act<-lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs2<-lm(-share_absent_2012+share_absent~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abst2<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_ly<-lm(-loy_2012/(loy_2012+nloy_2012)+loy/(loy+nloy)~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F&bs_gr_krs_vt$Party!="fraktionslos",]))
summary(reg_ly<-lm(-loy_2012/(loy_2012+nloy_2012)+loy/(loy+nloy)~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F&bs_gr_krs_vt$Party!="fraktionslos",]))
summary(reg_abst2<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs2<-lm(-share_absent_2012+share_absent~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(bs_act<-lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
stargazer(reg_ly)
stargazer(reg_abs,reg_abst,bs_act)
summary(bs_act<-lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs<-lm(-share_absent_2012+share_absent~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abst<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_ly<-lm(-loy_2012/(loy_2012+nloy_2012)+loy/(loy+nloy)~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F&bs_gr_krs_
vt$Party!="fraktionslos",]))
summary(reg_ly<-lm(-loy_2012/(loy_2012+nloy_2012)+loy/(loy+nloy)~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F&bs_gr_krs_vt$Party!="fraktionslos",]))
summary(reg_abst<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs<-lm(-share_absent_2012+share_absent~(tl_2016)+Party+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(bs_act<-lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
stargazer(reg_ly)
stargazer(reg_abs,reg_abst,bs_act)
table(bs_gr_krs_vt$tl,bs_gr_krs_vt$tl_2016)
table(bs_gr_krs_vt$tl_2016)
table(bs_gr_krs_vt$tl)

summary(bs_act<-lm((act_2017-act_2013)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abs<-lm(-share_absent_2012+share_absent~(tl)+Party+age_2016+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_abst<-lm(-share_abstain_2012+share_abstain~(tl)+Party+age_2016+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F,]))
summary(reg_ly<-lm(-loy_2012/(loy_2012+nloy_2012)+loy/(loy+nloy)~(tl)+Party+age_2016+reelec,,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$year.x)==F&is.na(bs_gr_krs_vt$year.y)==F&bs_gr_krs_vt$Party!="fraktionslos",]))
stargazer(reg_ly)
stargazer(reg_abs,reg_abst,bs_act)
stargazer(reg_ly)

save(reg_ly,file="C:/Users/simon/Dropbox/PCP/CH_retirees_dataset_1999_2015/saved_models/bs_reg_ly.rda")
save(reg_abst,file="C:/Users/simon/Dropbox/PCP/CH_retirees_dataset_1999_2015/saved_models/bs_reg_abst.rda")
save(reg_abs,file="C:/Users/simon/Dropbox/PCP/CH_retirees_dataset_1999_2015/saved_models/bs_reg_abs.rda")
save(bs_act,file="C:/Users/simon/Dropbox/PCP/CH_retirees_dataset_1999_2015/saved_models/bs_reg_act.rda")

## some other stuff

bs_gr$tl<-0
bs_gr$cont<-0
bs_gr$tl[c(grep("atici",bs_gr$Name,ignore.case=T),
grep("benz",bs_gr$Name,ignore.case=T),
grep("casagrande",bs_gr$Name,ignore.case=T),
grep("cramer",bs_gr$Name,ignore.case=T),
grep("hafner",bs_gr$Name,ignore.case=T),
grep("e_haller",bs_gr$Name,ignore.case=T),
grep("inglin",bs_gr$Name,ignore.case=T),
grep("konig",bs_gr$Name,ignore.case=T),
grep("rutschmann",bs_gr$Name,ignore.case=T),
grep("tobit",bs_gr$Name,ignore.case=T),
grep("ungricht",bs_gr$Name,ignore.case=T),
grep("vitelli",bs_gr$Name,ignore.case=T))]<-1

bs_gr$cont[c(
grep("ckermann",bs_gr$Name,ignore.case=T),
grep("atici",bs_gr$Name,ignore.case=T),
grep("auderset",bs_gr$Name,ignore.case=T),
grep("benz",bs_gr$Name,ignore.case=T),
grep("ernasconi",bs_gr$Name,ignore.case=T),
grep("ertschi",bs_gr$Name,ignore.case=T),
grep("ochsler",bs_gr$Name,ignore.case=T),
grep("urckhardt",bs_gr$Name,ignore.case=T),
grep("rigger",bs_gr$Name,ignore.case=T),
grep("amlibel",bs_gr$Name,ignore.case=T),
grep("casagrande",bs_gr$Name,ignore.case=T),
grep("cramer",bs_gr$Name,ignore.case=T),
grep("rdogan",bs_gr$Name,ignore.case=T),
grep("ymann",bs_gr$Name,ignore.case=T),
grep("allacchi",bs_gr$Name,ignore.case=T),
grep("gander",bs_gr$Name,ignore.case=T),
grep("oepfert",bs_gr$Name,ignore.case=T),
grep("reuter",bs_gr$Name,ignore.case=T),
grep("roflin",bs_gr$Name,ignore.case=T),
grep("rossenbacher",bs_gr$Name,ignore.case=T),
grep("hafner",bs_gr$Name,ignore.case=T),
grep("e_haller",bs_gr$Name,ignore.case=T),
grep("hofer",bs_gr$Name,ignore.case=T),
grep("inglin",bs_gr$Name,ignore.case=T),
grep("koechlin",bs_gr$Name,ignore.case=T),
grep("kaufmann",bs_gr$Name,ignore.case=T),
grep("konig",bs_gr$Name,ignore.case=T),
grep("achenmeier",bs_gr$Name,ignore.case=T),
grep("lindner",bs_gr$Name,ignore.case=T),
grep("luethi",bs_gr$Name,ignore.case=T),
grep("etzger",bs_gr$Name,ignore.case=T),
grep("meyer",bs_gr$Name,ignore.case=T),
grep("mury",bs_gr$Name,ignore.case=T),
grep("pfeifer",bs_gr$Name,ignore.case=T),
grep("einhard",bs_gr$Name,ignore.case=T),
grep("rutschmann",bs_gr$Name,ignore.case=T),
grep("schafer",bs_gr$Name,ignore.case=T),
grep("tobit",bs_gr$Name,ignore.case=T),
grep("schmid",bs_gr$Name,ignore.case=T),
grep("soland",bs_gr$Name,ignore.case=T),
grep("strahm",bs_gr$Name,ignore.case=T),
grep("ueberwass",bs_gr$Name,ignore.case=T),
grep("ungricht",bs_gr$Name,ignore.case=T),
grep("vischer",bs_gr$Name,ignore.case=T),
grep("vitelli",bs_gr$Name,ignore.case=T),
grep("falkenstein",bs_gr$Name,ignore.case=T),
grep("wartburg",bs_gr$Name,ignore.case=T),
grep("wanner",bs_gr$Name,ignore.case=T),
grep("wenk",bs_gr$Name,ignore.case=T),
grep("thrich",bs_gr$Name,ignore.case=T),
grep("wyss",bs_gr$Name,ignore.case=T),
grep("zappal",bs_gr$Name,ignore.case=T))]<-1

bs_gr_krs_vt$age_2016<-2016-as.numeric(substr(bs_gr_krs_vt$BirthDate,7,10))
bs_gr_krs_vt$reelec<-0
bs_gr_krs_vt$reelec[grep("2.2013",bs_gr_krs_vt$Membership)]<-1

summary(lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$loy)==F&is.na(bs_gr_krs_vt$loy_2012)==F,]))

summary(bs_act<-lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016+reelec,
 data=bs_gr_krs_vt[-c(grep("bis",bs_gr_krs_vt$Membership),which(is.na(bs_gr_krs_vt$id)==T)
),]))


summary(reg_abs2<-lm(-share_absent_2012+share_absent~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$loy)==F&is.na(bs_gr_krs_vt$loy_2012)==F,]))

summary(reg_abst2<-lm(-share_abstain_2012+share_abstain~(tl_2016)+Party,
 data=bs_gr_krs_vt[is.na(bs_gr_krs_vt$loy)==F&is.na(bs_gr_krs_vt$loy_2012)==F,]))



stargazer(reg_abs,reg_abst,bs_act)
summary(lm((act_2017+act_2016-act_2013-act_2012)~tl+Party+age_2016,
 data=bs_gr_krs_vt[c(as.numeric(rownames(model.frame(reg_ly)))),]))
bs_gr_krs_vt$name
 

bs_gr$id<-NA
(act_2017+act_2016-act_2013-act_2012)
as.year(paste(bs_gr$BirthDate))

for (i in 1:nrow(bs_gr)){
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))>1)print(paste(i,grep(bs_gr$Name[i],krs_vt_bs$Name)))
if(length(grep(bs_gr$Name[i],krs_vt_bs$Name))==1) bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)
if(length(grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)]))==1)bs_gr$id[i]<-grep(bs_gr$Name[i],krs_vt_bs$Name)[grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])]
}

krs_vt_bs$id<-as.numeric(rownames(krs_vt_bs))
 print(paste(i,grep(bs_gr$V3[i],krs_vt_bs$Name[grep(bs_gr$Name[i],krs_vt_bs$Name)])))
length(bs_gr$id)
table(bs_gr$tl[is.na(bs_gr$id)==F])
names(bs_gr_krs_vt)
bs_gr_krs_vt<-merge(krs_vt_bs,bs_gr,by.x="id",by.y="id",all.x=T,all.y=T)

unique(bs_gr$Name)
