Skip to content

Instantly share code, notes, and snippets.

# Load required packages
library(reshape2)
library(zoo)
library(data.table)
library(ggplot2)
library(lattice)
library(foreach)
library(doParallel)
# Import data and prepare data frames
prep.comp <- function(x){
if (names(x)[1]!='Week'){
stop("The first column should be 'Week'. Check the data.")
}
x[['Week']] <- as.character(x[['Week']])
x[['weekstart']] <- strsplit(x[['Week']],split=' - ',fixed=TRUE)[[1]]
x[['weekend']] <- strsplit(x[['Week']],split=' - ',fixed=TRUE)[[2]]
x[['weekstart']] <- as.Date(x[['weekstart']],format='%Y-%m-%d')
x[['weekend']] <- as.Date(x[['weekend']],format='%Y-%m-%d')
return(x)
setwd("/Users/thiemo/Dropbox/mafundo/")
is_installed <- function(mypkg) is.element(mypkg, installed.packages()[,1])
load_or_install<-function(package_names)
{
for(package_name in package_names)
{
if(!is_installed(package_name))
library(foreign)
library(dplyr)
library(zoo)
df1 <- read.dta('~/AeroFS/Googlestuff/searchtechnology-collapsed.dta')
df2 <- read.dta('~/AeroFS/Googlestuff/mergedquarterly.dta')
load('googledates.rdata')
df1 <- left_join(df1,monthlookup,by='time')
df1$time <- NULL
prep.comp <- function(x,y){
require(dplyr)
if (names(x)[1]!='Week'){
stop("The first column should be 'Week'. Check the data.")
}
countryname <- names(x)[2]
x[['Week']] <- as.character(x[['Week']])
x[['weekstart']] <- substr(x[['Week']],1,11)
x[['weekend']] <- substr(x[['Week']],13,24)
x[['weekstart']] <- as.Date(x[['weekstart']],format='%Y-%m-%d')
# Create function to get data from weekly to monthly
weekly.monthly <- function(x){
require(dplyr)
if (names(x)[1]!='Week'){
stop("The first column should be 'Week'. Check the data.")
}
countryname <- names(x)[2]
x[['Week']] <- as.character(x[['Week']])
x[['weekstart']] <- substr(x[['Week']],1,11)
x[['weekend']] <- substr(x[['Week']],13,24)
# Load required packages
library(dplyr)
library(reshape2)
# Load data
setwd('~/AeroFS/Googlestuff/chris/')
load("longform.rdata")
data$month <- as.Date(data$month)
# Get a random sample of word-country combinations to check against google trends
import sys
import re
f = open('~/Documents/spillovers/tech_proximity2/orbis_patents_titles.csv').read()
patents = {}
rows = f.split('\n')[1:]
numbers = [j.split(',')[0] for j in rows]
titles = [j.split(',')[1:] for j in rows]
for i in range(0,len(numbers)):
patents[numbers[i]] = titles[i]
for i in len(titles)
import sys
f = open('/Users/cigrainger/Documents/spillovers/tech_proximity2/orbis_patents_titles.csv','r')
f2 = open('/Users/cigrainger/Documents/spillovers/tech_proximity2/patenttitles.csv','w')
patents = {}
for line in f:
y = line.split(',',1)
if len(y)==2:
c = y[1].replace(',','')
f2.write(y[0]+','c+'\n')
f.close()
import sys
f = open('/Users/cigrainger/Documents/spillovers/tech_proximity2/orbis_patents_titles.csv','r')
f2 = open('/Users/cigrainger/Documents/spillovers/tech_proximity2/patenttitles.csv','w')
f2.truncate()
for line in f:
y = line.split(',',1)
if len(y)==2:
c = y[1].replace(',','')
f2.write(y[0]+','+c)
f.close()