File size: 4,136 Bytes
2f8aa81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Please run this script first to make sure you have all the necessary packages 
# installed for running the rest of the scripts in this R project

.libPaths(c("rlib/", .libPaths()))

if (!suppressPackageStartupMessages(require("pacman"))) { install.packages("pacman") }

pacman::p_load(
  here,
	tidyverse,
	reshape2,
	broom,
	glue,
	forcats,
	magrittr,
	stringr,
	purrr,
	rcompanion, 
	naniar,
	readr,
	tidyr,
	dplyr,
  openxlsx,
  jsonlite,
  readxl,
  flextable,
  officer,
  
	
	#INLA - related
	sp, 
	scico,
	BiocManager,
	foreach,
	
	MASS,
	matrixStats,
	
	#brms
#	brms,

	# imputation
	missForest,
	
	#plotting graphs
	scales,
	RColorBrewer,
	ggpubr,
	ggplot2,
	ggrepel,
	gplots,
	ggtree,
	ggridges,
	grid,
	gridExtra,
	scales,
	ggmap,
	psych, #for scatterplot matrix
	viridis,
	rlang,
	devtools,
	patchwork,
  ggnewscale,
  ggstance,
	
	#making maps
	mapdata,
	maptools,
	maps,
#  geoR,
  geosphere,
  fields,
	
	# phylogenetic packages
	ape,
	phytools,
	nlme,
	caper,
  MCMCglmm,

	
	# testing
	assertthat,
	beepr
)

# quiet down, tidyverse:
options(tidyverse.quiet = TRUE)
options(warn.conflicts = FALSE)
options(stringsAsFactors = FALSE)

GRAMBANK_LANGUAGES <- file.path("../..", "cldf", "languages.csv")
GRAMBANK_VALUES <- file.path("../..", "cldf", "values.csv")
GRAMBANK_PARAMETERS <- file.path("../..", "cldf", "parameters.csv")
GRAMBANK_CODES <- file.path("../..", "cldf", "codes.csv")

# The columns specifier for readr to parse ../cldf/values.csv
VALUES_COLSPEC <- c(
  ID = col_character(),
  Language_ID = col_character(),
  Parameter_ID = col_character(),
  Value = col_character(),
  Code_ID = col_character(),
  Comment = col_character(),
  Source = col_character()
)

LANGUAGES_COLSPEC = c(
  ID = col_character(),
  Name = col_character(),
  Macroarea = col_character(),
  Latitude = col_double(),
  Longitude = col_double(),
  Glottocode = col_character(),
  ISO639P3code = col_character(),
  contributed_datapoints = col_character(),
  provenance = col_character(),
  Family_name = col_character(),
  Family_id = col_character()
)

PARAMETERS_COLSPEC = c(
    ID = col_character(),
    Name = col_character(),
    Description = col_character(),
    patron = col_character(),
    name_in_french = col_character(),
    Grambank_ID_desc = col_character(),
    bound_morphology = col_character()
)

CODES_COLSPEC = c(
    ID = col_character(),
    Parameter_ID = col_character(),
    Name = col_character(),
    Description = col_character()
)

WIDE_COLSPEC = c(
    .default = col_integer(),
    Language_ID = col_character(),
    na_prop = col_double()
)

#creating folders
OUTPUTDIR_models <- here("output_models")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_models)) { dir.create(OUTPUTDIR_models) }	

OUTPUTDIR_tables <- here("output_tables")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_tables)) { dir.create(OUTPUTDIR_tables) }	

OUTPUTDIR_output <- here("output")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_output)) { dir.create(OUTPUTDIR_output) }	

OUTPUTDIR_data_wrangling<- here("data_wrangling")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_data_wrangling)) { dir.create(OUTPUTDIR_data_wrangling) }

OUTPUTDIR_models_reduced <- here("output_models_reduced")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_models_reduced)) { dir.create(OUTPUTDIR_models_reduced) }	
OUTPUTDIR_tables_reduced <- here("output_tables_reduced")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_tables_reduced)) { dir.create(OUTPUTDIR_tables_reduced) }	
OUTPUTDIR_output_reduced <- here("output_reduced")		
# create output dir if it does not exist.		
if (!dir.exists(OUTPUTDIR_output_reduced)) { dir.create(OUTPUTDIR_output_reduced) }	

#source custom functions to have them at hand
source("varcov.spatial_function.R")

#Adding a prior for modelling with INLA on precision/SD of random effects and likelihood: the probability of SD of each random effect and likelihood being > 1 is equal to 0.1
pcprior_hyper = list(prec =list(prior="pc.prec", param = c(1, 0.1)))