#' Autocatlytic Growth: Iterating differential equations (maps)
#'
#' @title Autocatlytic Growth: Iterating differential equations (maps)
#'
#' @param Y0 Initial value.
#' @param r Growth rate parameter.
#' @param k Carrying capacity.
#' @param N Length of the time series.
#' @param type One of: "driving" (default), "damping", "logistic", "vanGeert1991".
#'
#' @return A timeseries object of length N.
#' @export
#'
#' @author Fred Hasselman
#'
#' @family autocatalytic growth functions
#'
#' @examples
#' # The logistic map in the chaotic regime
#' growth.ac(Y0 = 0.01, r = 4, type = "logistic")
growth.ac <- function(Y0 = 0.01, r = 1, k = 1, N = 100, type = c("driving", "damping", "logistic", "vanGeert")[1]){
# Create a vector Y of length N, which has value Y0 at Y[1]
if(N>1){
Y <- as.numeric(c(Y0, rep(NA,N-2)))
# Conditional on the value of type ...
switch(type,
# Iterate N steps of the difference function with values passed for Y0, k and r.
driving = sapply(seq_along(Y), function(t) Y[[t+1]] <<- r * Y[t] ),
damping = k + sapply(seq_along(Y), function(t) Y[[t+1]] <<- - r * Y[t]^2 / k),
logistic = sapply(seq_along(Y), function(t) Y[[t+1]] <<- r * Y[t] * ((k - Y[t]) / k)),
vanGeert = sapply(seq_along(Y), function(t) Y[[t+1]] <<- Y[t] * (1 + r - r * Y[t] / k))
)}
return(ts(Y))
}
#' Conditional Autocatlytic Growth: Iterating differential equations (maps)
#'
#' @param Y0 Initial value
#' @param r Growth rate parameter
#' @param k Carrying capacity
#' @param cond Conditional rules passed as a data.frame of the form: cbind.data.frame(Y = ..., par = ..., val = ...)
#' @param N Length of the time series
#'
#' @export
#'
#' @author Fred Hasselman
#'
#' @family autocatalytic growth functions
#'
#' @examples
#' # Plot with the default settings
#' xyplot(growth.ac.cond())
#'
#' # The function such that it can take a set of conditional rules and apply them sequentially during the iterations.
#' # The conditional rules are passed as a `data.frame`
#' (cond <- cbind.data.frame(Y = c(0.2, 0.6), par = c("r", "r"), val = c(0.5, 0.1)))
#' xyplot(growth.ac.cond(cond=cond))
#'
#' # Combine a change of `r` and a change of `k`
#' (cond <- cbind.data.frame(Y = c(0.2, 1.99), par = c("r", "k"), val = c(0.5, 3)))
#' xyplot(growth.ac.cond(cond=cond))
#'
#' # A fantasy growth process
#' (cond <- cbind.data.frame(Y = c(0.1, 1.99, 1.999, 2.5, 2.9), par = c("r", "k", "r", "r","k"), val = c(0.3, 3, 0.9, 0.1, 1.3)))
#' xyplot(growth.ac.cond(cond=cond))
growth.ac.cond <- function(Y0 = 0.01, r = 0.1, k = 2, cond = cbind.data.frame(Y = 0.2, par = "r", val = 2), N = 100){
# Create a vector Y of length N, which has value Y0 at Y[1]
Y <- c(Y0, rep(NA, N-1))
# Iterate N steps of the difference equation with values passed for Y0, k and r.
cnt <- 1
for(t in seq_along(Y)){
# Check if the current value of Y is greater than the threshold for the current conditional rule in cond
if(Y[t] > cond$Y[cnt]){
# If the threshold is surpassed, change the parameter settings by evaluating: cond$par = cond$val
eval(parse(text = paste(cond$par[cnt], "=", cond$val[cnt])))
# Update the counter if there is another conditional rule in cond
if(cnt < nrow(cond)){cnt <- cnt + 1}
}
# Van Geert growth model
Y[[t+1]] <- Y[t] * (1 + r - r * Y[t] / k)
}
return(ts(Y))
}
# GRAPH PLOTTING ---------------------------------------------------------------
graph2svg <- function(TDM,pname){
# Create weighted Term-Term matrix
tTM <- as.matrix(TDM)
TTM <- tTM %*% t(tTM)
TTM <- log1p(TTM)
g <- graph.adjacency(TTM,weighted=T,mode="undirected",diag=F)
g <- simplify(g)
# Remove vertices that were used in the search query
Vrem <- which(V(g)$name %in% c("~dev~","~dys~","~sld~","development","children","dyslexia"))
g <- (g - V(g)$name[Vrem])
# Set colors and sizes for vertices
V(g)$degree <- degree(g)
rev <- scaleRange(log1p(V(g)$degree))
rev[rev<=0.3]<-0.3
V(g)$color <- rgb(scaleRange(V(g)$degree), 1-scaleRange(V(g)$degree), 0, rev)
V(g)$size <- 10*scaleRange(V(g)$degree)
V(g)$frame.color <- NA
# set vertex labels and their colors and sizes
V(g)$label <- V(g)$name
V(g)$label.color <- rgb(0, 0, 0, rev)
V(g)$label.cex <- scaleRange(V(g)$degree)+.1
# set edge width and color
rew <- scaleRange(E(g)$weight)
rew[rew<=0.3]<-0.3
E(g)$width <- 2*scaleRange(E(g)$weight)
E(g)$color <- rgb(.5, .5, 0, rew)
set.seed(958)
svg(paste(pname,sep=""),width=8,height=8)
plot(g, layout=layout.fruchterman.reingold(g))
dev.off()
return(g)
}
# Plot vertex neighbourhood
hoodGraph2svg <- function(TDM,Vname,pname){
# Create weighted Term-Term matrix
tTM <- as.matrix(TDM)
TTM <- tTM %*% t(tTM)
TTM <- log1p(TTM)
ig <- graph.adjacency(TTM,weighted=T,mode="undirected",diag=F)
ig <- simplify(ig)
# Remove vertices that were used in the search query
Vrem <- which(V(ig)$name %in% c("~dev~","~dys~","~sld~","development","children","dyslexia"))
ig <- (ig - V(ig)$name[Vrem])
# This is a deletion specific for the Neighbourhood graphs
Vrem <- which(V(ig)$name %in% c("~rdsp~","~imp~","~som~","~bod~","~mlt~"))
ig <- ig - V(ig)$name[Vrem]
idx <- which(V(ig)$name==Vname)
sg <- graph.neighborhood(ig, order = 1, nodes=V(ig)[idx], mode = 'all')[[1]]
# set colors and sizes for vertices
V(sg)$degree <- degree(sg)
rev<-scaleRange(log1p(V(sg)$degree))
rev[rev<=0.3]<-0.3
V(sg)$color <- rgb(scaleRange(V(sg)$degree), 1-scaleRange(log1p(V(sg)$degree*V(sg)$degree)), 0, rev)
V(sg)$size <- 35*scaleRange(V(sg)$degree)
V(sg)$frame.color <- NA
# set vertex labels and their colors and sizes
V(sg)$label <- V(sg)$name
V(sg)$label.color <- rgb(0, 0, 0, rev)
V(sg)$label.cex <- scaleRange(V(sg)$degree)
# set edge width and color
rew<-scaleRange(E(sg)$weight)
rew[rew<=0.3]<-0.3
E(sg)$width <- 6*scaleRange(E(sg)$weight)
E(sg)$color <- rgb(.5, .5, 0, rew)
idV <- which(V(sg)$name==Vname)
idE <- incident(sg,V(sg)[[idV]])
E(sg)$color[idE] <- rgb(0, 0, 1 ,0.8)
set.seed(958)
idx <- which(V(sg)$name==Vname)
svg(paste(pname,sep=""),width=8,height=8)
plot(sg,layout=layout.star(sg,center=V(sg)[idx]))
dev.off()
return(sg)
}
sliceTS<-function(TSmat,epochSz=1) {
# Slice columns of TSmat in epochs of size = epochSz
init(c("plyr"))
N<-dim(TSmat)
return(llply(seq(1,N[1],epochSz),function(i) TSmat[i:min(i+epochSz-1,N[1]),1:N[2]]))
}
fltrIT <- function(TS,f){
# Apply filtfilt to TS using f (filter settings)
require("signal")
return(filtfilt(f=f,x=TS))
}
SWtest0 <- function(g){
Nreps <- 10;
histr <- vector("integer",Nreps)
target<- round(mean(degree(g)))
now <- target/2
for(i in 1:Nreps){
gt <- watts.strogatz.game(dim=1, size=length(degree(g)), nei=now, 0)
histr[i] <- round(mean(degree(gt)))
ifelse(histr[i] %in% histr,break,{
ifelse(histr[i]>target,{now<-now-1},{
ifelse(histr[i]<target,{now<-now+1},{
break})
})
})
}
return(gt)
}
# SWtestV <- function(g,N){
# return(list(cp=transitivity(g,type="global"),cpR=transitivity(rewire(g,mode=c("simple"),niter=N),type="global"),lp=average.path.length(g), lpR=average.path.length(rewire(g,mode=c("simple"),niter=N))))
# }
SWtestE <- function(g,p=1,N=20){
values <- matrix(nrow=N,ncol=6,dimnames=list(c(1:N),c("cp","cpR","cp0","lp","lpR","lp0")))
for(n in 1:N) {
gt<-SWtest0(g)
values[n,] <- c(transitivity(g,type="localaverage"),transitivity(rewire.edges(g,prob=p),type="localaverage"),transitivity(gt,type="localaverage"),average.path.length(g),average.path.length(rewire.edges(g,prob=p)),average.path.length(gt))}
values[n,values[n,]==0] <- NA #values[n,values[n,]==0]+1e-8}
values <- cbind(values,(values[,1]/values[,2])/(values[,4]/values[,5]),(values[,1]/values[,3]),(values[,4]/values[,6]),((values[,1]/values[,3])/values[,2])/((values[,4]/values[,6])/values[,5]))
valuesSD <- data.frame(matrix(apply(values[,1:10],2,sd,na.rm=T),nrow=1,ncol=10,dimnames=list(c(1),c("cp","cpR","cp0","lp","lpR","lp0","SWI","cp:cp0","lp:lp0","SWIn"))))
valuesAV <- data.frame(matrix(colMeans(values[,1:10],na.rm=T),nrow=1,ncol=10,dimnames=list(c(1),c("cp","cpR","cp0","lp","lpR","lp0","SWI","cp:cp0","lp:lp0","SWIn"))))
return(list(valuesAV=valuesAV,valuesSD=valuesSD,valuesSE=valuesSD/sqrt(N)))
}
PLFsmall <- function(g){
reload <- FALSE
if("signal" %in% .packages()){
warning("signal:poly is loaded and stats:poly is needed... will unload package:signal, compute slope, and reload...")
reload <- TRUE
detach("package:signal", unload=TRUE)}
if(length(V(g))>100){warning("Vertices > 100, no need to use PLFsmall, use a binning procedure");break}
d<-degree(g,mode="all")
y<-hist(d,breaks=0:length(V(g)),plot=F)$density
y<-y[y>0]
if(length(y)==2){warning("Caution... Log-Log slope is a bridge (2 points)")}
if(length(y)<2){warning("Less than 2 points in Log-Log regression... aborting");break}
alpha=coef(lm(log(y) ~ stats::poly(log(1:length(y)), degree=1), na.action="na.exclude") )[2]
if(reload==TRUE){library(signal,verbose=FALSE,quietly=TRUE)}
return(alpha)
}
FDrel <- function(g){
d<-degree(g,mode="all")
nbreaks <- round(length(V(g))/2)-1
y<-hist(d,breaks=nbreaks,plot=F)$density
y<-y[y>0]
return(FD <- -sum(y*log2(y))/-(log2(1/length(y))))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.