-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenogi_functions.r
74 lines (65 loc) · 2.08 KB
/
denogi_functions.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
grepr <- function(pattern, x, ...){
idx <- grep(pattern, x, ...)
return(x[idx])
}
tablefix <- function(dat){
fill <- NULL
for (century in unique(centuries)){
temp <- dat[, c(1, which(centuries %in% century) + 1)]
names(temp) <- variables[1:4]
temp$century <- century
fill <- rbind(fill, temp)
}
return(fill)
}
FA_inits <- function(y, vrs){
y$init <- NA
for (i in 3:length(vrs)){ # min 3 for fantanal
combinations <- combn(vrs, i)
for (j in 1:ncol(combinations)){
variables <- combinations[, j]
dat_fa <- y[complete.cases(y[, variables]), variables]
try(fa <- factanal(dat_fa, 1, scores='Bartlet'))
y$init[match(rownames(fa$scores), rownames(y))] <- fa$scores
}
}
initxi <- y$init
initgamma <- t(apply(y[, variables], 2, function(x) lm(x ~ y$init)$coef))
initomega <- apply(y[, variables], 2, function(x) summary(lm(x ~ y$init))$sigma)
return(list(xi=initxi, gamma=initgamma, omega=initomega))
}
extract_from_coda <- function(coda, varname){
keep <- grep(varname, colnames(coda[[1]]))
# out <- coda[[1]][, keep]
out <- coda[, keep, drop=FALSE]
out <- as.matrix(out)
return(out)
}
sumstats <- function(x){
out <- c(mean(x), quantile(x, c(0.05, 0.5, 0.95)))
names(out) <- c('mean', 'q05', 'q50', 'q95')
return(out)
}
sumstatsDF <- function(coda){
out <- apply(coda, 2, sumstats)
out <- t(out)
out <- as.data.frame(out)
return(out)
}
vstrsplit <- function(strs, split){
spltlist <- strsplit(strs, split)
spltvec <- do.call(rbind, spltlist)
return(spltvec)
}
place_text <- function(d, countries, col=1, cex=0.6, clio=F){
# d should be an ordered dataframe
N <- nrow(d)
y_left <- seq(from=1, to=N, by=2)
x_left <- d$q05[y_left]
text_left <- countries[y_left]
text(x_left, y_left, text_left, cex=cex, pos=2, col=col)
y_right <- seq(from=2, to=N, by=2)
x_right <- d$q95[y_right]
text_right <- countries[y_right]
text(x_right, y_right, text_right, cex=cex, pos=4, col=col)
}