## import data file: daily maximum water levels/windspeed, called h h<- as.numeric(sample (1:500,size=10000,replace=TRUE)) # example numeric array for h, replace with real data ######## min WoO requirement ################ failure <- 3 # failure= period free from inundation which needs to be exceeded for seedlings to anchor after stranding # this is the loop to calculate inundation free days diff <- 0 maxdur <- array(data = NA, dim = (length(h)), dimnames = NULL) ## array for output: inundation free period maxel <- array(data = NA, dim = (length(h)), dimnames = NULL) ## array for output: water level which inundation free period is ## calculated for for (i in 1:paste(length(h) - 1)) { maxdur[i] <- 0 maxel[i] <- h[i] diff <- h[i] > h[i+1] if (diff == TRUE) { q=i+1 while (((h[i] > h[q])) && (q < length(h))) { maxdur[i] <- maxdur[i] + 1 q=q+1 } } } # ------------------------------------WoONa <- cbind(maxel, maxdur, deparse.level = 1) WoO <- na.omit(WoONa) ## deletes last day for which maxdur is NA disperse <- WoO[WoO[, 2] >= 1, ] ## selects all days where diaspore strand all WoO >= 1 day no inundation, ## all potential WoO plot(density(disperse[, 1])) ##density of all WoO <=1 day plot(disperse[, 1], disperse[, 2], xlab = "elevation", ylab = "WoO duration [days]") ## plots elevation against duration of WoO i.e. inundation free period all ## WoO which are defined as successful (longer than failure input) ## select all where windows occurred defWoO <- disperse[disperse[, 2] > failure, ] density(defWoO[, 1]) plot(density(defWoO[, 1])) ## density of all WoO which are defined successful ############# calculate P success ########## establish <- array(data = NA, dim = length(disperse[, 1]), dimnames = NULL) ### marks for all dispersal events, if minimum duration of WoO is > ### failure threshold for (i in 1:length(disperse[, 1])) { dispersal <- (disperse[i, 2]) > failure seedling <- dispersal == TRUE if (seedling == TRUE) { establish[i] <- 1 } else { establish[i] <- 0 } } WoOfin <- cbind(establish, disperse) ### probability of survival for different elevations, P success #### d1 <- WoOfin[WoOfin[, 1] == 1, ] step <- 0:ceiling(sqrt(length(d1[, 1]))) ## sqrt of N,round up, define number of steps for probability analysis ## along height gradient Pvalue <- array(data = NA, dim = max(step), dimnames = NULL) fromheight <- array(data = NA, dim = max(step), dimnames = NULL) toheight <- array(data = NA, dim = max(step), dimnames = NULL) PvalueNo <- array(data = NA, dim = max(step), dimnames = NULL) stepvector <- array(data = NA, dim = max(step), dimnames = NULL) startel <- min(WoOfin[, 2]) stepsize <- as.numeric((max(WoOfin[, 2]) - min(WoOfin[, 2]))/max(step)) ## calculate size of steps probability calculation for (i in 0:(max(step) - 1)) { Pselect2 <- 0 Pselect1 <- 0 Pselect2 <- subset(WoOfin, (WoOfin[, 2] >= (startel + (stepsize * i))),) Pselect1 <- subset(Pselect2, (Pselect2[, 2] <= (startel + (stepsize * (1 +i)))), ) Pvalue[i] <- (sum(Pselect1[, 1])/c(length(Pselect1[, 1]))) PvalueNo[i] <- (sum(Pselect1[, 1])/c(length(Pselect1[, 1]))) * sum(Pselect1[,1]) fromheight[i] <- as.numeric(startel + (stepsize * (i - 1))) toheight[i] <- as.numeric(startel + (stepsize * (i))) } Pvaluedeath <- c(1 - Pvalue) ## P value for failure ## plot results: density for all successful WoO and Psuccess b3 <- density(defWoO[, 1]) ## all established seedlings density plot(fromheight, Pvalue, type = "l", col = "red", axes = "false", lwd = 4, xlab = "elevation",ylab = "P success") par(mar = c(5, 4, 4, 4)) box(which = "plot", lty = "solid", lwd = 3) mtext(4, text = "density", line = 2) axis(2, col = "red", lwd = 3, ylim = c(0, 1), cex.axis = 1.5) par(usr = c(par("usr")[1:2], 0, c((max(b3$y)) + 1e-05))) lines(b3, lwd = 3) axis(4, lwd = 3, cex.axis = 1.5) axis(1, lwd = 3, cex.axis = 1.5) ## show histogramm of duration of all WoO >=1 day for max 100 days hist(disperse[, 2], breaks = c(max(disperse[, 2])), xlim = c(0, 100), cex.axis = 1.5,xlab = "WoO length",ylab = "frequency")