library("foreign", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
install.package('plyr')
install.packages('plyr')
install.packages('dplyr')
install.packages('felm')
?rnorm
alpha<-0.05
t.alpha<-abs(qt(p = alpha/2, df = 1000))
k<-0.80
t.k<-abs(qt(p = k, df = 1000))
P<-0.8
sigma2<-100000
J<-250
#MDE<-mean(data.new$kwh)*0.05
t<-30
rho<-0.7
MDE<-sqrt(((t.k+t.alpha)^2/(P*(1-P)))*((sigma2)/J)*((rho+(1-rho)/t)))
?qt
t.alpha<-abs(qt(p = alpha/2, df = 1000))
t.alpha<-abs(qt(p = alpha/2, df = 10000))
t.k<-abs(qt(p = k, df = 10000))
# Power Calc #
#Set Significance Levels
alpha<-0.05
t.alpha<-abs(qt(p = alpha/2, df = 10000))
#Set Power of test
k<-0.80
t.k<-abs(qt(p = k, df = 10000))
#Proportion Treated
P<-0.5
#Residual Variance (guess from lit)
sigma2<-1000
#MDE<-mean(data.new$kwh)*0.05
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
N=1000
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
N=100
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
N=7
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
# Power Calc #
#Set Significance Levels
alpha<-0.05
t.alpha<-abs(qt(p = alpha/2, df = 10000))
#Set Power of test
k<-0.80
t.k<-abs(qt(p = k, df = 10000))
#Proportion Treated
P<-0.5
#Residual Variance (guess from lit)
sigma2<-1000
#Fix Sample and solve for MDE
N=1000
#Calc MDE assuming simple RCT
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
#Fix MDE and solve for N
MDEa <- 10
#Calc N needed assuming simple RCT
Na <- ((t.k+t.alpha)^2/P*(1-P))*(sigma2/MDEa^2)
#Add clusters
#Set Clusters and Correlation
J=68
n=15
Nb<-J*n
#amount of correlation across errors - guess
rho=.3
#MDE for clustered estimation
MDEb <- ((t.k+t.alpha)/sqrt(P(1-P)*J))*sqrt((rho+(1-rho)/n)*simga2)
MDEb <- ((t.k+t.alpha)/sqrt(P*(1-P)*J))*sqrt((rho+(1-rho)/n)*simga2)
MDEb <- ((t.k+t.alpha)/sqrt(P*(1-P)*J))*sqrt((rho+(1-rho)/n)*sigma2)
sigma2<-100
# Power Calc #
#Set Significance Levels
alpha<-0.05
t.alpha<-abs(qt(p = alpha/2, df = 10000))
#Set Power of test
k<-0.80
t.k<-abs(qt(p = k, df = 10000))
#Proportion Treated
P<-0.5
#Residual Variance (guess from lit)
sigma2<-100
#Fix Sample and solve for MDE
N=1000
#Calc MDE assuming simple RCT
MDE<- (t.k+t.alpha)*sqrt(1/(P*(1-P)))*sqrt((sigma2)/N)
#Fix MDE and solve for N
MDEa <- 10
#Calc N needed assuming simple RCT
Na <- ((t.k+t.alpha)^2/P*(1-P))*(sigma2/MDEa^2)
#Add clusters#
#Set Clusters and Correlation
J=68
n=15
Nb<-J*n
#amount of correlation across errors - guess
rho=.3
#MDE for clustered estimation
MDEb <- ((t.k+t.alpha)/sqrt(P*(1-P)*J))*sqrt((rho+(1-rho)/n)*sigma2)
rm(list = ls())
dir <-'/Users/gregorylane/Dropbox/Bangladesh/Data Raw/Flood Data/'
dir2 <-'/Users/gregorylane/Dropbox/Bangladesh/Data Raw/Flood Data/NTWL/'
dir3 <-'/Users/gregorylane/Dropbox/Bangladesh/Data Modified/Location Matches/'
dir4 <-'/Users/gregorylane/Dropbox/Bangladesh/Data Modified/Flood Data/'
#Files with water-level data
files<-list.files(dir2,full.names=TRUE)
SW8 <- read.table("~/Dropbox/Berkeley/Bangladesh/Data Raw/Flood Data/NTWL/SW8.xls", header=TRUE, quote="\"")
View(SW8)
rm(list = ls())
dir <-'~/Dropbox/Berkeley/Bangladesh/Data Raw/Flood Data/'
dir2 <-'~/Dropbox/Berkeley/Bangladesh/Data Raw/Flood Data/NTWL/'
dir3 <-'~/Dropbox/Berkeley/Bangladesh/Data Modified/Location Matches/'
dir4 <-'~/Dropbox/Berkeley/Bangladesh/Data Modified/Flood Data/'
#Files with water-level data
files<-list.files(dir2,full.names=TRUE)
library(foreign)
library(gdata)
install.packages(gdata)
install.packages('gdata')
library(gdata)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations.xlsx'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations.xlsx'), header=TRUE)
danger.levels <- read.xls(paste0(dir,'Flood_DangerLevels.xls'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations.xlsx'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations2.xlsx'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations2.xls'), header=TRUE)
x
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations2.xlsx'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations2.xls'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.xls'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
rm gauge.loc
rm()gauge.loc
rm(gauge.loc)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
rm(gauge.loc)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations2.xlsx'), header=TRUE)
gauge.loc<-read.xls(paste0(dir,'Excel_Flood_Data_Locations.xlsx'), header=TRUE)
gauge.loc<-read.csv(paste0(dir,'Excel_Flood_Data_Locations2.csv'), header=TRUE)
View(gauge.loc)
colnames(gauge.loc)[5] <- 'StationID'
View(gauge.loc)
flood.df <- merge(x = gauge.loc, y = danger.levels , by = c("StationID"), all.x = F)
View(flood.df)
flood.df$Latitude<- as.numeric(as.character(flood.df$Latitude))
flood.df$Longitude <- as.numeric(as.character(flood.df$Longitude))
flood.df$Latitude <- round(flood.df$Latitude ,digits=4)
flood.df$Longitude <- round(flood.df$Longitude ,digits=4)
#Get rid of obs with no Danger Level:
flood.df <- na.omit(flood.df)
View(flood.df)
########## Stack Flood Level Data ##########
data.wlevels <- read.xls(files[1], header=TRUE)
for(i in 2:length(files)) {
temp <- read.xls(files[i], header=TRUE)
data.wlevels<- rbind(data.wlevels,temp)
}
data.wlevels  <- read.csv(paste0(dir,'WaterLevels.csv'))
View(data.wlevels)
all.flood.df <- merge(x = data.wlevels, y = flood.df , by = c("StationID"), all.x = F)
View(all.flood.df)
View(all.flood.df)
View(all.flood.df)
summary(all.flood.df)
all.flood.df <- all.flood.df[c(1:12,14,17)]
View(all.flood.df)
all.flood.df <- merge(x = data.wlevels, y = flood.df , by = c("StationID"), all.x = F)
colnames(all.flood.df)[c(11,12)] <- c("Lat_Gauge","Long_Gauge")
all.flood.df <- all.flood.df[c(1:12,17)]
View(all.flood.df)
write.csv(x = all.flood.df, file = paste0(dir4,'AllFloodData.csv'), row.names = FALSE)
write.csv(x = all.flood.df, file = paste0(dir4,'AllFloodData.csv'), row.names = FALSE)
write.csv(x = all.flood.df, file = paste0('~/Dropbox/Berkeley/Bangladesh/Data Modified/Flood Data/AllFloodData.csv'), row.names = FALSE)
?write.csv
write.csv(x = all.flood.df, file = paste0("~/Dropbox/Berkeley/Bangladesh/Data Modified/Flood Data/AllFloodData.csv"), row.names = FALSE)
write.csv(x = all.flood.df, file = paste0('/Users/gregorylane/Dropbox/Berkeley/Bangladesh/Data Modified/Flood Data/AllFloodData.csv'), row.names = FALSE)
write.csv(x = all.flood.df, file = paste0('/Users/gregorylane/Dropbox/Berkeley/Bangladesh/Data Modifed/Flood Data/AllFloodData.csv'), row.names = FALSE)
View(all.flood.df)
View(all.flood.df)
library(rgdal)
install.packages('rgdal')
library(rgdal)
install.packages('rgdal')
library(rgdal)
library(ggplot2)
library(RColorBrewer)
library(grid)
dir5 <-'/Users/gregorylane/Documents/Documents/Berkeley/Bangladesh/Data Raw/Bang Shape Files/BGD_adm/'
b.shape <-readOGR(dsn=paste0(dir5), layer='BGD_adm0')
b.shape <-readOGR(dsn=paste0(dir5), layer='BGD_adm1')
ggplot()  +  geom_point(data=branch.loc , aes(x = longitude , y = lat), size=4, color='cadetblue') +
theme_bw() +
theme(legend.position = 'bottom') +
xlab("") + ylab("")
ggplot()  +  geom_point(data=gauge.loc , aes(x = longitude , y = lat), size=4, color='cadetblue') +
theme_bw() +
theme(legend.position = 'bottom') +
xlab("") + ylab("")
names(gauge.loc) <- c('lat','longitude')
ggplot()  +  geom_point(data=gauge.loc , aes(x = longitude , y = lat), size=4, color='cadetblue') +
theme_bw() +
theme(legend.position = 'bottom') +
xlab("") + ylab("")
b.shape <-readOGR(dsn=paste0(dir5), layer='BGD_adm0')
setwd("~/Dropbox/Berkeley/Bangladesh/Data Raw/Bang Shape Files/BGD_adm")
dir5 <-'~/Dropbox/Berkeley/Bangladesh/Data Raw/Bang Shape Files/BGD_adm'
b.shape <-readOGR(dsn=paste0(dir5), layer='BGD_adm0')
?readOGR
