if (containsNaN) output <- c(output, NaN)
output
}
Unique(l)
l <- list(list('a','b',1,T,as.numeric(1), as.integer(1)),1,2,3,3,2,NA, list(NaN,NA,NaN,NaN))
Unique(l)
x <- list(list(c(T,T,T,NA,F), NULL, c(1,2,NA,3,100)),
c(7,"fish","dog"), "fish", "fish", "cow", "kitchen sink", NaN,
c(sample(1:100,10,replace=TRUE)), 7+5i, "7+5i", "4",4)
Unique(x)
x <- list(list(c(T,T,T,NA,F), c(1,2,NA,3,100)),
c(7,"fish","dog"), "fish", "fish", "cow", "kitchen sink", NaN,
c(sample(1:100,10,replace=TRUE)), 7+5i, "7+5i", "4",4)
Unique(x)
warnings()
list(1:5)
as.list(1:5)
Unique <- function(list.orig) {
output <- list()
containsNA <- F
containsNaN <- F
addItem <- function(item) {
i <- 0
found <- F
if (is.nan(item)) {  # avoiding troubles with NA & NaN and ==
found <- T
if (!containsNaN) containsNaN <<- T
} else if (is.na(item)) {
found <- T
if (!containsNA) containsNA <<- T
}
while(!found & i < length(output)) {
i <- i + 1
if (output[[i]] == item & class(output[[i]]) == class(item)) found <- T
}
if (!found) output <<- c(output, item)
}
unlistRecur <- function(x) {
if (class(x) == 'list') lapply(x, function(y) unlistRecur(y))#, file='/dev/null')
else if (length(x) > 1) lapply(as.list(x), function(y) unlistRecur(y))
else addItem(x)
}
unlistRecur(list.orig)
if (containsNA) output <- c(output, NA)
if (containsNaN) output <- c(output, NaN)
output
}
Unique(l)
Unique(x)
x <- list(list(c(T,T,T,NA,F), c(1,2,NA,3,100)),
c(7,"fish","dog"), "fish", NULL, "fish", "cow", "kitchen sink", NaN,
c(sample(1:100,10,replace=TRUE)), 7+5i, "7+5i", "4",4)
x
Unique(x)
is.null(NULL)
c(1,NULL,2)
list(1,NULL,2)
is.na(NULL)
is.null(NULL)
Unique <- function(list.orig) {
output <- list()
containsNA <- F
containsNaN <- F
containsNULL <- F
addItem <- function(item) {
i <- 0
found <- F
if (is.null(item)) {  # avoiding troubles with NULL, NA & NaN
found <- T
containsNULL <<- T
} else if (is.nan(item)) {
found <- T
containsNaN <<- T
} else if (is.na(item)) {
found <- T
containsNA <<- T
}
while(!found & i < length(output)) {
i <- i + 1
if (output[[i]] == item & class(output[[i]]) == class(item)) found <- T
}
if (!found) output <<- c(output, item)
}
unlistRecur <- function(x) {
if (class(x) == 'list') lapply(x, function(y) unlistRecur(y))#, file='/dev/null')
else if (length(x) > 1) lapply(as.list(x), function(y) unlistRecur(y))
else addItem(x)
}
unlistRecur(list.orig)
if (containsNULL) output <- c(output, NULL)
if (containsNA) output <- c(output, NA)
if (containsNaN) output <- c(output, NaN)
output
}
?system.time
system.time(for(i in 1:1000) mean(i))
system.time(for(i in 1:10000) mean(i))
bb
bb <- T
system.time(for(i in 1:1000) if (!bb) bb<-T)
warnings()
system.time(for(i in 1:1000) if (!bb) bb<<-T)
system.time(for(i in 1:1000) if bb<<-T)
system.time(for(i in 1:1000) bb<-T)
system.time(for(i in 1:10000) bb<-T)
system.time(for(i in 1:100000) bb<-T)
system.time(for(i in 1:1000000) bb<-T)
bb <- F
system.time(for(i in 1:1000000) bb<-T)
bb
bb <- F
system.time(for(i in 1:1000000) if (!bb) bb<-T)
system.time(for(i in 1:1000000) if (!bb) bb<-T)
system.time(for(i in 1:1000000) bb<-T)
x
x
Unique(x)
c(l,NULL)
c(l,list(NULL))
Unique <- function(list.orig) {
output <- list()
containsNA <- F
containsNaN <- F
containsNULL <- F
addItem <- function(item) {
i <- 0
found <- F
if (is.null(item)) {  # avoiding troubles with NULL, NA & NaN
found <- T
containsNULL <<- T
} else if (is.nan(item)) {
found <- T
containsNaN <<- T
} else if (is.na(item)) {
found <- T
containsNA <<- T
}
while(!found & i < length(output)) {
i <- i + 1
if (output[[i]] == item & class(output[[i]]) == class(item)) found <- T
}
if (!found) output <<- c(output, item)
}
unlistRecur <- function(x) {
if (class(x) == 'list') lapply(x, function(y) unlistRecur(y))#, file='/dev/null')
else if (length(x) > 1) lapply(as.list(x), function(y) unlistRecur(y))
else addItem(x)
}
unlistRecur(list.orig)
if (containsNULL) output <- c(output, list(NULL))
if (containsNA) output <- c(output, NA)
if (containsNaN) output <- c(output, NaN)
output
}
Unique(x)
x.list.mixed.2 <- list(list(c(TRUE,TRUE,TRUE),FALSE,99, 99L, "99",c(1,2,3,100),1:5),
c(7,"fish","dog", "kitchen sink", 7+5i),
"fish",
"fish",
"cow",
"kitchen sink",
c(sample(10:15,25,replace=TRUE)),
c(sample(as.numeric(300:305),25,replace=TRUE)),
7+5i,
"7+5i",
"4",
"3",
"2",
4)
x.list.mixed.3 <- list(list(c(TRUE,TRUE,TRUE,FALSE),c(1,2,3,100),1:5),
c(7,"fish","dog", "kitchen sink", 7+5i),
"fish",
"fish",
"cow",
"kitchen sink",
c(sample(c("aa","bb","cc","dd","ee"),25,replace=TRUE)),
7+5i,
"7+5i",
"4",
"3",
"2",
4,
list(list(a=-94),134))
str(Unique(x.list.mixed.2))
system.time(Unique(x.list.mixed.2))     # brutal?
system.time(Unique(x.list.mixed.3))     # brutal?
Unique(rnorm(100))
Unique(rnorm(1000))
unique(rnorm(1000))
source('/media/Data/Dropbox/skola/dipl/data/scripts/diplLoadAll.R')
x <- createTreeDF('cb')
x
tableP <- function(ps) {
ps.out <- decimalComma(ps, 2)
ps.out[ps < .01] <- '**'
ps.out[ps < .001] <- '***'
ps.out
}
emphIfSignif <- function(x, p) {
ifelse(p < .05, paste0('<b>', x, '</b>'), x)
}
writeVarSpeTable <- function(vars.list, file, which=1) {
var.table <- NULL
for (tree in c(names(tree.names.cs))) {
#   tree.table <- vars.list[[tree]][c('coef', 'dev0', 'p0', 'dev.adj', 'p')]
if (which) tree.table <- vars.list[[tree]][c('coef', 'dev.adj', 'p')]
else tree.table <- vars.list[[tree]][c('coef', 'dev0', 'p0')]
names(tree.table) <- c('coef', 'dev', 'p')
tree.var.p <- tree.table$p
tree.table$coef <- emphIfSignif(ifelse(tree.table$coef > 0, '+', '&minus;'), tree.var.p)
#   tree.table$dev0 <- emphIfSignif(ifelse(tree.table$dev0 < 0.01,
#                                          '&lthan;1', round(tree.table$dev0 * 100)), tree.var.p)
#   tree.table$p0 <- emphIfSignif(tableP(tree.table$p0), tree.var.p)
tree.table$dev <- emphIfSignif(ifelse(tree.table$dev < 0.01,
'&lthan;1', round(tree.table$dev * 100)), tree.var.p)
tree.table$p <- emphIfSignif(tableP(tree.table$p), tree.var.p)
names(tree.table) <- paste(tree, names(tree.table), sep='.')
tree.table$expl <- vars.list[[tree]][, 'expl']
if (is.null(var.table)) {
var.table <- tree.table
} else var.table <- merge(var.table, tree.table, by='expl', all=T)
}
row.names(var.table) <- var.table$expl
var.table <- var.table[c("tree.plot", "tree.plot.ratio", "tree.50", "tree.500", "tree.closest",
"ca", "mg", "c", "n", "c.n", "p", "k", "ph", "humid", "cover",
"LAI.4.ring", "plocha"), ]
var.table$expl <- c("FJ na ploše", "FJ na ploše (&percent;)", "FJ v porostu", "FJ v krajině", "nejbližší FJ",
"Ca", "Mg", "C", "N", "C/N", "P", "K", "pH", "vlhkost", "pokryvnost E<sub>3</sub>",
"LAI 4 ring", "lokalita")
var.table['tree.closest', grep('q', names(var.table))] <- NA
var.table[is.na(var.table)] <- '.'
# var.table <- rbind(c('Proměnná', rep(c('Vliv', 'VD<sub>0</sub>', 'p<sub>0</sub>',
#                                        'VD<sub>adj</sub>', 'p<sub>adj</sub>'), 7)),
#                    var.table)
var.table <- rbind(c('Proměnná', rep(c('Vliv', 'VD', 'p'), 7)),
var.table)
write.table(var.table, paste0('/media/Data/skola/dipl/text/img/', file), sep='&amper;', eol='\\\\\n',
row.names=F, col.names=F, quote=F)
var.table
}
writeVarSpeTable(vars.list, 'varSpeTable0.csv', which=0)
setwd("~")
if (getwd() == "D:/") basedir <- "D:/skola/dipl/" else basedir <- "/media/Data/skola/dipl/"
img.path <- function(filename) paste0(basedir, "text/img/", filename)
setwd(paste0(basedir, "data/csv/working"))
empty.cell <- '.'
trees.order.cs <- c('ac', 'st', 'q', 'cb', 'fx', 'tc')
tree.names.cs <- c("babyka", "břek", "dub", "habr", "jasan", "lípa")
names(tree.names.cs) <- trees.order.cs
loc.abb.cs <- c('BUD', 'ČK', 'KB', 'M', 'V8', 'V9')
#  "d"    "dh"   "jehl" "list" "luh"  "mix"  "0"
veg.colors <- paste0(c('#EB4231', '#74D72A', '#1649F1', '#78562C', '#C558E2', '#F3EB0A', gray(.8)), 'AA')
veg.labels <- c('doubrava', 'dubohabřina', 'jehličnany', 'nepůvodní listnáče',
'lužní les', 'smíšený porost', 'bezlesí')
library(Cairo)
CairoFonts(
regular="Myriad Pro:style=Regular",
bold="Myriad Pro:style=Bold",
italic="Myriad Pro:style=Italic",
bolditalic="Myriad Pro:style=Bold Italic,BoldItalic",
symbol="Symbol"
)
cm.to.pt <- function(x) x/0.035148
textwidth <- 469.47046   # textwidth in pt
fontsize <- 11.5
lwd.plots <- .6
title.cex <- 1.5
decimalComma <- function(x, digits=2, auto.digits=F, no.zero=T) {
if (auto.digits) digits <- max(sapply(strsplit(as.character(x), '\\.'),
function(y) ifelse(is.na(y[2]), 0, nchar(y[2]))))
is.zero <- which(round(x, digits)==0)
result <- sub('[.]', ',', sprintf(paste0("%.", digits, "f"), x))
if (no.zero) result[is.zero] <- paste0('&lthan;', sub('[.]', ',', 10^(-digits)))
result
}
axisWithCommas <- function(...) {
x <- axis(..., labels=F, tick=F)
axis(..., labels=decimalComma(x, auto.digits=T, no.zero=F), at=x)
}
addTableFormatLine <- function(table, commands, at=1) {
if (ncol(table) == 1) rbind(table[0:(at-1), ], commands, table[at:nrow(table), ])
else rbind(table[0:(at-1), ],
c(paste0(commands, '<ignore>'), rep('', ncol(table) - 2), '</ignore>'),
table[at:nrow(table), ])
}
# Species (frequency) count of occurencess in a table
speFreq <- function(spe, df, suffix='.sum') {
#freq <- tryCatch(sum(df[paste0(spe, suffix)] > 0) / nrow(df) * 100, error=function(e) empty.cell)
freq <- tryCatch(sum(df[paste0(spe, suffix)] > 0), error=function(e) empty.cell)
if (freq == 0 | freq == empty.cell) return(empty.cell)
freq
#freq <- round(freq)
#if (freq < .5) '&lthan;0,5' else freq
}
speMed <- function(spe, df, suffix='.sum') {
freq <- speFreq(spe, df, suffix)
if (freq == empty.cell) return(empty.cell)  # species is not present
round(median(unlist(df[paste0(spe, suffix)])))
}
speMean <- function(spe, df, suffix='.sum') {
freq <- speFreq(spe, df, suffix)
if (freq == empty.cell) return(empty.cell)  # species is not present
temp <- round(mean(unlist(df[paste0(spe, suffix)])))
temp[temp=='0'] <- '&lthan;0,5'
temp
}
speClosest <- function(spe) {
if (spe == 'q') return(0)
temp <- round(tryCatch(mean(unlist(trees.closest[spe])), error=function(e) 2000))
if (temp > 200) '&gthan;200' else temp
}
loc.cols <- veg.colors[c(2, 1, 2, 1, 1, 1)]
loc.cols[5] <- paste0(rgb(t((col2rgb(veg.colors[1]) + col2rgb(veg.colors[2]))/2), maxColorValue=255),
substr(loc.cols[1], 8, 9))
## Draws a weighted Venn diagram
# x  variability left
# y  variability right
# xy  shared variability (x + y)
drawVennDiagram <- function(x, y, xy, ratio=1.1, ratio.inner=1, ratio.inner.adj=1,
col.x='#ff440044', col.y='#33cc2244', ann=T,
ann.pos=rep('in', 3), percent=F, title=NULL,
ann.hadj=rep(0, 3), ann.vadj=rep(0, 3), ...) {
if (!percent) {
x <- x * 100
y <- y * 100
xy <- xy * 100
}
xy <- x + y - xy
if (xy < 0) xy <- 0
draw.xy <- T
print(paste('x', x, 'y', y, 'xy', xy))
ht <- sqrt(100 / ratio)
wd <- ht * ratio
over <- xy / min(x, y)
ht1 <- sqrt(x / ratio.inner)
wd1 <- ht1 * ratio.inner
ht2 <- sqrt(y / (ratio.inner * ratio.inner.adj))
wd2 <- ht2 * ratio.inner * ratio.inner.adj
wd.over <- min(wd1, wd2) * over
wd.total <- wd1 + wd2 - wd.over
#   if (!is.null(title)) par(mar=c(0, 0, 2, 4))
#   else par(mar=rep(0, 4))
plot(c(0, wd), c(0, ht), type='n', bty='n', xlab='', ylab='', xaxt='n', yaxt='n', main=title, ...)
rect(0, 0, wd, ht, lwd=.7)
x <- round(x)
xy <- round(xy)
y <- round(y)
# one variability is zero or less
if (x <= 0 || y <= 0) {
if (y <= 0) {
wd.one <- wd1
ht.one <- ht1
col.one <- col.x
perc.one <- x
ann.pos.one <- ann.pos[1]
ann.hadj.one <- ann.hadj[1]
ann.vadj.one <- ann.vadj[1]
} else {
wd.one <- wd2
ht.one <- ht2
col.one <- col.y
perc.one <- y
ann.pos.one <- ann.pos[3]
ann.hadj.one <- ann.hadj[2]
ann.vadj.one <- ann.vadj[2]
}
margin.x <- (wd - wd.one) / 2
rect(margin.x, (ht - ht.one) / 2, wd - margin.x, ht / 2 + ht.one / 2,
col=col.one, border=substr(col.one, 1, 7))
if (ann) {
if (ann.pos.one=='above') text(wd/2 + ann.hadj.one, ht/2 + ht.one/2 + ann.vadj.one,
paste(perc.one, ' %'), pos=3)
else text(wd/2 + ann.hadj.one, ht/2 + ann.vadj.one, paste(perc.one, ' %'), pos=3)
}
return()
}
if (xy > min(x, y)) {wd.total <- wd1 + wd2 + .1 * wd; draw.xy <- F}
if (xy <= 0) draw.xy <- F
margin.x <- (wd - wd.total) / 2
rect(margin.x, (ht - ht1) / 2, margin.x + wd1, ht - (ht - ht1) / 2,
col=col.x, border=substr(col.x, 1, 7))
rect(wd - margin.x - wd2, (ht - ht2) / 2, wd - margin.x, ht - (ht - ht2) / 2,
col=col.y, border=substr(col.y, 1, 7))
if (ann) {
if (ann.pos[1]=='above') text(margin.x + ann.hadj[1],
ht/2 + ht1/2 + ann.vadj[1], paste(x-xy, ' %'), pos=3)
else text(margin.x + (wd1-wd.over)/2 + ann.hadj[1], ht/2 + ann.vadj[1], paste(x-xy, ' %'), pos=3)
if (draw.xy) {
if (ann.pos[3]=='above') text(margin.x + wd1 - wd.over/2 + ann.hadj[3],
ht/2 + max(ht1, ht2)/2 + ann.vadj[3], paste(xy, ' %'), pos=3)
else text(margin.x + wd1 - wd.over/2 + ann.hadj[3], ht/2 + ann.vadj[3], paste(xy, ' %'), pos=3)
}
if (ann.pos[2]=='above') text(wd - margin.x + ann.hadj[2],
ht/2 + ht2/2 + ann.vadj[2], paste(y-xy, ' %'), pos=3)
else text(margin.x + wd1 + (wd2-wd.over)/2 + ann.hadj[2], ht/2 + ann.vadj[2], paste(y-xy, ' %'), pos=3)
}
}
writeVarSpeTable(vars.list, 'varSpeTable0.csv', which=0)
getwd()
load('../../results/varSpeTablesAdj.RData')
writeVarSpeTable(vars.list, 'varSpeTable0.csv', which=0)
writeVarSpeTable <- function(vars.list, file, which=1) {
var.table <- NULL
for (tree in c(names(tree.names.cs))) {
#   tree.table <- vars.list[[tree]][c('coef', 'dev0', 'p0', 'dev.adj', 'p')]
if (which) tree.table <- vars.list[[tree]][c('coef', 'dev.adj', 'p')]
else tree.table <- vars.list[[tree]][c('coef', 'dev0', 'p0')]
names(tree.table) <- c('coef', 'dev', 'p')
tree.var.p <- tree.table$p
tree.table$coef <- emphIfSignif(ifelse(tree.table$coef > 0, '+', '&minus;'), tree.var.p)
#   tree.table$dev0 <- emphIfSignif(ifelse(tree.table$dev0 < 0.01,
#                                          '&lthan;1', round(tree.table$dev0 * 100)), tree.var.p)
#   tree.table$p0 <- emphIfSignif(tableP(tree.table$p0), tree.var.p)
tree.table$dev <- emphIfSignif(ifelse(tree.table$dev < 0.01,
'&lthan;1', round(tree.table$dev * 100)), tree.var.p)
tree.table$p <- emphIfSignif(tableP(tree.table$p), tree.var.p)
names(tree.table) <- paste(tree, names(tree.table), sep='.')
tree.table$expl <- vars.list[[tree]][, 'expl']
if (is.null(var.table)) {
var.table <- tree.table
} else var.table <- merge(var.table, tree.table, by='expl', all=T)
}
row.names(var.table) <- var.table$expl
var.table <- var.table[c("tree.plot", "tree.plot.ratio", "tree.50", "tree.500", "tree.closest",
"ca", "mg", "c", "n", "c.n", "p", "k", "ph", "humid", "cover",
"LAI.4.ring", "plocha"), ]
var.table$expl <- c("FJ na ploše", "FJ na ploše (&percent;)", "FJ v porostu", "FJ v krajině", "nejbližší FJ",
"Ca", "Mg", "C", "N", "C/N", "P", "K", "pH", "vlhkost", "pokryvnost E<sub>3</sub>",
"LAI 4 Ring", "lokalita")
var.table['tree.closest', grep('q', names(var.table))] <- NA
var.table[is.na(var.table)] <- '.'
# var.table <- rbind(c('Proměnná', rep(c('Vliv', 'VD<sub>0</sub>', 'p<sub>0</sub>',
#                                        'VD<sub>adj</sub>', 'p<sub>adj</sub>'), 7)),
#                    var.table)
var.table <- rbind(c('Proměnná', rep(c('Vliv', 'VD', 'p'), 7)),
var.table)
write.table(var.table, paste0('/media/Data/skola/dipl/text/img/', file), sep='&amper;', eol='\\\\\n',
row.names=F, col.names=F, quote=F)
var.table
}
writeVarSpeTable(vars.list, 'varSpeTable.csv')
writeVarSpeTable(vars.list, 'varSpeTable0.csv', which=0)
distClass(1)
distClass(15)
distClass(9)
distClass(8)
distClass(7)
distClass(600)
distClass(500)
tdf.rp <- createTreeDF('rp')
m <- glm(gener.all ~ tree.50, data=)
y <- as.numeric(tdf.rp$gener.all >0)
y
m <- glm(y ~ tree.50, data=tdf.rp, family=binomial)
summary(m)
plot(m)
citation
citation(MASS)
citation('MASS')
citation(package='MASS')
setwd('../../fmv/')
perm.type.temp <- "all"
for (tree in names(tree.names.cs)) {
print(paste('--------', tree, '---------'))
assign(paste0('tdf.', tree), createTreeDF(tree))
print('-------- all ---------')
assign(paste0('fmv.', tree),
findMaxVariability('gener.all', 'all', get(paste0('tdf.', tree)),
perm.type=perm.type.temp, perm.count=50))
save(list=paste0('fmv.', tree), file=paste('fmv', perm.type.temp, tree, 'RData', sep='.'))
print('-------- space ---------')
assign(paste0('fmv.space.', tree),
findMaxVariability('gener.all', 'space', get(paste0('tdf.', tree)),
perm.type=perm.type.temp, perm.count=50))
save(list=paste0('fmv.space.', tree), file=paste('fmv.space', perm.type.temp, tree, 'RData', sep='.'))
print('-------- env ---------')
assign(paste0('fmv.env.', tree),
findMaxVariability('gener.all', 'env', get(paste0('tdf.', tree)),
perm.type=perm.type.temp, perm.count=50))
save(list=paste0('fmv.env.', tree), file=paste('fmv.env', perm.type.temp, tree, 'RData', sep='.'))
}
par(mfrow=c(3, 2), mar=c(.1, .1, 2.3, 3))
ratios.adj <- rep(.9, 6)
ratios.adj[1] <- .8
ann.hadj <- lapply(rep(0, 6), rep, times=3)
ann.vadj <- lapply(rep(-.3, 6), rep, times=3)
ann.hadj[[2]] <- c(-.8, 0, 0)
for(i in seq_along(trees.order.cs)) {
tree <- trees.order.cs[i]
drawVennDiagram(max(get(paste0('fmv.space.', tree))$dev.adj),
max(get(paste0('fmv.env.', tree))$dev.adj),
max(get(paste0('fmv.', tree))$dev.adj),
ratio.inner=ratios.adj[i],
ann.hadj=ann.hadj[[i]], ann.vadj=ann.vadj[[i]])
title(main=tree.names.cs[tree], line=.2, cex.main=title.cex)
}
dev.off()
par(mfrow=c(3, 2), mar=c(.1, .1, 2.3, 3))
ratios.adj <- rep(.9, 6)
ratios.adj[1] <- .8
ann.hadj <- lapply(rep(0, 6), rep, times=3)
ann.vadj <- lapply(rep(-.3, 6), rep, times=3)
ann.hadj[[2]] <- c(-.8, 0, 0)
for(i in seq_along(trees.order.cs)) {
tree <- trees.order.cs[i]
drawVennDiagram(max(get(paste0('fmv.space.', tree))$dev.adj),
max(get(paste0('fmv.env.', tree))$dev.adj),
max(get(paste0('fmv.', tree))$dev.adj),
ratio.inner=ratios.adj[i],
ann.hadj=ann.hadj[[i]], ann.vadj=ann.vadj[[i]])
title(main=tree.names.cs[tree], line=.2, cex.main=title.cex)
}
