#Code written by William Doonan 2017/2018

#Need igraph to be able to manipulate the graphs in R
library("igraph")

#To reproduce the plots in the manuscript one needs these packages
library(ggplot2)
library(pracma)
library(fBasics)

#A function to return a dataframe with the wanted time till next citation
#x is a data frame with labels 'cited', 'lag', 'seq' 
#'seq' is the sequence in which the citations of a single patent occur in time
#The parameter t defines what time till next citation you want. For time till 
#the first citation t will equal 1
delta_t<- function(x,t){
	if(t>1){
		x$ind<- c(1:length(x[,1]))
		ifelse(t<1+max(x$seq),xt<- x[x$seq==t,],return("No patent at this lag"))
		xt1<- x[(xt$ind-1),]
		xt$dellag<- xt$lag-xt1$lag
		return(xt)
	}else{
		return(x[x$seq==1,])
	}
}


#This is how you load in the graph
#Before loading make sure that the graph is in your working directory
#You can change your working directory by using 'setwd("myNewWorkingDirectory")'

#This is the graph of the Largest Connected Component (LCC), its weights 
#are calculated by equation (1) in the manuscript
collabGraphLCC<- read.graph("coInventorNetworkLCC.graphml",format="graphml")

#This is the file that conatins the information pertaining to what patents are citing what,
#the time lag of the citation, and whether the citation is classed as a self citation etc...
data_citations<- read.table("citationDataLCC.csv",T,sep=",")


#An example of using the function 'delta_t(...)'. The below example computes the time
#till first citation for patents as well as finding the relevant subsets of the citations

timeTillFirstCitations<- delta_t(data_citations,1)
#Finding the subsets of self citations, in-citations, and out-citations for the different partitions
selfCit<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==1]
inCitInfomap<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupInfmp==1]
outCitInfomap<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupInfmp==0]

inCitGreedy<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupGreed==1]
outCitGreedy<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupGreed==0]

inCitLouvain<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupLouvain==1]
outCitLouvain<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupLouvain==0]

inCitPL<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupPl==1]
outCitPL<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupPl==0]

inCitRW<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupRw==1]
outCitRW<- timeTillFirstCitations$lag[timeTillFirstCitations$selfcit==0&timeTillFirstCitations$ingroupRw==0]





