Commit
·
f4102f9
1
Parent(s):
96929ce
add 101
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- 101/paper.pdf +3 -0
- 101/replication_package/.gitignore +30 -0
- 101/replication_package/.gitmodules +0 -0
- 101/replication_package/LICENSE.md +395 -0
- 101/replication_package/README.md +105 -0
- 101/replication_package/WALS_reanalysis_controlled_setup.R +140 -0
- 101/replication_package/WALS_reanalysis_controlled_setup_high_coverage.R +181 -0
- 101/replication_package/WALS_reanalysis_setup.R +40 -0
- 101/replication_package/WALS_sparseness.R +70 -0
- 101/replication_package/all_scripts.R +91 -0
- 101/replication_package/assigning_AUTOTYP_areas.R +85 -0
- 101/replication_package/create_pop_table.R +117 -0
- 101/replication_package/creating_boundness_metric.R +48 -0
- 101/replication_package/creating_informativity_score.R +51 -0
- 101/replication_package/data/GB_wide/parameters.csv +3 -0
- 101/replication_package/data/complexity_data_WALS.csv +3 -0
- 101/replication_package/data/glottolog-cldf_wide_df.tsv +3 -0
- 101/replication_package/data/lang_endangerment_predictors.xlsx +3 -0
- 101/replication_package/data/phylogenies/EDGE6635-merged-relabelled.tree +3 -0
- 101/replication_package/data_wrangling/ethnologue_pop_SM.tsv +3 -0
- 101/replication_package/data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv +3 -0
- 101/replication_package/data_wrangling/glottolog_AUTOTYPE_areas.tsv +3 -0
- 101/replication_package/data_wrangling/glottolog_cldf_wide_df.tsv +3 -0
- 101/replication_package/data_wrangling/pop_reduced.tsv +3 -0
- 101/replication_package/data_wrangling/pop_reduced_with_ISO.tsv +3 -0
- 101/replication_package/data_wrangling/wrangled.tree +3 -0
- 101/replication_package/generating_GB_input_file.R +42 -0
- 101/replication_package/get_external_data.R +46 -0
- 101/replication_package/install_and_load_INLA.R +22 -0
- 101/replication_package/make_ethnologue_SM_and_merging_tables.R +54 -0
- 101/replication_package/make_ethnologue_SM_for_morphological_complexity_reanalysis.R +37 -0
- 101/replication_package/measuring_phylosignal.R +48 -0
- 101/replication_package/models_Boundness_phylogenetic_spatial.R +412 -0
- 101/replication_package/models_Boundness_reduced_social.R +567 -0
- 101/replication_package/models_Boundness_reduced_social_only.R +433 -0
- 101/replication_package/models_Boundness_social.R +560 -0
- 101/replication_package/models_Boundness_social_only.R +452 -0
- 101/replication_package/models_Informativity_phylogenetic_spatial.R +414 -0
- 101/replication_package/models_Informativity_reduced_social.R +537 -0
- 101/replication_package/models_Informativity_reduced_social_only.R +435 -0
- 101/replication_package/models_Informativity_social.R +556 -0
- 101/replication_package/models_Informativity_social_only.R +454 -0
- 101/replication_package/output/Bound_morph/bound_morph_score.tsv +3 -0
- 101/replication_package/output/Informativity/informativity_score.tsv +3 -0
- 101/replication_package/output_tables/ effects Boundness_phylogenetic_spatial_models .csv +3 -0
- 101/replication_package/output_tables/ effects Boundness_social_models .csv +3 -0
- 101/replication_package/output_tables/ effects Boundness_social_models prior_0.01 .csv +3 -0
- 101/replication_package/output_tables/ effects Boundness_social_models prior_0.5 .csv +3 -0
- 101/replication_package/output_tables/ effects Boundness_social_models prior_0.99 .csv +3 -0
- 101/replication_package/output_tables/ effects Boundness_social_only_models .csv +3 -0
101/paper.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0288699f0b4a67532df6cb1a3dd6391f46688831a3b092bea21f0fb35e1889d8
|
3 |
+
size 3262536
|
101/replication_package/.gitignore
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.Rproj.user
|
2 |
+
.Rhistory
|
3 |
+
.RData
|
4 |
+
.Ruserdata
|
5 |
+
.DS_Store
|
6 |
+
*.Rproj
|
7 |
+
|
8 |
+
*.RData
|
9 |
+
Table_of_Languages.tab
|
10 |
+
ethnologue_pop_full.tsv
|
11 |
+
pop.tsv
|
12 |
+
pop_full.tsv
|
13 |
+
GB_wide_strict.tsv
|
14 |
+
*.png
|
15 |
+
.xlsx
|
16 |
+
*.jpg
|
17 |
+
*.svg
|
18 |
+
*.tiff
|
19 |
+
*.emf
|
20 |
+
*.eps
|
21 |
+
*.ps
|
22 |
+
*.wmf
|
23 |
+
*.pptx
|
24 |
+
*.docx
|
25 |
+
*.pdf
|
26 |
+
*.jpeg
|
27 |
+
*.qs
|
28 |
+
|
29 |
+
#folders
|
30 |
+
*grambank-analysed*
|
101/replication_package/.gitmodules
ADDED
File without changes
|
101/replication_package/LICENSE.md
ADDED
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Attribution 4.0 International
|
2 |
+
|
3 |
+
=======================================================================
|
4 |
+
|
5 |
+
Creative Commons Corporation ("Creative Commons") is not a law firm and
|
6 |
+
does not provide legal services or legal advice. Distribution of
|
7 |
+
Creative Commons public licenses does not create a lawyer-client or
|
8 |
+
other relationship. Creative Commons makes its licenses and related
|
9 |
+
information available on an "as-is" basis. Creative Commons gives no
|
10 |
+
warranties regarding its licenses, any material licensed under their
|
11 |
+
terms and conditions, or any related information. Creative Commons
|
12 |
+
disclaims all liability for damages resulting from their use to the
|
13 |
+
fullest extent possible.
|
14 |
+
|
15 |
+
Using Creative Commons Public Licenses
|
16 |
+
|
17 |
+
Creative Commons public licenses provide a standard set of terms and
|
18 |
+
conditions that creators and other rights holders may use to share
|
19 |
+
original works of authorship and other material subject to copyright
|
20 |
+
and certain other rights specified in the public license below. The
|
21 |
+
following considerations are for informational purposes only, are not
|
22 |
+
exhaustive, and do not form part of our licenses.
|
23 |
+
|
24 |
+
Considerations for licensors: Our public licenses are
|
25 |
+
intended for use by those authorized to give the public
|
26 |
+
permission to use material in ways otherwise restricted by
|
27 |
+
copyright and certain other rights. Our licenses are
|
28 |
+
irrevocable. Licensors should read and understand the terms
|
29 |
+
and conditions of the license they choose before applying it.
|
30 |
+
Licensors should also secure all rights necessary before
|
31 |
+
applying our licenses so that the public can reuse the
|
32 |
+
material as expected. Licensors should clearly mark any
|
33 |
+
material not subject to the license. This includes other CC-
|
34 |
+
licensed material, or material used under an exception or
|
35 |
+
limitation to copyright. More considerations for licensors:
|
36 |
+
wiki.creativecommons.org/Considerations_for_licensors
|
37 |
+
|
38 |
+
Considerations for the public: By using one of our public
|
39 |
+
licenses, a licensor grants the public permission to use the
|
40 |
+
licensed material under specified terms and conditions. If
|
41 |
+
the licensor's permission is not necessary for any reason--for
|
42 |
+
example, because of any applicable exception or limitation to
|
43 |
+
copyright--then that use is not regulated by the license. Our
|
44 |
+
licenses grant only permissions under copyright and certain
|
45 |
+
other rights that a licensor has authority to grant. Use of
|
46 |
+
the licensed material may still be restricted for other
|
47 |
+
reasons, including because others have copyright or other
|
48 |
+
rights in the material. A licensor may make special requests,
|
49 |
+
such as asking that all changes be marked or described.
|
50 |
+
Although not required by our licenses, you are encouraged to
|
51 |
+
respect those requests where reasonable. More_considerations
|
52 |
+
for the public:
|
53 |
+
wiki.creativecommons.org/Considerations_for_licensees
|
54 |
+
|
55 |
+
=======================================================================
|
56 |
+
|
57 |
+
Creative Commons Attribution 4.0 International Public License
|
58 |
+
|
59 |
+
By exercising the Licensed Rights (defined below), You accept and agree
|
60 |
+
to be bound by the terms and conditions of this Creative Commons
|
61 |
+
Attribution 4.0 International Public License ("Public License"). To the
|
62 |
+
extent this Public License may be interpreted as a contract, You are
|
63 |
+
granted the Licensed Rights in consideration of Your acceptance of
|
64 |
+
these terms and conditions, and the Licensor grants You such rights in
|
65 |
+
consideration of benefits the Licensor receives from making the
|
66 |
+
Licensed Material available under these terms and conditions.
|
67 |
+
|
68 |
+
|
69 |
+
Section 1 -- Definitions.
|
70 |
+
|
71 |
+
a. Adapted Material means material subject to Copyright and Similar
|
72 |
+
Rights that is derived from or based upon the Licensed Material
|
73 |
+
and in which the Licensed Material is translated, altered,
|
74 |
+
arranged, transformed, or otherwise modified in a manner requiring
|
75 |
+
permission under the Copyright and Similar Rights held by the
|
76 |
+
Licensor. For purposes of this Public License, where the Licensed
|
77 |
+
Material is a musical work, performance, or sound recording,
|
78 |
+
Adapted Material is always produced where the Licensed Material is
|
79 |
+
synched in timed relation with a moving image.
|
80 |
+
|
81 |
+
b. Adapter's License means the license You apply to Your Copyright
|
82 |
+
and Similar Rights in Your contributions to Adapted Material in
|
83 |
+
accordance with the terms and conditions of this Public License.
|
84 |
+
|
85 |
+
c. Copyright and Similar Rights means copyright and/or similar rights
|
86 |
+
closely related to copyright including, without limitation,
|
87 |
+
performance, broadcast, sound recording, and Sui Generis Database
|
88 |
+
Rights, without regard to how the rights are labeled or
|
89 |
+
categorized. For purposes of this Public License, the rights
|
90 |
+
specified in Section 2(b)(1)-(2) are not Copyright and Similar
|
91 |
+
Rights.
|
92 |
+
|
93 |
+
d. Effective Technological Measures means those measures that, in the
|
94 |
+
absence of proper authority, may not be circumvented under laws
|
95 |
+
fulfilling obligations under Article 11 of the WIPO Copyright
|
96 |
+
Treaty adopted on December 20, 1996, and/or similar international
|
97 |
+
agreements.
|
98 |
+
|
99 |
+
e. Exceptions and Limitations means fair use, fair dealing, and/or
|
100 |
+
any other exception or limitation to Copyright and Similar Rights
|
101 |
+
that applies to Your use of the Licensed Material.
|
102 |
+
|
103 |
+
f. Licensed Material means the artistic or literary work, database,
|
104 |
+
or other material to which the Licensor applied this Public
|
105 |
+
License.
|
106 |
+
|
107 |
+
g. Licensed Rights means the rights granted to You subject to the
|
108 |
+
terms and conditions of this Public License, which are limited to
|
109 |
+
all Copyright and Similar Rights that apply to Your use of the
|
110 |
+
Licensed Material and that the Licensor has authority to license.
|
111 |
+
|
112 |
+
h. Licensor means the individual(s) or entity(ies) granting rights
|
113 |
+
under this Public License.
|
114 |
+
|
115 |
+
i. Share means to provide material to the public by any means or
|
116 |
+
process that requires permission under the Licensed Rights, such
|
117 |
+
as reproduction, public display, public performance, distribution,
|
118 |
+
dissemination, communication, or importation, and to make material
|
119 |
+
available to the public including in ways that members of the
|
120 |
+
public may access the material from a place and at a time
|
121 |
+
individually chosen by them.
|
122 |
+
|
123 |
+
j. Sui Generis Database Rights means rights other than copyright
|
124 |
+
resulting from Directive 96/9/EC of the European Parliament and of
|
125 |
+
the Council of 11 March 1996 on the legal protection of databases,
|
126 |
+
as amended and/or succeeded, as well as other essentially
|
127 |
+
equivalent rights anywhere in the world.
|
128 |
+
|
129 |
+
k. You means the individual or entity exercising the Licensed Rights
|
130 |
+
under this Public License. Your has a corresponding meaning.
|
131 |
+
|
132 |
+
|
133 |
+
Section 2 -- Scope.
|
134 |
+
|
135 |
+
a. License grant.
|
136 |
+
|
137 |
+
1. Subject to the terms and conditions of this Public License,
|
138 |
+
the Licensor hereby grants You a worldwide, royalty-free,
|
139 |
+
non-sublicensable, non-exclusive, irrevocable license to
|
140 |
+
exercise the Licensed Rights in the Licensed Material to:
|
141 |
+
|
142 |
+
a. reproduce and Share the Licensed Material, in whole or
|
143 |
+
in part; and
|
144 |
+
|
145 |
+
b. produce, reproduce, and Share Adapted Material.
|
146 |
+
|
147 |
+
2. Exceptions and Limitations. For the avoidance of doubt, where
|
148 |
+
Exceptions and Limitations apply to Your use, this Public
|
149 |
+
License does not apply, and You do not need to comply with
|
150 |
+
its terms and conditions.
|
151 |
+
|
152 |
+
3. Term. The term of this Public License is specified in Section
|
153 |
+
6(a).
|
154 |
+
|
155 |
+
4. Media and formats; technical modifications allowed. The
|
156 |
+
Licensor authorizes You to exercise the Licensed Rights in
|
157 |
+
all media and formats whether now known or hereafter created,
|
158 |
+
and to make technical modifications necessary to do so. The
|
159 |
+
Licensor waives and/or agrees not to assert any right or
|
160 |
+
authority to forbid You from making technical modifications
|
161 |
+
necessary to exercise the Licensed Rights, including
|
162 |
+
technical modifications necessary to circumvent Effective
|
163 |
+
Technological Measures. For purposes of this Public License,
|
164 |
+
simply making modifications authorized by this Section 2(a)
|
165 |
+
(4) never produces Adapted Material.
|
166 |
+
|
167 |
+
5. Downstream recipients.
|
168 |
+
|
169 |
+
a. Offer from the Licensor -- Licensed Material. Every
|
170 |
+
recipient of the Licensed Material automatically
|
171 |
+
receives an offer from the Licensor to exercise the
|
172 |
+
Licensed Rights under the terms and conditions of this
|
173 |
+
Public License.
|
174 |
+
|
175 |
+
b. No downstream restrictions. You may not offer or impose
|
176 |
+
any additional or different terms or conditions on, or
|
177 |
+
apply any Effective Technological Measures to, the
|
178 |
+
Licensed Material if doing so restricts exercise of the
|
179 |
+
Licensed Rights by any recipient of the Licensed
|
180 |
+
Material.
|
181 |
+
|
182 |
+
6. No endorsement. Nothing in this Public License constitutes or
|
183 |
+
may be construed as permission to assert or imply that You
|
184 |
+
are, or that Your use of the Licensed Material is, connected
|
185 |
+
with, or sponsored, endorsed, or granted official status by,
|
186 |
+
the Licensor or others designated to receive attribution as
|
187 |
+
provided in Section 3(a)(1)(A)(i).
|
188 |
+
|
189 |
+
b. Other rights.
|
190 |
+
|
191 |
+
1. Moral rights, such as the right of integrity, are not
|
192 |
+
licensed under this Public License, nor are publicity,
|
193 |
+
privacy, and/or other similar personality rights; however, to
|
194 |
+
the extent possible, the Licensor waives and/or agrees not to
|
195 |
+
assert any such rights held by the Licensor to the limited
|
196 |
+
extent necessary to allow You to exercise the Licensed
|
197 |
+
Rights, but not otherwise.
|
198 |
+
|
199 |
+
2. Patent and trademark rights are not licensed under this
|
200 |
+
Public License.
|
201 |
+
|
202 |
+
3. To the extent possible, the Licensor waives any right to
|
203 |
+
collect royalties from You for the exercise of the Licensed
|
204 |
+
Rights, whether directly or through a collecting society
|
205 |
+
under any voluntary or waivable statutory or compulsory
|
206 |
+
licensing scheme. In all other cases the Licensor expressly
|
207 |
+
reserves any right to collect such royalties.
|
208 |
+
|
209 |
+
|
210 |
+
Section 3 -- License Conditions.
|
211 |
+
|
212 |
+
Your exercise of the Licensed Rights is expressly made subject to the
|
213 |
+
following conditions.
|
214 |
+
|
215 |
+
a. Attribution.
|
216 |
+
|
217 |
+
1. If You Share the Licensed Material (including in modified
|
218 |
+
form), You must:
|
219 |
+
|
220 |
+
a. retain the following if it is supplied by the Licensor
|
221 |
+
with the Licensed Material:
|
222 |
+
|
223 |
+
i. identification of the creator(s) of the Licensed
|
224 |
+
Material and any others designated to receive
|
225 |
+
attribution, in any reasonable manner requested by
|
226 |
+
the Licensor (including by pseudonym if
|
227 |
+
designated);
|
228 |
+
|
229 |
+
ii. a copyright notice;
|
230 |
+
|
231 |
+
iii. a notice that refers to this Public License;
|
232 |
+
|
233 |
+
iv. a notice that refers to the disclaimer of
|
234 |
+
warranties;
|
235 |
+
|
236 |
+
v. a URI or hyperlink to the Licensed Material to the
|
237 |
+
extent reasonably practicable;
|
238 |
+
|
239 |
+
b. indicate if You modified the Licensed Material and
|
240 |
+
retain an indication of any previous modifications; and
|
241 |
+
|
242 |
+
c. indicate the Licensed Material is licensed under this
|
243 |
+
Public License, and include the text of, or the URI or
|
244 |
+
hyperlink to, this Public License.
|
245 |
+
|
246 |
+
2. You may satisfy the conditions in Section 3(a)(1) in any
|
247 |
+
reasonable manner based on the medium, means, and context in
|
248 |
+
which You Share the Licensed Material. For example, it may be
|
249 |
+
reasonable to satisfy the conditions by providing a URI or
|
250 |
+
hyperlink to a resource that includes the required
|
251 |
+
information.
|
252 |
+
|
253 |
+
3. If requested by the Licensor, You must remove any of the
|
254 |
+
information required by Section 3(a)(1)(A) to the extent
|
255 |
+
reasonably practicable.
|
256 |
+
|
257 |
+
4. If You Share Adapted Material You produce, the Adapter's
|
258 |
+
License You apply must not prevent recipients of the Adapted
|
259 |
+
Material from complying with this Public License.
|
260 |
+
|
261 |
+
|
262 |
+
Section 4 -- Sui Generis Database Rights.
|
263 |
+
|
264 |
+
Where the Licensed Rights include Sui Generis Database Rights that
|
265 |
+
apply to Your use of the Licensed Material:
|
266 |
+
|
267 |
+
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
|
268 |
+
to extract, reuse, reproduce, and Share all or a substantial
|
269 |
+
portion of the contents of the database;
|
270 |
+
|
271 |
+
b. if You include all or a substantial portion of the database
|
272 |
+
contents in a database in which You have Sui Generis Database
|
273 |
+
Rights, then the database in which You have Sui Generis Database
|
274 |
+
Rights (but not its individual contents) is Adapted Material; and
|
275 |
+
|
276 |
+
c. You must comply with the conditions in Section 3(a) if You Share
|
277 |
+
all or a substantial portion of the contents of the database.
|
278 |
+
|
279 |
+
For the avoidance of doubt, this Section 4 supplements and does not
|
280 |
+
replace Your obligations under this Public License where the Licensed
|
281 |
+
Rights include other Copyright and Similar Rights.
|
282 |
+
|
283 |
+
|
284 |
+
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
|
285 |
+
|
286 |
+
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
|
287 |
+
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
|
288 |
+
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
|
289 |
+
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
|
290 |
+
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
|
291 |
+
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
292 |
+
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
|
293 |
+
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
|
294 |
+
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
|
295 |
+
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
|
296 |
+
|
297 |
+
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
|
298 |
+
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
|
299 |
+
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
|
300 |
+
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
|
301 |
+
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
|
302 |
+
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
|
303 |
+
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
|
304 |
+
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
|
305 |
+
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
|
306 |
+
|
307 |
+
c. The disclaimer of warranties and limitation of liability provided
|
308 |
+
above shall be interpreted in a manner that, to the extent
|
309 |
+
possible, most closely approximates an absolute disclaimer and
|
310 |
+
waiver of all liability.
|
311 |
+
|
312 |
+
|
313 |
+
Section 6 -- Term and Termination.
|
314 |
+
|
315 |
+
a. This Public License applies for the term of the Copyright and
|
316 |
+
Similar Rights licensed here. However, if You fail to comply with
|
317 |
+
this Public License, then Your rights under this Public License
|
318 |
+
terminate automatically.
|
319 |
+
|
320 |
+
b. Where Your right to use the Licensed Material has terminated under
|
321 |
+
Section 6(a), it reinstates:
|
322 |
+
|
323 |
+
1. automatically as of the date the violation is cured, provided
|
324 |
+
it is cured within 30 days of Your discovery of the
|
325 |
+
violation; or
|
326 |
+
|
327 |
+
2. upon express reinstatement by the Licensor.
|
328 |
+
|
329 |
+
For the avoidance of doubt, this Section 6(b) does not affect any
|
330 |
+
right the Licensor may have to seek remedies for Your violations
|
331 |
+
of this Public License.
|
332 |
+
|
333 |
+
c. For the avoidance of doubt, the Licensor may also offer the
|
334 |
+
Licensed Material under separate terms or conditions or stop
|
335 |
+
distributing the Licensed Material at any time; however, doing so
|
336 |
+
will not terminate this Public License.
|
337 |
+
|
338 |
+
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
|
339 |
+
License.
|
340 |
+
|
341 |
+
|
342 |
+
Section 7 -- Other Terms and Conditions.
|
343 |
+
|
344 |
+
a. The Licensor shall not be bound by any additional or different
|
345 |
+
terms or conditions communicated by You unless expressly agreed.
|
346 |
+
|
347 |
+
b. Any arrangements, understandings, or agreements regarding the
|
348 |
+
Licensed Material not stated herein are separate from and
|
349 |
+
independent of the terms and conditions of this Public License.
|
350 |
+
|
351 |
+
|
352 |
+
Section 8 -- Interpretation.
|
353 |
+
|
354 |
+
a. For the avoidance of doubt, this Public License does not, and
|
355 |
+
shall not be interpreted to, reduce, limit, restrict, or impose
|
356 |
+
conditions on any use of the Licensed Material that could lawfully
|
357 |
+
be made without permission under this Public License.
|
358 |
+
|
359 |
+
b. To the extent possible, if any provision of this Public License is
|
360 |
+
deemed unenforceable, it shall be automatically reformed to the
|
361 |
+
minimum extent necessary to make it enforceable. If the provision
|
362 |
+
cannot be reformed, it shall be severed from this Public License
|
363 |
+
without affecting the enforceability of the remaining terms and
|
364 |
+
conditions.
|
365 |
+
|
366 |
+
c. No term or condition of this Public License will be waived and no
|
367 |
+
failure to comply consented to unless expressly agreed to by the
|
368 |
+
Licensor.
|
369 |
+
|
370 |
+
d. Nothing in this Public License constitutes or may be interpreted
|
371 |
+
as a limitation upon, or waiver of, any privileges and immunities
|
372 |
+
that apply to the Licensor or You, including from the legal
|
373 |
+
processes of any jurisdiction or authority.
|
374 |
+
|
375 |
+
|
376 |
+
=======================================================================
|
377 |
+
|
378 |
+
Creative Commons is not a party to its public
|
379 |
+
licenses. Notwithstanding, Creative Commons may elect to apply one of
|
380 |
+
its public licenses to material it publishes and in those instances
|
381 |
+
will be considered the “Licensor.” The text of the Creative Commons
|
382 |
+
public licenses is dedicated to the public domain under the CC0 Public
|
383 |
+
Domain Dedication. Except for the limited purpose of indicating that
|
384 |
+
material is shared under a Creative Commons public license or as
|
385 |
+
otherwise permitted by the Creative Commons policies published at
|
386 |
+
creativecommons.org/policies, Creative Commons does not authorize the
|
387 |
+
use of the trademark "Creative Commons" or any other trademark or logo
|
388 |
+
of Creative Commons without its prior written consent including,
|
389 |
+
without limitation, in connection with any unauthorized modifications
|
390 |
+
to any of its public licenses or any other arrangements,
|
391 |
+
understandings, or agreements concerning use of licensed material. For
|
392 |
+
the avoidance of doubt, this paragraph does not form part of the
|
393 |
+
public licenses.
|
394 |
+
|
395 |
+
Creative Commons may be contacted at creativecommons.org.
|
101/replication_package/README.md
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Code accompanying the paper *Societies of strangers do not speak grammatically simpler languages* by Olena Shcherbakova, Susanne Maria Michaelis, Hannah J. Haynie, Sam Passmore, Volker Gast, Russell D. Gray, Simon J. Greenhill, Damián E. Blasi, and Hedvig Skirgård
|
2 |
+
|
3 |
+
# Overview of structure
|
4 |
+
This project contains all data and all scripts for data-wrangling, analysis and plotting.
|
5 |
+
|
6 |
+
## Data sources
|
7 |
+
|
8 |
+
The data that serves as the input for the analysis comes from Grambank,
|
9 |
+
(v1.0, Skirgård et al (in prep)), AUTOTYP (v1.01, Bickel et al (2022)), Glottolog (v4.5), EDGE-tree (v1.0.0, Bouckaert et al (2023)), Ethnologue (Eberhard et al 2020)), and WALS (Dryer & Haspelmath 2013).
|
10 |
+
|
11 |
+
With the exception of the Ethnologue data, all the data is available
|
12 |
+
openly via the science archive Zenodo and/or public GitHub repositories. A
|
13 |
+
modified version of the Ethnologue data is available in this repository, it contains
|
14 |
+
transformed population numbers that cannot be transformed back into the
|
15 |
+
raw numbers. The MCCT EDGE-tree is found in a file inside grambank-analysed.
|
16 |
+
|
17 |
+
Zenodo locations:
|
18 |
+
|
19 |
+
* Grambank (v.1.0) <https://doi.org/10.5281/zenodo.7740140>
|
20 |
+
* Grambank-analysed (v1.0) <https://doi.org/10.5281/zenodo.7740822>
|
21 |
+
* Glottolog-cldf (v4.5) <https://doi.org/10.5281/zenodo.5772649>
|
22 |
+
* AUTOTYP (v1.0.1) <https://doi.org/10.5281/zenodo.6255206>
|
23 |
+
|
24 |
+
GitHub locations:
|
25 |
+
|
26 |
+
* EDGE-tree (v1.0.0) <https://github.com/rbouckaert/global-language-tree-pipeline/tree/v1.0.0>
|
27 |
+
* Grambank (v1.0) <https://github.com/grambank/grambank/tree/v1.0>
|
28 |
+
* Grambank-analysed (v1.0) <https://github.com/grambank/grambank-analysed/tree/v1.0>
|
29 |
+
* Glottolog-cldf (v4.5) <https://github.com/glottolog/glottolog-cldf/tree/v4.5>
|
30 |
+
* AUTOTYP (v1.01) <https://github.com/autotyp/autotyp-data/tree/v1.0.1>
|
31 |
+
|
32 |
+
In this project, we fetch the data from the Zenodo locations by downloading a zip file and expanding it. We have also made tables and files available derived from these sources in this repos so that users may run the analysis without engaging with fetching from Zenodo. The scripts that generate these files are also found in this repository and can be run by users if they would like.
|
33 |
+
|
34 |
+
## Running data-wrangling, analysis and plotting scripts
|
35 |
+
All scripts are written in R. The necessary scripts can be called
|
36 |
+
one-by-one in order or executed by running the script `all_scripts.R`.
|
37 |
+
|
38 |
+
Running `all_scripts.R` involves the following:
|
39 |
+
|
40 |
+
- downloading, installing and loading necessary packages and create
|
41 |
+
folders for output (see `requirements.R` & `install_and_load_INLA.R`
|
42 |
+
for specific packages)
|
43 |
+
- generating a table of languoids from Glottolog v.4.5
|
44 |
+
- calculating metric scores from Grambank v.1.0: fusion metric and
|
45 |
+
informativity metric; both metrics designed by Hedvig Skirgård and
|
46 |
+
Hannah J. Haynie.
|
47 |
+
- generating population table (all sociodemographic variables in one
|
48 |
+
dataframe): data from Ethnologue e24 (Eberhard et al. 2020) and
|
49 |
+
Supplementary Materials in `data\lang_endangerment_predictors.xlsx`
|
50 |
+
from Bromham et al. (2022). Based on data availability, within
|
51 |
+
`set_up_inla.R`, it is necessary to specify whether `sample` is
|
52 |
+
`"full"` (full access to both Ethnologue variables in transformed
|
53 |
+
and non-transformed form and running all models; possible only for
|
54 |
+
users with their own access to Ethnologue) and `"reduced"` (access
|
55 |
+
to both Ethnologue variables - the number of L1 speakers and the
|
56 |
+
proportion of L2 speakers - in transformed form (logged and
|
57 |
+
standardized number of L1 speakers and the proportion of L2 speakers
|
58 |
+
than than raw numbers) and running all models except for one
|
59 |
+
including the interaction between the number of L1 speakers and L2
|
60 |
+
proportion; the dataset is already provided within the repository).
|
61 |
+
- wrangling global phylogeny - EDGE-tree (v1.0.0, Bouckaert et al 2023)
|
62 |
+
- generating AUTOTYP-areas table (v.1.0.1, Bickel et al. 2020)
|
63 |
+
- prepare everything for and run INLA analysis, including sensitivity
|
64 |
+
analyses
|
65 |
+
- measuring phylogenetic signal in fusion and informativity
|
66 |
+
- generating tables from INLA analyses, including sensitivity analyses
|
67 |
+
- make plots
|
68 |
+
- running additional analyses on WALS-based morphological complexity scores used in Lupyan & Dale's (2010) study (`data/complexity_data_WALS.csv`) (obtained from Gary Lupyan, personal communication 02.06.2023)
|
69 |
+
|
70 |
+
Please note: the necessary files, such as metrics scores obtained from the
|
71 |
+
Grambank dataset and parameters of metrics (these determine the
|
72 |
+
inclusion of Grambank into the metrics), are already made available. The
|
73 |
+
script that generates these `generating_GB_input_file.R` relies on the
|
74 |
+
folder `grambank_analysed` which incorporates data from
|
75 |
+
Grambank v.1.0, AUTOTYP (v1.0.1) and Glottolog v.4.5. To run this script, one needs to
|
76 |
+
first clone the repository and then run the R-script `get_external_data.R`.
|
77 |
+
|
78 |
+
# References
|
79 |
+
|
80 |
+
R. Bouckaert, D. Redding, O. Sheehan, T. Kyritsis, R. Gray, K. E. Jones, Q. Atkinson, Global language diversification is linked to socio-ecology and threat status (2022), , doi:10.31235/osf.io/f8tr6.
|
81 |
+
|
82 |
+
Bickel, Balthasar, Johanna Nichols, Taras Zakharko, Alena
|
83 |
+
Witzlack-Makarevich, Kristine Hildebrandt, Michael Rießler, Lennart
|
84 |
+
Bierkandt, Fernando Zúñiga & John B Lowe. 2022. The AUTOTYP database
|
85 |
+
(v1.1.0). <https://doi.org/10.5281/zenodo.6793367>.
|
86 |
+
|
87 |
+
Bromham, Lindell, Russell Dinnage, Hedvig Skirgård, Andrew Ritchie,
|
88 |
+
Marcel Cardillo, Felicity Meakins, Simon Greenhill & Xia Hua. 2022.
|
89 |
+
Global predictors of language endangerment and the future of linguistic
|
90 |
+
diversity. Nature ecology & evolution 6(2). 163--173.
|
91 |
+
|
92 |
+
Dryer, Matthew & Martin Haspelmath (eds.). 2013. The World Atlas of Language Structures Online. Leipzig: Max Planck Institute for Evolutionary Anthropology. http://wals.info.
|
93 |
+
|
94 |
+
Eberhard, David M., Gary F. Simons & Charles D. Fennig (eds.). 2020.
|
95 |
+
Ethnologue: Languages of the World. Dallas, Texas: SIL International.
|
96 |
+
www.ethnologue.com.
|
97 |
+
|
98 |
+
Hammarström, Harald & Forkel, Robert & Haspelmath, Martin & Bank, Sebastian. 2021. Glottolog 4.5. Leipzig: Max Planck Institute for Evolutionary Anthropology. (Available online at https://glottolog.org)
|
99 |
+
|
100 |
+
Skirgård, H., Haynie, H. J., Blasi, D. E., Hammarström, H., Collins, J., Latarche, J., Lesage, J., Weber, T., Witzlack-Makarevich, A., Passmore, S., Chira, A., Dinnage, R., Maurits, L., Dinnage, R., Dunn, M., Reesink, G., Singer, R., Bowern, C., Epps, P., Hill, J., Vesakoski, O., Robbeets, M., Abbas, K., Auer, D., Bakker, N., Barbos, G., Borges, R., Danielsen, S., Dorenbusch, L., Dorn, E., Elliott, J., Falcone, G., Fischer, J., Ghanggo Ate, Y., Gibson, H., Göbel, H., Goodall, J., Gruner, V., Harvey, A., Hayes, R., Heer, L., Herrera Miranda, R., Hübler, N., Huntington-Rainey, B., Ivani, J., Johns, M., Just, E., Kashima, E., Kipf, C., Klingenberg, J., König, N., Koti, K., Kowalik, R., Krasnoukhova, O., Lindvall, N., Lorenzen, M., Lutzenberger, H., Martins, T., Mata German, C., Meer, S., Montoya Samamé, J., Müller, M., Muradoglu, S., Neely, K., Nickel, J., Norvik, M., Oluoch, C. A., Peacock, J., Pearey , I., Peck, N., Petit, S., Pieper, S., Poblete, M., Prestipino, D., Raabe, L., Raja, A., Reimringer, J., Rey, S., Rizaew, J., Ruppert, E., Salmon, K., Sammet, J., Schembri, R., Schlabbach, L., Schmidt, F., Skilton, A., Smith, W. D., Sousa, H., Sverredal, K., Valle, D., Vera, J., Voß, J., Witte, T., Wu, H., Yam, S., Ye 葉婧婷, J., Yong, M., Yuditha, T., Zariquiey, R., Forkel, R., Evans, N., Levinson, S. C., Haspelmath, M., Greenhill, S. J., Atkinson, Q. D. and Gray, R. D. (in prep) "Grambank reveals the importance of genealogical constraints on linguistic diversity and highlights the impact of language loss". Science Advances
|
101 |
+
|
102 |
+
Hedvig Skirgård; Hannah J. Haynie; Harald Hammarström; Damián E. Blasi; Jeremy Collins; Jay Latarche; Jakob Lesage; Tobias Weber; Alena Witzlack-Makarevich; Michael Dunn; Ger Reesink; Ruth Singer; Claire Bowern; Patience Epps; Jane Hill; Outi Vesakoski; Noor Karolin Abbas; Sunny Ananth; Daniel Auer; Nancy A. Bakker; Giulia Barbos; Anina Bolls; Robert D. Borges; Mitchell Browen; Lennart Chevallier; Swintha Danielsen; Sinoël Dohlen; Luise Dorenbusch; Ella Dorn; Marie Duhamel; Farah El Haj Ali; John Elliott; Giada Falcone; Anna-Maria Fehn; Jana Fischer; Yustinus Ghanggo Ate; Hannah Gibson; Hans-Philipp Göbel; Jemima A. Goodall; Victoria Gruner; Andrew Harvey; Rebekah Hayes; Leonard Heer; Roberto E. Herrera Miranda; Nataliia Hübler; Biu H. Huntington-Rainey; Guglielmo Inglese; Jessica K. Ivani; Marilen Johns; Erika Just; Ivan Kapitonov; Eri Kashima; Carolina Kipf; Janina V. Klingenberg; Nikita König; Aikaterina Koti; Richard G. A. Kowalik; Olga Krasnoukhova; Kate Lynn Lindsey; Nora L. M. Lindvall; Mandy Lorenzen; Hannah Lutzenberger; Alexandra Marley; Tânia R. A. Martins; Celia Mata German; Suzanne van der Meer; Jaime Montoya; Michael Müller; Saliha Muradoglu; HunterGatherer; David Nash; Kelsey Neely; Johanna Nickel; Miina Norvik; Bruno Olsson; Cheryl Akinyi Oluoch; David Osgarby; Jesse Peacock; India O.C. Pearey; Naomi Peck; Jana Peter; Stephanie Petit; Sören Pieper; Mariana Poblete; Daniel Prestipino; Linda Raabe; Amna Raja; Janis Reimringer; Sydney C. Rey; Julia Rizaew; Eloisa Ruppert; Kim K. Salmon; Jill Sammet; Rhiannon Schembri; Lars Schlabbach; Frederick W. P. Schmidt; Dineke Schokkin; Jeff Siegel; Amalia Skilton; Hilário de Sousa; Kristin Sverredal; Daniel Valle; Javier Vera; Judith Voß; Daniel Wikalier Smith; Tim Witte; Henry Wu; Stephanie Yam; Jingting Ye 葉婧婷; Maisie Yong; Tessa Yuditha; Roberto Zariquiey; Robert Forkel; Nicholas Evans; Stephen C. Levinson; Martin Haspelmath; Simon J. Greenhill; Quentin D. Atkinson; Russell D. Gray (2023) Grambank v1.0 https://doi.org/10.5281/zenodo.7740140
|
103 |
+
|
104 |
+
The Grambank Consortium (eds.). 2022. Grambank 1.0. Leipzig: Max Planck
|
105 |
+
Institute for Evolutionary Anthropology. <http://grambank.clld.org>.
|
101/replication_package/WALS_reanalysis_controlled_setup.R
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("install_and_load_INLA.R")
|
2 |
+
|
3 |
+
#parameters
|
4 |
+
kappa = 1
|
5 |
+
phi_1 = c(1, 1.25) # "Local" version: (sigma, phi) First value is not used
|
6 |
+
|
7 |
+
WALS <- read_csv("data/complexity_data_WALS.csv") %>%
|
8 |
+
dplyr::select("Name" = lang, roundComp, logpop2, "ISO_639" = silCode) %>%
|
9 |
+
dplyr::mutate(ISO_639 = str_to_lower(ISO_639))
|
10 |
+
|
11 |
+
min_val <- min(WALS$roundComp)
|
12 |
+
max_val <- max(WALS$roundComp)
|
13 |
+
|
14 |
+
# Perform the rescaling
|
15 |
+
WALS$roundComp <- (WALS$roundComp - min_val) / (max_val - min_val)
|
16 |
+
|
17 |
+
pop_file_fn <-
|
18 |
+
"data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv"
|
19 |
+
L1 <-
|
20 |
+
read_tsv(pop_file_fn, show_col_types = F) %>% dplyr::select(ISO_639, L1_log10_scaled)
|
21 |
+
|
22 |
+
glottolog_df <-
|
23 |
+
read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", col_types = cols()) %>%
|
24 |
+
dplyr::select(
|
25 |
+
Glottocode,
|
26 |
+
Language_ID,
|
27 |
+
"ISO_639" = ISO639P3code,
|
28 |
+
Language_level_ID,
|
29 |
+
level,
|
30 |
+
Family_ID,
|
31 |
+
Longitude,
|
32 |
+
Latitude
|
33 |
+
) %>%
|
34 |
+
mutate(Language_level_ID = if_else(is.na(Language_level_ID), Glottocode, Language_level_ID)) %>%
|
35 |
+
mutate(Family_ID = ifelse(is.na(Family_ID), Language_level_ID, Family_ID)) %>%
|
36 |
+
dplyr::select(
|
37 |
+
Glottocode,
|
38 |
+
Language_ID,
|
39 |
+
ISO_639,
|
40 |
+
Language_level_ID,
|
41 |
+
level,
|
42 |
+
Family_ID,
|
43 |
+
Longitude,
|
44 |
+
Latitude
|
45 |
+
)
|
46 |
+
|
47 |
+
WALS_df <- WALS %>%
|
48 |
+
inner_join(L1,
|
49 |
+
by = c("ISO_639")) %>%
|
50 |
+
inner_join(glottolog_df, by = "ISO_639") %>%
|
51 |
+
filter(!is.na(Latitude), !is.na(Longitude)) %>%
|
52 |
+
dplyr::select(Language_ID = Glottocode,
|
53 |
+
Name,
|
54 |
+
roundComp,
|
55 |
+
ISO_639,
|
56 |
+
L1_log10_scaled,
|
57 |
+
Longitude,
|
58 |
+
Latitude)
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
tree <- read.tree(file.path("data_wrangling/wrangled.tree"))
|
63 |
+
|
64 |
+
#dropping tips not in Grambank
|
65 |
+
WALS_df <- WALS_df[WALS_df$Language_ID %in% tree$tip.label,]
|
66 |
+
tree <- keep.tip(tree, WALS_df$Language_ID)
|
67 |
+
|
68 |
+
x <-
|
69 |
+
assert_that(all(tree$tip.label %in% WALS_df$Language_ID), msg = "The data and phylogeny taxa do not match")
|
70 |
+
|
71 |
+
## Building standardized phylogenetic precision matrix
|
72 |
+
tree_scaled <- tree
|
73 |
+
|
74 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
75 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
76 |
+
|
77 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
78 |
+
tree_scaled$edge.length <-
|
79 |
+
tree_scaled$edge.length / typical_phylogenetic_variance
|
80 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
81 |
+
nodes = "ALL",
|
82 |
+
scale = FALSE)$Ainv
|
83 |
+
|
84 |
+
WALS_df = WALS_df[order(match(WALS_df$Language_ID, rownames(phylo_prec_mat))), ]
|
85 |
+
|
86 |
+
#"local" set of parameters
|
87 |
+
## Create spatial covariance matrix using the matern covariance function
|
88 |
+
spatial_covar_mat_1 = varcov.spatial(WALS_df[, c("Longitude", "Latitude")],
|
89 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
90 |
+
# Calculate and standardize by the typical variance
|
91 |
+
typical_variance_spatial_1 = exp(mean(log(diag(
|
92 |
+
spatial_covar_mat_1
|
93 |
+
))))
|
94 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
95 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
96 |
+
dimnames(spatial_prec_mat_1) = list(WALS_df$Language_ID, WALS_df$Language_ID)
|
97 |
+
|
98 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
99 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
100 |
+
WALS_df$phy_id = phy_id
|
101 |
+
|
102 |
+
## Other effects are in the same order they appear in the dataset
|
103 |
+
WALS_df$sp_id = 1:nrow(spatial_prec_mat_1)
|
104 |
+
|
105 |
+
|
106 |
+
formula <- as.formula(
|
107 |
+
paste(
|
108 |
+
"roundComp ~",
|
109 |
+
"L1_log10_scaled +",
|
110 |
+
"f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat,
|
111 |
+
constr = TRUE, hyper = pcprior_hyper) +
|
112 |
+
f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1,
|
113 |
+
constr = TRUE, hyper = pcprior_hyper)"
|
114 |
+
)
|
115 |
+
)
|
116 |
+
|
117 |
+
result <- inla(
|
118 |
+
formula,
|
119 |
+
family = "gaussian",
|
120 |
+
data = WALS_df,
|
121 |
+
control.compute = list(waic = TRUE)
|
122 |
+
)
|
123 |
+
summary(result)
|
124 |
+
|
125 |
+
save(result, file = "output_models/model_WALS_controlled.RData")
|
126 |
+
|
127 |
+
social_effects_controlled <-
|
128 |
+
c("morphological complexity ~ L1 + phylogenetic effect + spatial effect",
|
129 |
+
round(
|
130 |
+
c(
|
131 |
+
result$summary.fixed[2,]$`0.025quant`,
|
132 |
+
result$summary.fixed[2,]$`0.5quant`,
|
133 |
+
result$summary.fixed[2,]$`0.975quant`,
|
134 |
+
nrow(WALS_df)
|
135 |
+
),
|
136 |
+
2
|
137 |
+
), "default (~10%)")
|
138 |
+
|
139 |
+
save(social_effects_controlled, file = "output_models/social_effects_controlled.RData")
|
140 |
+
|
101/replication_package/WALS_reanalysis_controlled_setup_high_coverage.R
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("install_and_load_INLA.R")
|
2 |
+
|
3 |
+
#parameters
|
4 |
+
kappa = 1
|
5 |
+
phi_1 = c(1, 1.25) # "Local" version: (sigma, phi) First value is not used
|
6 |
+
|
7 |
+
WALS <- read_csv("data/complexity_data_WALS.csv") %>%
|
8 |
+
dplyr::select("Name" = lang, roundComp, logpop2, "ISO_639" = silCode) %>%
|
9 |
+
dplyr::mutate(ISO_639 = str_to_lower(ISO_639)) %>%
|
10 |
+
inner_join(read_csv("output_tables/WALS_high_coverage.csv"),
|
11 |
+
by = c("ISO_639"))
|
12 |
+
|
13 |
+
min_val <- min(WALS$roundComp)
|
14 |
+
max_val <- max(WALS$roundComp)
|
15 |
+
|
16 |
+
# Perform the rescaling
|
17 |
+
WALS$roundComp <- (WALS$roundComp - min_val) / (max_val - min_val)
|
18 |
+
|
19 |
+
pop_file_fn <-
|
20 |
+
"data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv"
|
21 |
+
L1 <-
|
22 |
+
read_tsv(pop_file_fn, show_col_types = F) %>% dplyr::select(ISO_639, L1_log10_scaled)
|
23 |
+
|
24 |
+
glottolog_df <-
|
25 |
+
read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", col_types = cols()) %>%
|
26 |
+
dplyr::select(
|
27 |
+
Glottocode,
|
28 |
+
Name,
|
29 |
+
Language_ID,
|
30 |
+
"ISO_639" = ISO639P3code,
|
31 |
+
Language_level_ID,
|
32 |
+
level,
|
33 |
+
Family_ID,
|
34 |
+
Longitude,
|
35 |
+
Latitude
|
36 |
+
) %>%
|
37 |
+
mutate(Language_level_ID = if_else(is.na(Language_level_ID), Glottocode, Language_level_ID)) %>%
|
38 |
+
mutate(Family_ID = ifelse(is.na(Family_ID), Language_level_ID, Family_ID)) %>%
|
39 |
+
dplyr::select(
|
40 |
+
Glottocode,
|
41 |
+
Name,
|
42 |
+
Language_ID,
|
43 |
+
ISO_639,
|
44 |
+
Language_level_ID,
|
45 |
+
level,
|
46 |
+
Family_ID,
|
47 |
+
Longitude,
|
48 |
+
Latitude
|
49 |
+
)
|
50 |
+
|
51 |
+
WALS_df <- WALS %>%
|
52 |
+
inner_join(L1,
|
53 |
+
by = c("ISO_639")) %>%
|
54 |
+
inner_join(glottolog_df, by = "ISO_639") %>%
|
55 |
+
filter(!is.na(Latitude),!is.na(Longitude)) %>%
|
56 |
+
dplyr::select(Language_ID = Glottocode,
|
57 |
+
Name,
|
58 |
+
roundComp,
|
59 |
+
ISO_639,
|
60 |
+
L1_log10_scaled,
|
61 |
+
Longitude,
|
62 |
+
Latitude)
|
63 |
+
|
64 |
+
# jitter points locations
|
65 |
+
WALS_df$Latitude <- jitter(WALS_df$Latitude, amount = 0.001)
|
66 |
+
WALS_df$Longitude <- jitter(WALS_df$Longitude, amount = 0.001)
|
67 |
+
|
68 |
+
tree <- read.tree(file.path("data_wrangling/wrangled.tree"))
|
69 |
+
|
70 |
+
#dropping tips not in Grambank
|
71 |
+
WALS_df <- WALS_df[WALS_df$Language_ID %in% tree$tip.label, ]
|
72 |
+
WALS_df <- WALS_df[!duplicated(WALS_df$Language_ID),]
|
73 |
+
tree <- keep.tip(tree, WALS_df$Language_ID)
|
74 |
+
|
75 |
+
x <-
|
76 |
+
assert_that(all(tree$tip.label %in% WALS_df$Language_ID), msg = "The data and phylogeny taxa do not match")
|
77 |
+
|
78 |
+
## Building standardized phylogenetic precision matrix
|
79 |
+
tree_scaled <- tree
|
80 |
+
|
81 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
82 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
83 |
+
|
84 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
85 |
+
tree_scaled$edge.length <-
|
86 |
+
tree_scaled$edge.length / typical_phylogenetic_variance
|
87 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
88 |
+
nodes = "ALL",
|
89 |
+
scale = FALSE)$Ainv
|
90 |
+
|
91 |
+
WALS_df = WALS_df[order(match(WALS_df$Language_ID, rownames(phylo_prec_mat))),]
|
92 |
+
|
93 |
+
#"local" set of parameters
|
94 |
+
## Create spatial covariance matrix using the matern covariance function
|
95 |
+
spatial_covar_mat_1 = varcov.spatial(WALS_df[, c("Longitude", "Latitude")],
|
96 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
97 |
+
# Calculate and standardize by the typical variance
|
98 |
+
typical_variance_spatial_1 = exp(mean(log(diag(
|
99 |
+
spatial_covar_mat_1
|
100 |
+
))))
|
101 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
102 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
103 |
+
dimnames(spatial_prec_mat_1) = list(WALS_df$Language_ID, WALS_df$Language_ID)
|
104 |
+
|
105 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
106 |
+
phy_id <- match(tree$tip.label, rownames(phylo_prec_mat))
|
107 |
+
if (length(phy_id) != nrow(WALS_df)) {
|
108 |
+
stop("The number of phylogenetic IDs does not match the number of rows in WALS_df.")
|
109 |
+
}
|
110 |
+
|
111 |
+
WALS_df$phy_id <- phy_id
|
112 |
+
|
113 |
+
## Other effects are in the same order they appear in the dataset
|
114 |
+
WALS_df$sp_id = 1:nrow(spatial_prec_mat_1)
|
115 |
+
|
116 |
+
|
117 |
+
formula <- as.formula(
|
118 |
+
paste(
|
119 |
+
"roundComp ~",
|
120 |
+
"L1_log10_scaled +",
|
121 |
+
"f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat,
|
122 |
+
constr = TRUE, hyper = pcprior_hyper) +
|
123 |
+
f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1,
|
124 |
+
constr = TRUE, hyper = pcprior_hyper)"
|
125 |
+
)
|
126 |
+
)
|
127 |
+
|
128 |
+
result <- inla(
|
129 |
+
formula,
|
130 |
+
family = "gaussian",
|
131 |
+
control.family = list(hyper = pcprior_hyper),
|
132 |
+
data = WALS_df,
|
133 |
+
control.compute = list(waic = TRUE)
|
134 |
+
)
|
135 |
+
summary(result)
|
136 |
+
|
137 |
+
save(result, file = "output_models/model_WALS_high_coverage.RData")
|
138 |
+
|
139 |
+
#mean estimate of L1_Users: with credible intervals not crossing zero ()
|
140 |
+
|
141 |
+
social_effects_controlled_coverage <-
|
142 |
+
c(
|
143 |
+
"morphological complexity ~ L1 + phylogenetic effect + spatial effect",
|
144 |
+
round(
|
145 |
+
c(
|
146 |
+
result$summary.fixed[2, ]$`0.025quant`,
|
147 |
+
result$summary.fixed[2, ]$`0.5quant`,
|
148 |
+
result$summary.fixed[2, ]$`0.975quant`,
|
149 |
+
nrow(WALS_df)
|
150 |
+
),
|
151 |
+
2
|
152 |
+
),
|
153 |
+
"35%"
|
154 |
+
)
|
155 |
+
|
156 |
+
save(social_effects_controlled_coverage, file = "output_models/social_effects_controlled.RData")
|
157 |
+
|
158 |
+
load("output_models/social_effects_uncontrolled.RData")
|
159 |
+
load("output_models/social_effects_controlled.RData")
|
160 |
+
load("output_models/social_effects_controlled_coverage.RData")
|
161 |
+
|
162 |
+
effects_morph_comp <-
|
163 |
+
as.data.frame(
|
164 |
+
rbind(
|
165 |
+
social_effects_uncontrolled,
|
166 |
+
social_effects_controlled,
|
167 |
+
social_effects_controlled_coverage
|
168 |
+
)
|
169 |
+
)
|
170 |
+
colnames(effects_morph_comp) <-
|
171 |
+
c("model",
|
172 |
+
"2.5%",
|
173 |
+
"50%",
|
174 |
+
"97.5%",
|
175 |
+
"sample size",
|
176 |
+
"feature coverage threshold")
|
177 |
+
|
178 |
+
rownames(effects_morph_comp) <- NULL
|
179 |
+
|
180 |
+
effects_morph_comp %>%
|
181 |
+
write_csv("output_tables/WALS_morph_compl_effects.csv")
|
101/replication_package/WALS_reanalysis_setup.R
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("install_and_load_INLA.R")
|
2 |
+
|
3 |
+
#parameters
|
4 |
+
kappa = 1
|
5 |
+
phi_1 = c(1, 1.25) # "Local" version: (sigma, phi) First value is not used
|
6 |
+
|
7 |
+
WALS <- read_csv("data/complexity_data_WALS.csv") %>%
|
8 |
+
dplyr::select("Name"=lang, roundComp, logpop2, "ISO_639"=silCode) %>%
|
9 |
+
dplyr::mutate(ISO_639 = str_to_lower(ISO_639))
|
10 |
+
|
11 |
+
min_val <- min(WALS$roundComp)
|
12 |
+
max_val <- max(WALS$roundComp)
|
13 |
+
|
14 |
+
# Perform the rescaling
|
15 |
+
WALS$roundComp <- (WALS$roundComp - min_val) / (max_val - min_val)
|
16 |
+
|
17 |
+
pop_file_fn <- "data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv"
|
18 |
+
L1 <-
|
19 |
+
read_tsv(pop_file_fn, show_col_types = F) %>% dplyr::select(ISO_639, L1_log10_scaled)
|
20 |
+
|
21 |
+
WALS_df <- WALS %>%
|
22 |
+
inner_join(L1,
|
23 |
+
by = c("ISO_639"))
|
24 |
+
|
25 |
+
formula <- as.formula(paste("roundComp ~", "L1_log10_scaled"))
|
26 |
+
result <- inla(formula, family = "gaussian",
|
27 |
+
data = WALS_df, control.compute = list(waic = TRUE))
|
28 |
+
summary(result)
|
29 |
+
|
30 |
+
save(result, file = "output_models/models_WALS_uncontrolled.RData")
|
31 |
+
|
32 |
+
social_effects_uncontrolled <- c("morphological complexity ~ L1",
|
33 |
+
round(c(
|
34 |
+
result$summary.fixed[2,]$`0.025quant`,
|
35 |
+
result$summary.fixed[2,]$`0.5quant`,
|
36 |
+
result$summary.fixed[2,]$`0.975quant`, nrow(WALS_df)), 2), "default (~10%)")
|
37 |
+
|
38 |
+
save(social_effects_uncontrolled, file = "output_models/social_effects_uncontrolled.RData")
|
39 |
+
|
40 |
+
|
101/replication_package/WALS_sparseness.R
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("requirements.R")
|
2 |
+
|
3 |
+
library(INLA)
|
4 |
+
inla.setOption(inla.mode = "experimental")
|
5 |
+
|
6 |
+
wals <- read.delim(
|
7 |
+
"https://raw.githubusercontent.com/cldf-datasets/wals/master/cldf/languages.csv",
|
8 |
+
sep = ","
|
9 |
+
) %>%
|
10 |
+
dplyr::select(ID, ISO_639 = ISO_codes, Name, Glottocode) %>%
|
11 |
+
rename(Language_ID = ID) %>% #renaming the column to avoid problems
|
12 |
+
left_join(
|
13 |
+
read.delim(
|
14 |
+
"https://raw.githubusercontent.com/cldf-datasets/wals/master/cldf/values.csv",
|
15 |
+
sep = ","
|
16 |
+
) %>% dplyr::select(Language_ID, Parameter_ID, Value)
|
17 |
+
) %>%
|
18 |
+
dplyr::select(-Language_ID) %>%
|
19 |
+
rename(Language_ID = Glottocode)
|
20 |
+
|
21 |
+
wals_selected <- wals %>%
|
22 |
+
filter(
|
23 |
+
Parameter_ID == "20A" |
|
24 |
+
Parameter_ID == "26A" |
|
25 |
+
Parameter_ID == "49A" |
|
26 |
+
Parameter_ID == "28A" |
|
27 |
+
Parameter_ID == "98A" |
|
28 |
+
Parameter_ID == "22A" |
|
29 |
+
Parameter_ID == "100A" |
|
30 |
+
Parameter_ID == "102A" |
|
31 |
+
Parameter_ID == "48A" |
|
32 |
+
Parameter_ID == "29A" |
|
33 |
+
Parameter_ID == "74A" |
|
34 |
+
Parameter_ID == "75A" |
|
35 |
+
Parameter_ID == "76A" |
|
36 |
+
Parameter_ID == "77A" |
|
37 |
+
Parameter_ID == "112A" |
|
38 |
+
Parameter_ID == "34A" |
|
39 |
+
Parameter_ID == "36A" |
|
40 |
+
Parameter_ID == "92A" |
|
41 |
+
Parameter_ID == "66A" |
|
42 |
+
Parameter_ID == "67A" |
|
43 |
+
Parameter_ID == "65A" |
|
44 |
+
Parameter_ID == "70A" |
|
45 |
+
Parameter_ID == "57A" |
|
46 |
+
Parameter_ID == "59A" |
|
47 |
+
Parameter_ID == "73A" |
|
48 |
+
Parameter_ID == "38A" |
|
49 |
+
Parameter_ID == "39A" |
|
50 |
+
Parameter_ID == "41A" |
|
51 |
+
Parameter_ID == "101A"
|
52 |
+
) %>%
|
53 |
+
pivot_wider(
|
54 |
+
names_from = Parameter_ID,
|
55 |
+
values_from = Value
|
56 |
+
)
|
57 |
+
|
58 |
+
# Specify the range of columns
|
59 |
+
start_column <- "92A"
|
60 |
+
end_column <- "76A"
|
61 |
+
|
62 |
+
# Filter and gather the selected columns
|
63 |
+
wals_selected_na <- wals_selected %>%
|
64 |
+
rowwise() %>%
|
65 |
+
mutate(na_proportion = mean(is.na(c_across(starts_with(start_column):starts_with(end_column))))) %>%
|
66 |
+
filter(na_proportion <= 0.35)
|
67 |
+
|
68 |
+
wals_selected_na %>%
|
69 |
+
write_csv("output_tables/WALS_high_coverage.csv")
|
70 |
+
|
101/replication_package/all_scripts.R
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Running all scripts
|
2 |
+
|
3 |
+
#download packages and create folders
|
4 |
+
#generate Glottolog table (based on Glottolog 4.4)
|
5 |
+
#calculate metric scores (based on Grambank 1.0)
|
6 |
+
#generate population table (all sociodemographic variables in one dataframe)
|
7 |
+
#wrangling EDGE tree
|
8 |
+
#generating AUTOTYP areas table
|
9 |
+
source("get_external_data.R")
|
10 |
+
source("generating_GB_input_file.R")
|
11 |
+
source("set_up_general.R")
|
12 |
+
|
13 |
+
#setup for INLA analysis
|
14 |
+
source("install_and_load_INLA.R")
|
15 |
+
#choosing whether to use the full dataset (possible only for reviewers and if one has own access to Ethnologue and saved the dataset in the data folder on their own) or to only to the subset of Ethnologue with transformed variables made available in this repostiory after running create_pop_table.R
|
16 |
+
|
17 |
+
#sample <- "full"
|
18 |
+
sample <- "reduced" #default
|
19 |
+
|
20 |
+
source("make_ethnologue_SM_and_merging_tables.R")
|
21 |
+
source("create_pop_table.R")
|
22 |
+
source("set_up_inla.R")
|
23 |
+
|
24 |
+
#run all INLA models + extract main results tables
|
25 |
+
#Note that previously "fusion" was called "boundness", and this is how it is referenced in all scripts
|
26 |
+
|
27 |
+
#predictors: random effects - phylogenetic and spatial (same scripts for "full" and "reduced" versions)
|
28 |
+
source("models_Boundness_phylogenetic_spatial.R")
|
29 |
+
source("models_Informativity_phylogenetic_spatial.R")
|
30 |
+
|
31 |
+
if(sample == "full"){
|
32 |
+
|
33 |
+
#predictors: phylogenetic and spatial random effects + sociodemograhic variables as fixed effects
|
34 |
+
source("models_Boundness_social.R")
|
35 |
+
source("models_Informativity_social.R")
|
36 |
+
|
37 |
+
#predictors: sociodemographic variables as fixed effects
|
38 |
+
source("models_Boundness_social_only.R")
|
39 |
+
source("models_Informativity_social_only.R")
|
40 |
+
|
41 |
+
#conduct sensitivity testing + extract the corresponding table
|
42 |
+
source("runs_sensitivity.R")
|
43 |
+
|
44 |
+
#extract tables from INLA analyses
|
45 |
+
source("table_INLA_summary_all_models_SI.R")
|
46 |
+
source("variance_top_ranking_models.R")
|
47 |
+
|
48 |
+
#plotting main results
|
49 |
+
source("plot_social_effects_combined.R")
|
50 |
+
}
|
51 |
+
|
52 |
+
if(sample == "reduced"){
|
53 |
+
|
54 |
+
#predictors: phylogenetic and spatial random effects + sociodemograhic variables as fixed effects
|
55 |
+
#(on reduced set of social variables: without log10 transformed L1 speakers)
|
56 |
+
source("models_Boundness_reduced_social.R")
|
57 |
+
source("models_Informativity_reduced_social.R")
|
58 |
+
|
59 |
+
#predictors: sociodemographic variables as fixed effects
|
60 |
+
source("models_Boundness_reduced_social_only.R")
|
61 |
+
source("models_Informativity_reduced_social_only.R")
|
62 |
+
|
63 |
+
#conduct sensitivity testing + extract the corresponding table
|
64 |
+
source("runs_sensitivity_on_reduced.R")
|
65 |
+
|
66 |
+
#extract tables from INLA analyses
|
67 |
+
source("table_INLA_summary_all_models_SI_reduced.R")
|
68 |
+
source("variance_top_ranking_models_reduced.R")
|
69 |
+
|
70 |
+
#plotting main results
|
71 |
+
source("plots_social_effects_combined_on_reduced.R")
|
72 |
+
}
|
73 |
+
|
74 |
+
#measure phylogenetic signal in two fusion and informativity
|
75 |
+
source("measuring_phylosignal.R")
|
76 |
+
|
77 |
+
|
78 |
+
#plotting
|
79 |
+
source("plot_maps_main.R") #maps of scores
|
80 |
+
source("plot_heatmap_B_I.R") #phylogenetic tree with a heatmap
|
81 |
+
source("plot_map_Africa.R")
|
82 |
+
source("plot_map_Eurasia.R")
|
83 |
+
source("plot_heatmap_informativity_Uralic.R") #Uralic tree (informativity) + combined plot with two maps from above
|
84 |
+
source("plot_spatial_parameters_linear_distances.R") #SI figure for visualizing how covariance under different kappa and phi parameters corresponds to spatial distances
|
85 |
+
|
86 |
+
#additional analyses on WALS data
|
87 |
+
source("make_ethnologue_SM_for_morphological_complexity_reanalysis.R")
|
88 |
+
source("WALS_sparseness.R")
|
89 |
+
source("WALS_reanalysis_setup.R")
|
90 |
+
source("WALS_reanalysis_controlled_setup.R")
|
91 |
+
source("WALS_reanalysis_controlled_setup_high_coverage.R") #analysis + summary table
|
101/replication_package/assigning_AUTOTYP_areas.R
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#This script assigns all languages in glottolog_df to their nearest AUTOTYP area
|
2 |
+
|
3 |
+
#Script was written by Hedvig Skirgård
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
OUTPUTDIR_data_wrangling <- here("data_wrangling")
|
8 |
+
# create output dir if it does not exist.
|
9 |
+
if (!dir.exists(OUTPUTDIR_data_wrangling)) {
|
10 |
+
dir.create(OUTPUTDIR_data_wrangling)
|
11 |
+
}
|
12 |
+
|
13 |
+
if (!file.exists(here(OUTPUTDIR_data_wrangling, "glottolog_AUTOTYPE_areas.tsv"))) {
|
14 |
+
#GB langs for subsettting
|
15 |
+
GB_langs <-
|
16 |
+
read_tsv("data/GB_wide/GB_wide_strict.tsv", col_types = WIDE_COLSPEC) %>%
|
17 |
+
dplyr::select(Language_ID)
|
18 |
+
|
19 |
+
#combining the tables languages and values from glottolog_df-cldf into one wide dataframe.
|
20 |
+
#this can be replaced with any list of Language_IDs, long and lat
|
21 |
+
|
22 |
+
glottolog_fn <- "data_wrangling/glottolog_cldf_wide_df.tsv"
|
23 |
+
if (!file.exists(glottolog_fn)) {
|
24 |
+
source("generating_GB_input_file.R")
|
25 |
+
}
|
26 |
+
|
27 |
+
glottolog_df <- read.delim(glottolog_fn , sep = "\t") %>%
|
28 |
+
dplyr::select(Language_ID, Longitude, Latitude) %>%
|
29 |
+
inner_join(GB_langs, by = "Language_ID")
|
30 |
+
|
31 |
+
##Adding in areas of linguistic contact from AUTOTYP
|
32 |
+
|
33 |
+
AUTOTYP <-
|
34 |
+
read.delim(
|
35 |
+
"https://raw.githubusercontent.com/autotyp/autotyp-data/master/data/csv/Register.csv",
|
36 |
+
sep = ","
|
37 |
+
) %>%
|
38 |
+
dplyr::select(Language_ID = Glottocode, Area, Longitude, Latitude) %>%
|
39 |
+
group_by(Language_ID, Area) %>% #some lgs are assigned to more than one area, we level that out.
|
40 |
+
sample_n(1)
|
41 |
+
|
42 |
+
#This next bit where we find the autotyp areas of languages was written by Seán Roberts
|
43 |
+
# We know the autotyp-area of langauges in autotyp and their long lat. We don't know the autotyp area of languages in Glottolog. We also can't be sure that the long lat of languoids with the same glottoids in autotyp and glottolog_df have the exact identical long lat. First let's make two datasets, one for autotyp languages (hence lgs where we know the area) and those that we wish to know about, the Glottolog ones.
|
44 |
+
|
45 |
+
lgs_with_known_area <-
|
46 |
+
as.matrix(AUTOTYP[!is.na(AUTOTYP$Area), c("Longitude", "Latitude")])
|
47 |
+
rownames(lgs_with_known_area) <-
|
48 |
+
AUTOTYP[!is.na(AUTOTYP$Area), ]$Language_ID
|
49 |
+
|
50 |
+
known_areas <- AUTOTYP %>%
|
51 |
+
dplyr::filter(!is.na(Area)) %>%
|
52 |
+
dplyr::select(Language_ID, Area) %>%
|
53 |
+
distinct() %>%
|
54 |
+
dplyr::select(AUTOTYP_Language_ID = Language_ID, everything())
|
55 |
+
|
56 |
+
rm(AUTOTYP)
|
57 |
+
|
58 |
+
lgs_with_unknown_area <-
|
59 |
+
as.matrix(glottolog_df[, c("Longitude", "Latitude")])
|
60 |
+
rownames(lgs_with_unknown_area) <- glottolog_df$Language_ID
|
61 |
+
|
62 |
+
# For missing, find area of closest langauge
|
63 |
+
atDist <-
|
64 |
+
rdist.earth(lgs_with_known_area, lgs_with_unknown_area, miles = F)
|
65 |
+
|
66 |
+
rm(lgs_with_known_area, lgs_with_unknown_area)
|
67 |
+
|
68 |
+
df_matched_up <-
|
69 |
+
as.data.frame(unlist(apply(atDist, 2, function(x) {
|
70 |
+
names(which.min(x))
|
71 |
+
})), stringsAsFactors = F) %>%
|
72 |
+
rename(AUTOTYP_Language_ID = `unlist(apply(atDist, 2, function(x) { names(which.min(x)) }))`)
|
73 |
+
|
74 |
+
glottolog_df_with_AUTOTYP <- df_matched_up %>%
|
75 |
+
tibble::rownames_to_column("Language_ID") %>%
|
76 |
+
full_join(known_areas, by = "AUTOTYP_Language_ID") %>%
|
77 |
+
right_join(glottolog_df, by = "Language_ID") %>%
|
78 |
+
dplyr::select(-AUTOTYP_Language_ID) %>%
|
79 |
+
group_by(Language_ID) %>% #some lgs are assigned to more than one area, we level that out.
|
80 |
+
sample_n(1) %>%
|
81 |
+
rename(AUTOTYP_area = Area)
|
82 |
+
|
83 |
+
glottolog_df_with_AUTOTYP %>%
|
84 |
+
write_tsv(here(OUTPUTDIR_data_wrangling, "glottolog_AUTOTYPE_areas.tsv"))
|
85 |
+
}
|
101/replication_package/create_pop_table.R
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#create_pop_table
|
2 |
+
|
3 |
+
OUTPUTDIR_data_wrangling <- here("data_wrangling")
|
4 |
+
# create output dir if it does not exist.
|
5 |
+
if (!dir.exists(OUTPUTDIR_data_wrangling)) {
|
6 |
+
dir.create(OUTPUTDIR_data_wrangling)
|
7 |
+
}
|
8 |
+
|
9 |
+
|
10 |
+
#Glottolog df for ISO_639 merging
|
11 |
+
glottolog_df <-
|
12 |
+
read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", col_types = cols()) %>%
|
13 |
+
dplyr::select(
|
14 |
+
Glottocode,
|
15 |
+
Language_ID,
|
16 |
+
"ISO_639" = ISO639P3code,
|
17 |
+
Language_level_ID,
|
18 |
+
level,
|
19 |
+
Family_ID,
|
20 |
+
Longitude,
|
21 |
+
Latitude
|
22 |
+
) %>%
|
23 |
+
mutate(Language_level_ID = if_else(is.na(Language_level_ID), Glottocode, Language_level_ID)) %>%
|
24 |
+
mutate(Family_ID = ifelse(is.na(Family_ID), Language_level_ID, Family_ID)) %>%
|
25 |
+
dplyr::select(
|
26 |
+
Glottocode,
|
27 |
+
Language_ID,
|
28 |
+
ISO_639,
|
29 |
+
Language_level_ID,
|
30 |
+
level,
|
31 |
+
Family_ID,
|
32 |
+
Longitude,
|
33 |
+
Latitude
|
34 |
+
)
|
35 |
+
|
36 |
+
|
37 |
+
if (sample == "full") {
|
38 |
+
data_ethnologue <-
|
39 |
+
read_tsv("data_wrangling/ethnologue_pop_full.tsv")
|
40 |
+
}
|
41 |
+
|
42 |
+
if (sample == "reduced") {
|
43 |
+
#double check if the file below needs to be changed
|
44 |
+
data_ethnologue <-
|
45 |
+
read_tsv("data_wrangling/ethnologue_pop_SM.tsv", show_col_types = F) %>%
|
46 |
+
rename(L1_log10_st = L1_log10_scaled) %>%
|
47 |
+
dplyr::select(ISO_639, Language_ID, L1_log10_st, L2_prop)
|
48 |
+
}
|
49 |
+
|
50 |
+
social_vars <-
|
51 |
+
readxl::read_xlsx(
|
52 |
+
"data/lang_endangerment_predictors.xlsx",
|
53 |
+
sheet = "Supplementary data 1",
|
54 |
+
skip = 1,
|
55 |
+
col_types = "text",
|
56 |
+
na = "NA"
|
57 |
+
) %>%
|
58 |
+
left_join(glottolog_df, by = c("ISO" = "ISO_639")) %>%
|
59 |
+
rename("ISO_639" = "ISO") %>%
|
60 |
+
dplyr::select(
|
61 |
+
Language_ID = Glottocode,
|
62 |
+
ISO_639,
|
63 |
+
official_status,
|
64 |
+
language_of_education,
|
65 |
+
bordering_language_richness
|
66 |
+
) %>%
|
67 |
+
rename(Official = official_status) %>%
|
68 |
+
# naniar::replace_with_na(replace = list(L1_log10 = -Inf, L2_log10 = -Inf)) #removing for now
|
69 |
+
dplyr::mutate(neighboring_languages = bordering_language_richness, Education =
|
70 |
+
language_of_education) %>%
|
71 |
+
dplyr::mutate(neighboring_languages = as.numeric(neighboring_languages)) %>%
|
72 |
+
#dplyr::mutate(neighboring_languages_log10 = log10(neighboring_languages+1)) %>%
|
73 |
+
dplyr::mutate(neighboring_languages_st = scale(neighboring_languages)[, 1]) %>%
|
74 |
+
#dplyr::mutate(neighboring_languages_log10_st = scale(neighboring_languages_log10)[,1]) %>%
|
75 |
+
dplyr::select(Language_ID, Education, Official, neighboring_languages_st)
|
76 |
+
|
77 |
+
if (sample == "full") {
|
78 |
+
social_vars %>%
|
79 |
+
left_join(data_ethnologue, by = c("Language_ID")) %>%
|
80 |
+
dplyr::select(
|
81 |
+
Language_ID,
|
82 |
+
L1_log10_st,
|
83 |
+
L1_log10,
|
84 |
+
L2_prop,
|
85 |
+
Education,
|
86 |
+
Official,
|
87 |
+
neighboring_languages_st
|
88 |
+
) %>%
|
89 |
+
write_tsv(here(OUTPUTDIR_data_wrangling, "pop_full.tsv"))
|
90 |
+
} else{
|
91 |
+
social_vars %>%
|
92 |
+
left_join(data_ethnologue, by = c("Language_ID")) %>%
|
93 |
+
dplyr::select(Language_ID,
|
94 |
+
L1_log10_st,
|
95 |
+
L2_prop,
|
96 |
+
Education,
|
97 |
+
Official,
|
98 |
+
neighboring_languages_st) %>%
|
99 |
+
write_tsv(here(OUTPUTDIR_data_wrangling, "pop_reduced.tsv"))
|
100 |
+
}
|
101 |
+
|
102 |
+
glottolog_df_ISO <- glottolog_df %>%
|
103 |
+
dplyr::select("Language_ID", "ISO_639")
|
104 |
+
|
105 |
+
if (sample == "reduced") {
|
106 |
+
social_vars %>%
|
107 |
+
left_join(data_ethnologue, by = c("Language_ID")) %>%
|
108 |
+
dplyr::select(Language_ID,
|
109 |
+
L1_log10_st,
|
110 |
+
L2_prop,
|
111 |
+
Education,
|
112 |
+
Official,
|
113 |
+
neighboring_languages_st) %>%
|
114 |
+
left_join(glottolog_df_ISO,
|
115 |
+
by = c("Language_ID")) %>%
|
116 |
+
write_tsv(here(OUTPUTDIR_data_wrangling, "pop_reduced_with_ISO.tsv"))
|
117 |
+
}
|
101/replication_package/creating_boundness_metric.R
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#boundness/fusion
|
2 |
+
|
3 |
+
#Script was written by Hedvig Skirgård
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
OUTPUTDIR1 <- file.path('.', "output", "Bound_morph")
|
8 |
+
# create output dir if it does not exist.
|
9 |
+
if (!dir.exists(OUTPUTDIR1)) {
|
10 |
+
dir.create(OUTPUTDIR1)
|
11 |
+
}
|
12 |
+
|
13 |
+
if (!file.exists(here(OUTPUTDIR1, "bound_morph_score.tsv"))) {
|
14 |
+
GB_wide <-
|
15 |
+
read_tsv(file.path("data", "GB_wide", "GB_wide_strict.tsv"),
|
16 |
+
col_types = WIDE_COLSPEC)
|
17 |
+
|
18 |
+
#read in sheet with scores for whether a feature denotes fusion
|
19 |
+
GB_fusion_points <-
|
20 |
+
data.table::fread(
|
21 |
+
file.path("data", "GB_wide", "parameters.csv"),
|
22 |
+
encoding = 'UTF-8',
|
23 |
+
quote = "\"",
|
24 |
+
header = TRUE,
|
25 |
+
sep = ","
|
26 |
+
) %>%
|
27 |
+
dplyr::select(Parameter_ID = ID, Fusion = boundness, informativity) %>%
|
28 |
+
mutate(Fusion = as.numeric(Fusion))
|
29 |
+
|
30 |
+
df_morph_count <- GB_wide %>%
|
31 |
+
filter(na_prop <= 0.25) %>% #exclude languages with more than 25% missing data
|
32 |
+
dplyr::select(-na_prop) %>%
|
33 |
+
reshape2::melt(id.vars = "Language_ID") %>%
|
34 |
+
dplyr::rename(Parameter_ID = variable) %>%
|
35 |
+
inner_join(GB_fusion_points, by = "Parameter_ID") %>%
|
36 |
+
filter(Fusion == 1) %>%
|
37 |
+
filter(!is.na(value)) %>%
|
38 |
+
group_by(Language_ID) %>%
|
39 |
+
dplyr::summarise(mean_morph = mean(value)) %>%
|
40 |
+
dplyr::select(Language_ID, boundness = mean_morph)
|
41 |
+
|
42 |
+
boundness_st = scale(df_morph_count$boundness)
|
43 |
+
df_morph_count <- cbind(df_morph_count, boundness_st)
|
44 |
+
|
45 |
+
df_morph_count %>%
|
46 |
+
write_tsv(file.path(OUTPUTDIR1, "bound_morph_score.tsv"))
|
47 |
+
|
48 |
+
}
|
101/replication_package/creating_informativity_score.R
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#informativity
|
2 |
+
source("requirements.R")
|
3 |
+
|
4 |
+
#Script was written by Hedvig Skirgård
|
5 |
+
|
6 |
+
OUTPUTDIR2 <- file.path('.', "output", "Informativity")
|
7 |
+
# create output dir if it does not exist.
|
8 |
+
if (!dir.exists(OUTPUTDIR2)) { dir.create(OUTPUTDIR2) }
|
9 |
+
|
10 |
+
if (!file.exists(here(OUTPUTDIR2, "informativity_score.tsv"))) {
|
11 |
+
|
12 |
+
GB_wide <-
|
13 |
+
read_tsv(file.path("data", "GB_wide", "GB_wide_strict.tsv"),
|
14 |
+
show_col_types = F) %>%
|
15 |
+
filter(na_prop <= 0.25) %>%
|
16 |
+
dplyr::select(-na_prop)
|
17 |
+
|
18 |
+
#read in sheet with scores for whether a feature denotes informativity
|
19 |
+
GB_informativity_points <- read_csv(file.path("data", "GB_wide", "parameters.csv"),
|
20 |
+
show_col_types = F) %>%
|
21 |
+
dplyr::select(Parameter_ID = ID, informativity) %>%
|
22 |
+
mutate(informativity = replace(informativity, Parameter_ID == "GB177", "argumentanimacy")) %>% #manually adding another parameter: assigning the parameter of GB177 ("Can the verb carry a marker of animacy of argument, unrelated to any gender/noun class of the argument visible in the NP domain?") feature to be informative
|
23 |
+
filter(!is.na(informativity))
|
24 |
+
|
25 |
+
GB_long_for_calc <- GB_wide %>%
|
26 |
+
reshape2::melt(id.vars = "Language_ID") %>%
|
27 |
+
rename(Parameter_ID = variable) %>%
|
28 |
+
inner_join(GB_informativity_points , by = "Parameter_ID")
|
29 |
+
|
30 |
+
##informativity score
|
31 |
+
lg_df_informativity_score <- GB_long_for_calc %>%
|
32 |
+
mutate(value = if_else(Parameter_ID == "GB140", abs(value - 1), value)) %>% # reversing GB140 because 0 is the informative state
|
33 |
+
group_by(Language_ID, informativity) %>% #grouping per language and per informativity category
|
34 |
+
summarise(sum_informativity = sum(value, na.rm = T),
|
35 |
+
#for each informativity cateogry for each langauge, how many are answered 1 ("yes")
|
36 |
+
sum_na = sum(is.na(value))) %>% #how many of the values per informativity category are missing
|
37 |
+
mutate(sum_informativity = ifelse(sum_na >= 1 &
|
38 |
+
sum_informativity == 0, NA, sum_informativity)) %>% #if there is at least one NA and the sum of values for the entire category is 0, the informativity score should be NA because there could be a 1 hiding under the NA value
|
39 |
+
mutate(informativity_score = ifelse(sum_informativity >= 1, 1, sum_informativity)) %>%
|
40 |
+
ungroup() %>%
|
41 |
+
group_by(Language_ID) %>%
|
42 |
+
summarise(`Informativity` = mean(informativity_score, na.rm = T, .groups = "drop")) %>%
|
43 |
+
dplyr::select(Language_ID, `Informativity`)
|
44 |
+
|
45 |
+
informativity_st = scale(lg_df_informativity_score$Informativity)
|
46 |
+
lg_df_informativity_score <-
|
47 |
+
cbind(lg_df_informativity_score, informativity_st)
|
48 |
+
|
49 |
+
lg_df_informativity_score %>%
|
50 |
+
write_tsv(here(OUTPUTDIR2, "informativity_score.tsv"))
|
51 |
+
}
|
101/replication_package/data/GB_wide/parameters.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:effcb8e4ae1ef49e5558a828a2efba5b446c9c289e564fabd822e4b3d1739083
|
3 |
+
size 955498
|
101/replication_package/data/complexity_data_WALS.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:05af559ebdfd0fb8a4a84f0ab525fbce3f2609eb43301fb0edeb310a12b9805a
|
3 |
+
size 49149
|
101/replication_package/data/glottolog-cldf_wide_df.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fa70feae51fe695ae91d4ec29e5f78fb90bc26e507ede909dd3c10d45e14852b
|
3 |
+
size 10892168
|
101/replication_package/data/lang_endangerment_predictors.xlsx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:65ba3f53b2eb30352d758c326b4889910022adb64b45bdead1d0fbfa00df6e69
|
3 |
+
size 5040478
|
101/replication_package/data/phylogenies/EDGE6635-merged-relabelled.tree
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a99c4b3cda474be6091ed433fa3fc5ba50f55082e599319b6a6677b5baacbc7f
|
3 |
+
size 20461790
|
101/replication_package/data_wrangling/ethnologue_pop_SM.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42cde751f7f3555c3b9fa4b7bf70a5f0e9c5927c9524286834759370e5c82d90
|
3 |
+
size 228185
|
101/replication_package/data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bf9d6ae875e98a5bb4cecfcfee4dd69f0f70949205ff6e3b3f3a6df0430d6e25
|
3 |
+
size 639778
|
101/replication_package/data_wrangling/glottolog_AUTOTYPE_areas.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7365af670f13ad1fee3af423a7fb2c1a508c8dcf90ddd086e43571893ee77818
|
3 |
+
size 93225
|
101/replication_package/data_wrangling/glottolog_cldf_wide_df.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4171fe67d3786919c87dae3a64b85524d6b5c636311d8845e1137e84878f76bc
|
3 |
+
size 10906052
|
101/replication_package/data_wrangling/pop_reduced.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8ef392e4f37b4afafe44312bd52cbfdb5d182ce23bcdac0d8d036d419b839a29
|
3 |
+
size 289960
|
101/replication_package/data_wrangling/pop_reduced_with_ISO.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bbd2c0fbc91a2b9bd7b6ae38f259840b0a27173af6d86ac8d163e84adc0ab5ae
|
3 |
+
size 316008
|
101/replication_package/data_wrangling/wrangled.tree
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f50cb6ea90d4e3edd5aa26127e19ea1f97dc97a4a809994bcd4cece8720bc522
|
3 |
+
size 80074
|
101/replication_package/generating_GB_input_file.R
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("requirements.R")
|
2 |
+
|
3 |
+
if (!dir.exists("./grambank-analysed/R_grambank/output")) {
|
4 |
+
dir.create("./grambank-analysed/R_grambank/output")
|
5 |
+
}
|
6 |
+
|
7 |
+
#creating a full Grambank file: first, within the submodule itself, and next placing it in the data folder within the repository
|
8 |
+
|
9 |
+
setwd("grambank-analysed/R_grambank")
|
10 |
+
|
11 |
+
if (!(file.exists("./../../data/GB_wide/GB_wide_strict.tsv"))) {
|
12 |
+
cat("Generating GB_wide_strict.\n")
|
13 |
+
|
14 |
+
source("make_wide.R")
|
15 |
+
|
16 |
+
read_tsv("output/GB_wide/GB_wide_strict.tsv", show_col_types = F) %>%
|
17 |
+
write_tsv(file = "../../data/GB_wide/GB_wide_strict.tsv")
|
18 |
+
|
19 |
+
}
|
20 |
+
|
21 |
+
#extracting glottolog: first, within the submodule itself, and next placing it in the data folder within the repository
|
22 |
+
|
23 |
+
if (!(file.exists("../../../data_wrangling/glottolog_cldf_wide_df.tsv"))) {
|
24 |
+
cat("Generating glottolog table.\n")
|
25 |
+
|
26 |
+
source("make_glottolog-cldf_table.R")
|
27 |
+
|
28 |
+
read_tsv("output/non_GB_datasets/glottolog-cldf_wide_df.tsv",
|
29 |
+
show_col_types = F) %>%
|
30 |
+
write_tsv(file = "./../../data_wrangling/glottolog_cldf_wide_df.tsv")
|
31 |
+
}
|
32 |
+
|
33 |
+
if (!(file.exists("./../data/GB_wide/parameters.csv"))) {
|
34 |
+
cat("Generating parameters table.\n")
|
35 |
+
|
36 |
+
read_csv("../grambank/cldf/parameters.csv", show_col_types = F) %>%
|
37 |
+
dplyr::select(ID, Name, Description, boundness = Boundness, informativity = Informativity) %>%
|
38 |
+
write_csv(file = "../../data/GB_wide/parameters.csv")
|
39 |
+
}
|
40 |
+
|
41 |
+
|
42 |
+
setwd("../../")
|
101/replication_package/get_external_data.R
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Script was written by Hedvig Skirgård
|
2 |
+
|
3 |
+
source("requirements.R")
|
4 |
+
|
5 |
+
#setting up a tempfile path where we can put the zipped files before unzipped to a specific location
|
6 |
+
filepath <- file.path(tempfile())
|
7 |
+
|
8 |
+
##grambank-analysed: downloading, zipping and moving
|
9 |
+
grambank_analysed_fn <- c("https://zenodo.org/record/7740822/files/grambank/grambank-analysed-v1.0.zip")
|
10 |
+
|
11 |
+
utils::download.file(file.path(grambank_analysed_fn), destfile = filepath)
|
12 |
+
utils::unzip(zipfile = filepath, exdir = "grambank-analysed")
|
13 |
+
|
14 |
+
#Zenodo locations contain a dir with the name of the repos and the commit in the release. This is not convenient for later scripts, so we move the contents up one level
|
15 |
+
old_fn <- "grambank-analysed/grambank-grambank-analysed-fcf971a/"
|
16 |
+
old_fn_files <- list.files(old_fn)
|
17 |
+
new_fn <- "grambank-analysed/"
|
18 |
+
|
19 |
+
file.copy(from = paste0(old_fn, old_fn_files),to = new_fn, recursive = T, overwrite = T)
|
20 |
+
#remove old dir
|
21 |
+
unlink(old_fn, recursive = T)
|
22 |
+
|
23 |
+
## dirs within grambank-analysed
|
24 |
+
# for the dirs within grambank-analysed we can fetch them with a for loop
|
25 |
+
|
26 |
+
fns_within_grambank_analysed_zip<- c("https://zenodo.org/record/7740140/files/grambank/grambank-v1.0.zip", "https://zenodo.org/record/5772649/files/glottolog/glottolog-cldf-v4.5.zip", "https://zenodo.org/record/6255206/files/autotyp-data-v1.0.1.zip")
|
27 |
+
exdir_names <- c("grambank-analysed/grambank", "grambank-analysed/glottolog-cldf", "grambank-analysed/autotyp-data")
|
28 |
+
|
29 |
+
commit_dir_names <- c("grambank-analysed/grambank/grambank-grambank-9e0f341/", "grambank-analysed/glottolog-cldf/glottolog-glottolog-cldf-6f1558e/", "grambank-analysed/autotyp-data/autotyp-data-1.0.1/")
|
30 |
+
|
31 |
+
|
32 |
+
for(n in 1:3){
|
33 |
+
utils::download.file(file.path(fns_within_grambank_analysed_zip[n]), destfile = filepath)
|
34 |
+
utils::unzip(zipfile = filepath, exdir = exdir_names[n])
|
35 |
+
|
36 |
+
old_fn <- commit_dir_names[n]
|
37 |
+
old_fn_files <- list.files(old_fn)
|
38 |
+
new_fn <- exdir_names[n]
|
39 |
+
|
40 |
+
file.copy(from = paste0(old_fn, old_fn_files),to = new_fn, recursive = T, overwrite = T)
|
41 |
+
#remove old dir
|
42 |
+
unlink(old_fn, recursive = T)
|
43 |
+
|
44 |
+
}
|
45 |
+
|
46 |
+
|
101/replication_package/install_and_load_INLA.R
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#installing and loading INLA
|
2 |
+
|
3 |
+
# script was written by Hedvig Skirgård and Sam Passmore
|
4 |
+
|
5 |
+
# 1. Install/update and load BiocManager and other necessary packages
|
6 |
+
source("requirements.R")
|
7 |
+
|
8 |
+
if (!is_installed("INLA")) {message("INLA wasn't installed, it is now being installed.")
|
9 |
+
|
10 |
+
# 2. Install INLA dependencies with BiocManager using:
|
11 |
+
BiocManager::install(c("grap","Rgraphviz","sf","rgdal","rgl","spdep"), force=TRUE)#, version = '3.15') #version indication is relevant only for R version 4.2.0
|
12 |
+
#grap package is not available for Bioconductor 3.16 (latest version)
|
13 |
+
|
14 |
+
# 3. Install INLA using:
|
15 |
+
# NOTE: This is a big download
|
16 |
+
install.packages("INLA", repos=c(getOption("repos"),
|
17 |
+
INLA="https://inla.r-inla-download.org/R/testing"), dep=TRUE)
|
18 |
+
|
19 |
+
}
|
20 |
+
|
21 |
+
library(INLA)
|
22 |
+
inla.setOption(inla.mode="experimental")
|
101/replication_package/make_ethnologue_SM_and_merging_tables.R
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("requirements.R")
|
2 |
+
|
3 |
+
#Script was written by Hedvig Skirgård
|
4 |
+
|
5 |
+
#this script necessitates that grambank and glottolog files exist. if they do not, run generate_GB_input_file.R
|
6 |
+
#this script generates the Ethnologue file that can be published based on the raw ethnologue file from SIL. The raw file CANNOT be shared publicly, but derived information that cannot be transformed back to the original values can, such as scaled and log-transformed values
|
7 |
+
|
8 |
+
glottolog_df <- read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", show_col_types = F) %>%
|
9 |
+
dplyr::select(ISO_639 = ISO639P3code, Glottocode, Language_level_ID) %>%
|
10 |
+
mutate(Language_level_ID = ifelse(is.na(Language_level_ID), Glottocode, Language_level_ID))
|
11 |
+
|
12 |
+
GB <- read_tsv("data/GB_wide/GB_wide_strict.tsv", show_col_types = F) %>%
|
13 |
+
dplyr::select(Glottocode = "Language_ID")
|
14 |
+
|
15 |
+
#this script needs the Table_of_languages.tab file to exists, which is only available to people with an SIL lisence
|
16 |
+
data_ethnologue <- read_tsv("data/Table_of_Languages.tab", show_col_types = F) %>%
|
17 |
+
filter(!is.na("All_Users")) %>% #remove rows with missing data for pop of all users
|
18 |
+
filter(!is.na("L1_Users")) %>%
|
19 |
+
left_join(glottolog_df, by = "ISO_639" ) %>%
|
20 |
+
dplyr::select(-Glottocode) %>% #removing old Glottocode column
|
21 |
+
rename(Glottocode = Language_level_ID) %>%
|
22 |
+
group_by(Glottocode) %>%
|
23 |
+
summarise(All_Users = sum(All_Users, na.rm = T),
|
24 |
+
L1_Users = sum(L1_Users, na.rm = T),
|
25 |
+
ISO_639 = paste0(ISO_639, collapse = "; "))
|
26 |
+
|
27 |
+
#do some the subsettting to GB and log10 and L2 prop
|
28 |
+
data_ethnologue <- data_ethnologue %>%
|
29 |
+
inner_join(GB, by = "Glottocode" ) %>%
|
30 |
+
dplyr::mutate(L2 = All_Users - L1_Users,
|
31 |
+
#calculating the number of L2 users by subtracting the number of L1 from All users
|
32 |
+
L2_prop = L2/ All_Users,
|
33 |
+
#calculating the proportion of L2 users out of the entire population
|
34 |
+
L1_log10 = log10(L1_Users+1),
|
35 |
+
All_Users_log10 = log10(All_Users+1)) %>% #adding a 1 for cases where pop is 0
|
36 |
+
mutate(L2_prop = ifelse(All_Users == 0, 0, L2_prop)) %>% #if All users is 0, L2_prop would be NA if we didn't do this (can't divide by 0). It should be 0
|
37 |
+
dplyr::select(Glottocode, ISO_639, L1_log10, L2_prop, L1_Users, All_Users_log10, All_Users)
|
38 |
+
|
39 |
+
#do the scaling
|
40 |
+
data_ethnologue$L1_scaled <- scale(data_ethnologue$L1_Users)[,1]
|
41 |
+
data_ethnologue$L1_log10_scaled <- scale(data_ethnologue$L1_log10)[,1]
|
42 |
+
|
43 |
+
data_ethnologue$All_Users_scaled <- scale(data_ethnologue$All_Users)[,1]
|
44 |
+
data_ethnologue$All_Users_log10_scaled <- scale(data_ethnologue$All_Users_log10)[,1]
|
45 |
+
|
46 |
+
#write to file: Ethnologue data for supplementary materials and merging into "reduced" version of the final dataset with social variables (excluding L1_log10)
|
47 |
+
data_ethnologue %>%
|
48 |
+
dplyr::select(ISO_639, Language_ID=Glottocode, L2_prop, L1_scaled, L1_log10_scaled, All_Users_scaled, All_Users_log10_scaled) %>%
|
49 |
+
write_tsv("data_wrangling/ethnologue_pop_SM.tsv")
|
50 |
+
|
51 |
+
#write to file: Ethnologue data for merging into "full" version of the final dataset with social variables (including L1_log10) - won't be available to public
|
52 |
+
data_ethnologue %>%
|
53 |
+
dplyr::select(ISO_639, Language_ID=Glottocode, L2_prop, L1_st = L1_scaled, L1_log10_st=L1_log10_scaled, L1_log10 ) %>%
|
54 |
+
write_tsv("data_wrangling/ethnologue_pop_full.tsv")
|
101/replication_package/make_ethnologue_SM_for_morphological_complexity_reanalysis.R
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
source("requirements.R")
|
2 |
+
|
3 |
+
#Script was written by Hedvig Skirgård and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
#this script generates the Ethnologue file that can be published based on the raw ethnologue file from SIL. The raw file CANNOT be shared publicly, but derived information that cannot be transformed back to the original values can, such as scaled and log-transformed values
|
6 |
+
|
7 |
+
#this script needs the Table_of_languages.tab file to exists, which is only available to people with an SIL lisence
|
8 |
+
data_ethnologue <- read_tsv("data/Table_of_Languages.tab", show_col_types = F) %>%
|
9 |
+
filter(!is.na("All_Users")) %>% #remove rows with missing data for pop of all users
|
10 |
+
filter(!is.na("L1_Users")) %>%
|
11 |
+
dplyr::select(ISO_639, L1_Users, All_Users)
|
12 |
+
|
13 |
+
|
14 |
+
#do some the subsettting to GB and log10 and L2 prop
|
15 |
+
data_ethnologue <- data_ethnologue %>%
|
16 |
+
dplyr::mutate(L2 = All_Users - L1_Users,
|
17 |
+
#calculating the number of L2 users by subtracting the number of L1 from All users
|
18 |
+
L2_prop = L2/ All_Users,
|
19 |
+
#calculating the proportion of L2 users out of the entire population
|
20 |
+
L1_log10 = log10(L1_Users+1),
|
21 |
+
All_Users_log10 = log10(All_Users+1)) %>% #adding a 1 for cases where pop is 0
|
22 |
+
mutate(L2_prop = ifelse(All_Users == 0, 0, L2_prop)) %>% #if All users is 0, L2_prop would be NA if we didn't do this (can't divide by 0). It should be 0
|
23 |
+
dplyr::select(ISO_639, L1_log10, L2_prop, L1_Users, All_Users_log10, All_Users)
|
24 |
+
|
25 |
+
#do the scaling
|
26 |
+
data_ethnologue$L1_scaled <- scale(data_ethnologue$L1_Users)[,1]
|
27 |
+
data_ethnologue$L1_log10_scaled <- scale(data_ethnologue$L1_log10)[,1]
|
28 |
+
|
29 |
+
data_ethnologue$All_Users_scaled <- scale(data_ethnologue$All_Users)[,1]
|
30 |
+
data_ethnologue$All_Users_log10_scaled <- scale(data_ethnologue$All_Users_log10)[,1]
|
31 |
+
|
32 |
+
#write to file: Ethnologue data for supplementary materials and merging into "reduced" version of the final dataset with social variables (excluding L1_log10)
|
33 |
+
data_ethnologue_file <- data_ethnologue %>%
|
34 |
+
dplyr::select(ISO_639, L2_prop, L1_scaled, L1_log10_scaled, All_Users_scaled, All_Users_log10_scaled)
|
35 |
+
|
36 |
+
data_ethnologue_file <- data_ethnologue_file %>%
|
37 |
+
write_tsv("data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv")
|
101/replication_package/measuring_phylosignal.R
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#global tree
|
2 |
+
source("set_up_inla.R")
|
3 |
+
|
4 |
+
metrics_joined <- metrics_joined %>%
|
5 |
+
filter(!is.na(L1_log10_st)) %>%
|
6 |
+
rename(L1_log_st = L1_log10_st) %>%
|
7 |
+
mutate(L1_copy = L1_log_st) %>%
|
8 |
+
filter(!is.na(L2_prop)) %>%
|
9 |
+
mutate(L2_copy = L2_prop) %>%
|
10 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
11 |
+
filter(!is.na(Official)) %>%
|
12 |
+
filter(!is.na(Education)) %>%
|
13 |
+
filter(!is.na(boundness_st)) %>%
|
14 |
+
filter(!is.na(informativity_st))
|
15 |
+
|
16 |
+
#dropping tips not in Grambank
|
17 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
18 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
19 |
+
|
20 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
21 |
+
|
22 |
+
#measuring phylogenetic signal of boundness/fusion
|
23 |
+
boundness<-setNames(metrics_joined$boundness_st, metrics_joined$Language_ID)
|
24 |
+
physig_boundness_l <- phytools::phylosig(tree, boundness, method="lambda", test=TRUE)
|
25 |
+
lambda_boundness_l <- physig_boundness_l[1][["lambda"]]
|
26 |
+
LR_boundness_l <- 2*(physig_boundness_l$logL-physig_boundness_l$logL0) #performing likelihood ratio test
|
27 |
+
P_lambda_boundness_l <- physig_boundness_l$P
|
28 |
+
|
29 |
+
#measuring phylogenetic signal of informativity
|
30 |
+
informativity<-setNames(metrics_joined$informativity_st, metrics_joined$Language_ID)
|
31 |
+
physig_informativity_l <- phytools::phylosig(tree, informativity, method="lambda", test=TRUE)
|
32 |
+
lambda_informativity_l <- physig_informativity_l[1][["lambda"]]
|
33 |
+
LR_informativity_l <- 2*(physig_informativity_l$logL-physig_informativity_l$logL0) #performing likelihood ratio test
|
34 |
+
P_lambda_informativity_l <- physig_informativity_l$P
|
35 |
+
|
36 |
+
boundness_signal <- c(physig_boundness_l$logL, physig_boundness_l$logL0, LR_boundness_l, lambda_boundness_l, P_lambda_boundness_l)
|
37 |
+
informativity_signal <- c(physig_informativity_l$logL, physig_informativity_l$logL0, LR_informativity_l, lambda_informativity_l, P_lambda_informativity_l)
|
38 |
+
|
39 |
+
|
40 |
+
#Making a table out of two measures of phylogenetic signal
|
41 |
+
physig <- as.data.frame(rbind(boundness_signal, informativity_signal))
|
42 |
+
colnames(physig) <- c("logL", "logL0", "LR (lambda)", "lambda", "p-value")
|
43 |
+
physig <- round(physig, digits=2)
|
44 |
+
physig$`p-value` <- ifelse(physig$`p-value` < 0.001, "< 0.001", physig$`p-value`)
|
45 |
+
features <- as.data.frame(c("fusion", "informativity"))
|
46 |
+
colnames(features) <- "Feature"
|
47 |
+
physig <- cbind(features, physig)
|
48 |
+
write.csv(physig, file=here("output_tables", "Table_phylosig.csv"), row.names = FALSE)
|
101/replication_package/models_Boundness_phylogenetic_spatial.R
ADDED
@@ -0,0 +1,412 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Boundness predicted by combinations of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
#"regional" set of parameters
|
54 |
+
spatial_covar_mat_2 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")], cov.pars = phi_2, kappa = kappa)$varcov
|
55 |
+
typical_variance_spatial_2 = exp(mean(log(diag(spatial_covar_mat_2))))
|
56 |
+
spatial_cov_std_2 = spatial_covar_mat_2 / typical_variance_spatial_2
|
57 |
+
spatial_prec_mat_2 = solve(spatial_cov_std_2)
|
58 |
+
dimnames(spatial_prec_mat_2) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
59 |
+
|
60 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
61 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
62 |
+
metrics_joined$phy_id = phy_id
|
63 |
+
|
64 |
+
## Other effects are in the same order they appear in the dataset
|
65 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
66 |
+
|
67 |
+
#Preparing the formulas for 7 competing models to be used in inla() call
|
68 |
+
listcombo <- list(#phylogenetic and spatial effects in isolation
|
69 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)"),
|
70 |
+
c("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)"),
|
71 |
+
c("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)"),
|
72 |
+
c("f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)"),
|
73 |
+
#phylogenetic and distinct spatial effects in combination
|
74 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
75 |
+
"f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)"),
|
76 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
77 |
+
"f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)"),
|
78 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
79 |
+
"f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)"))
|
80 |
+
|
81 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
82 |
+
|
83 |
+
predterms <- t(as.data.frame(predterms))
|
84 |
+
|
85 |
+
predterms_short <- predterms
|
86 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
87 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
88 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)", "Spatial: regional", predterms_short, fixed=TRUE)
|
89 |
+
predterms_short <- gsub("f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)", "Areal", predterms_short, fixed=TRUE)
|
90 |
+
|
91 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
92 |
+
number = 1:length(predterms_short))
|
93 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
94 |
+
|
95 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
96 |
+
number = 1:length(predterms_short))
|
97 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
98 |
+
|
99 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
100 |
+
number = 1:length(predterms_short))
|
101 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
102 |
+
|
103 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
104 |
+
|
105 |
+
areal_element <- data.frame("judgement" = grepl("Areal", predterms_short),
|
106 |
+
number = 1:length(predterms_short))
|
107 |
+
areal_element <- areal_element[areal_element$judgement == TRUE,]$number
|
108 |
+
|
109 |
+
|
110 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
111 |
+
phy_effects_matrix <- matrix(NA, 7, 5)
|
112 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
113 |
+
spa_effects_matrix <- matrix(NA, 7, 5)
|
114 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
115 |
+
area_effects_matrix <- matrix(NA, 7, 5)
|
116 |
+
colnames(area_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
117 |
+
|
118 |
+
intercept_matrix <- matrix(NA, 7, 5)
|
119 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
120 |
+
|
121 |
+
#fitted values
|
122 |
+
fitted_list <- vector("list", 7)
|
123 |
+
names(fitted_list) <- predterms_short
|
124 |
+
|
125 |
+
#marginals of hyperparameters
|
126 |
+
marginals_hyperpar_list_gaussian <- vector("list", 7)
|
127 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
128 |
+
|
129 |
+
marginals_hyperpar_list_phy <- vector("list", 7)
|
130 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
131 |
+
|
132 |
+
marginals_hyperpar_list_spa <- vector("list", 7)
|
133 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
134 |
+
|
135 |
+
marginals_hyperpar_list_area <- vector("list", 7)
|
136 |
+
names(marginals_hyperpar_list_area) <- predterms_short
|
137 |
+
|
138 |
+
|
139 |
+
#marginals of fixed effects
|
140 |
+
marginals_fixed_list_Intercept <- vector("list", 7)
|
141 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
142 |
+
|
143 |
+
|
144 |
+
#summary statistics of random effects
|
145 |
+
summary_random_list_phy <- vector("list", 7)
|
146 |
+
names(summary_random_list_phy) <- predterms_short
|
147 |
+
|
148 |
+
summary_random_list_spa <- vector("list", 7)
|
149 |
+
names(summary_random_list_spa) <- predterms_short
|
150 |
+
|
151 |
+
summary_random_list_area <- vector("list", 7)
|
152 |
+
names(summary_random_list_area) <- predterms_short
|
153 |
+
|
154 |
+
coefm <- matrix(NA,7,1)
|
155 |
+
result <- vector("list",7)
|
156 |
+
|
157 |
+
for(i in 1:7){
|
158 |
+
formula <- as.formula(paste("boundness_st ~ ",predterms[[i]]))
|
159 |
+
result[[i]] <- inla(formula, family="gaussian",
|
160 |
+
control.family = list(hyper = pcprior_hyper),
|
161 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
162 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
163 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
164 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
165 |
+
|
166 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
167 |
+
|
168 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
169 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
170 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
171 |
+
|
172 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
173 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
174 |
+
|
175 |
+
if(i %in% phylogenetic_element) {
|
176 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
177 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
178 |
+
method = "linear") %>%
|
179 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
180 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
181 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
182 |
+
}
|
183 |
+
|
184 |
+
if(i %in% spatial_element) {
|
185 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
186 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
187 |
+
method = "linear") %>%
|
188 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
189 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
190 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
191 |
+
}
|
192 |
+
|
193 |
+
if(i %in% areal_element){
|
194 |
+
area_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
195 |
+
result[[i]]$marginals.hyperpar$`Precision for AUTOTYP_area`,
|
196 |
+
method = "linear") %>%
|
197 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
198 |
+
area_effects_matrix[i, 4] <- predterms_short[[i]]
|
199 |
+
area_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
200 |
+
}
|
201 |
+
|
202 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
203 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
204 |
+
mutate(across(where(is.numeric), round, 2))
|
205 |
+
|
206 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
207 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
208 |
+
|
209 |
+
if(i %in% phylogenetic_element){
|
210 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
211 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
212 |
+
}
|
213 |
+
|
214 |
+
if(i %in% spatial_element){
|
215 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
216 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
217 |
+
}
|
218 |
+
|
219 |
+
if(i %in% areal_element){
|
220 |
+
marginals_hyperpar_list_area[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for AUTOTYP_area"]]))
|
221 |
+
colnames(marginals_hyperpar_list_area[[i]]) <- c("x for AUTOTYP_area", "y for AUTOTYP_area")
|
222 |
+
}
|
223 |
+
|
224 |
+
if(i %in% phylogenetic_element){
|
225 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
226 |
+
rename(phy_id = ID) %>%
|
227 |
+
as.data.frame() %>%
|
228 |
+
mutate(across(where(is.numeric), round, 2))
|
229 |
+
}
|
230 |
+
|
231 |
+
if(i %in% spatial_element){
|
232 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
233 |
+
rename(sp_id = ID) %>%
|
234 |
+
as.data.frame() %>%
|
235 |
+
mutate(across(where(is.numeric), round, 2))
|
236 |
+
}
|
237 |
+
|
238 |
+
if(i %in% areal_element){
|
239 |
+
summary_random_list_area[[i]] <- cbind(result[[i]]$summary.random$AUTOTYP_area) %>%
|
240 |
+
rename(AUTOTYP_area = ID) %>%
|
241 |
+
as.data.frame() %>%
|
242 |
+
mutate(across(where(is.numeric), round, 2))
|
243 |
+
}
|
244 |
+
}
|
245 |
+
|
246 |
+
#beepr::beep(5)
|
247 |
+
|
248 |
+
save(result, file = "output_models/models_Boundness_phylogenetic_spatial.RData")
|
249 |
+
|
250 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
251 |
+
colnames(coefm) <- c("model", "WAIC")
|
252 |
+
coefm <- coefm %>%
|
253 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
254 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
255 |
+
arrange(WAIC)
|
256 |
+
|
257 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
258 |
+
coefm <- coefm[order(coefm$WAIC),]
|
259 |
+
|
260 |
+
coefm_path <- paste("output_tables/", "waics", "Boundness_phylogenetic_spatial_models", ".csv", collapse = "")
|
261 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
262 |
+
|
263 |
+
for (i in 1:length(fitted_list)) {
|
264 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
265 |
+
}
|
266 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
267 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Boundness_phylogenetic_spatial_models", ".csv", collapse = "")
|
268 |
+
write.csv(fitted_list, fitted_list_path)
|
269 |
+
|
270 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
271 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
272 |
+
area_effects <- as.data.frame(area_effects_matrix)
|
273 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
274 |
+
|
275 |
+
phy_effects$effect <- "phylogenetic SD"
|
276 |
+
spa_effects$effect <- "spatial SD"
|
277 |
+
area_effects$effect <- "areal SD"
|
278 |
+
intercept_effects$effect <- "Intercept"
|
279 |
+
|
280 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, area_effects, intercept_effects))
|
281 |
+
effs <- effs %>%
|
282 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
283 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
284 |
+
na.omit() %>%
|
285 |
+
arrange(WAIC) %>%
|
286 |
+
relocate(model)
|
287 |
+
|
288 |
+
effs_path <- paste("output_tables/", "effects", "Boundness_phylogenetic_spatial_models", ".csv", collapse = "")
|
289 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
290 |
+
|
291 |
+
effs <- read.csv("output_tables/ effects Boundness_phylogenetic_spatial_models .csv")
|
292 |
+
|
293 |
+
effs_table_SM <- effs %>%
|
294 |
+
mutate(effect =
|
295 |
+
dplyr::recode(effect,
|
296 |
+
"areal SD" = "spatial SD")) %>%
|
297 |
+
rename("2.5%"=2,
|
298 |
+
"50%" = 4,
|
299 |
+
"97.5%" = 3) %>%
|
300 |
+
flextable() %>%
|
301 |
+
autofit() %>%
|
302 |
+
merge_v(j=c("model", "WAIC")) %>%
|
303 |
+
fix_border_issues() %>%
|
304 |
+
border_inner_h()
|
305 |
+
|
306 |
+
save_as_docx(
|
307 |
+
"Effects in boundess models with random effects" = effs_table_SM,
|
308 |
+
path = "output_tables/table_SM_effects_Boundness_phylogenetic_spatial_models.docx")
|
309 |
+
|
310 |
+
effs_plot <- effs %>%
|
311 |
+
#filter(WAIC <= top_9) %>%
|
312 |
+
rename(lower=2,
|
313 |
+
upper = 4,
|
314 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
315 |
+
#filter(!effect == "Intercept") %>%
|
316 |
+
mutate(effect =
|
317 |
+
dplyr::recode(effect,
|
318 |
+
"areal SD" = "spatial SD")) %>%
|
319 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "areal SD", "Intercept"))) %>%
|
320 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
321 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
322 |
+
group_by(WAIC) %>%
|
323 |
+
arrange(WAIC) %>%
|
324 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
325 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
326 |
+
|
327 |
+
|
328 |
+
|
329 |
+
#plot modified from function ggregplot::Efxplot
|
330 |
+
cols = c(brewer.pal(12, "Paired"))
|
331 |
+
cols = c(cols[c(12, 10)], "gray50")
|
332 |
+
|
333 |
+
show_col(cols)
|
334 |
+
|
335 |
+
plot_1 <- ggplot(effs_plot,
|
336 |
+
aes(y = as.factor(model),
|
337 |
+
x = mean,
|
338 |
+
group = effect,
|
339 |
+
colour = effect)) +
|
340 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
341 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
342 |
+
scale_color_manual(values=cols) +
|
343 |
+
ylab("Model of boundness") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
344 |
+
theme(axis.text=element_text(size=50),
|
345 |
+
legend.text=element_text(size=50),
|
346 |
+
axis.title=element_text(size=50),
|
347 |
+
legend.title=element_text(size=50),
|
348 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
349 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
350 |
+
|
351 |
+
|
352 |
+
#plot_1
|
353 |
+
ggsave(filename = 'output/SP_models_plot_Boundness_phylogenetic_spatial_models.jpg',
|
354 |
+
plot_1, height = 29, width = 33)
|
355 |
+
|
356 |
+
|
357 |
+
#saving hyperparameters: Gaussian observations
|
358 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
359 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
360 |
+
}
|
361 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
362 |
+
|
363 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Boundness_phylogenetic_spatial_models_marginals_hyperpar_gaussian.csv")
|
364 |
+
|
365 |
+
#saving hyperparameters: phylogenetic
|
366 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
367 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
368 |
+
}
|
369 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
370 |
+
|
371 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables/Boundness_phylogenetic_spatial_models_marginals_hyperpar_phylogenetic.csv")
|
372 |
+
|
373 |
+
#saving hyperparameters: spatial
|
374 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
375 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
376 |
+
}
|
377 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
378 |
+
|
379 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables/Boundness_phylogenetic_spatial_models_marginals_hyperpar_spatial.csv")
|
380 |
+
|
381 |
+
#saving hyperparameters: areas
|
382 |
+
for (i in 1:length(marginals_hyperpar_list_area)) {
|
383 |
+
marginals_hyperpar_list_area[[i]]$model <- names(marginals_hyperpar_list_area)[i]
|
384 |
+
}
|
385 |
+
marginals_hyperpar_list_area <- dplyr::bind_rows(marginals_hyperpar_list_area)
|
386 |
+
|
387 |
+
write.csv(marginals_hyperpar_list_area, "output_tables/Boundness_phylogenetic_spatial_models_marginals_hyperpar_areal.csv")
|
388 |
+
|
389 |
+
|
390 |
+
#saving summaries of random effects: phylogenetic
|
391 |
+
for (i in 1:length(summary_random_list_phy)) {
|
392 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
393 |
+
}
|
394 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
395 |
+
|
396 |
+
write.csv(summary_random_list_phy, "output_tables/Boundness_phylogenetic_spatial_models_summary_random_phy.csv")
|
397 |
+
|
398 |
+
#saving summaries of random effects: spatial
|
399 |
+
for (i in 1:length(summary_random_list_spa)) {
|
400 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
401 |
+
}
|
402 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
403 |
+
|
404 |
+
write.csv(summary_random_list_spa, "output_tables/Boundness_phylogenetic_spatial_models_summary_random_spa.csv")
|
405 |
+
|
406 |
+
#saving summaries of random effects: areas
|
407 |
+
for (i in 1:length(summary_random_list_area)) {
|
408 |
+
summary_random_list_area[[i]]$model <- names(summary_random_list_area)[i]
|
409 |
+
}
|
410 |
+
summary_random_list_area <- dplyr::bind_rows(summary_random_list_area)
|
411 |
+
|
412 |
+
write.csv(summary_random_list_area, "output_tables/Boundness_phylogenetic_spatial_models_summary_random_areas.csv")
|
101/replication_package/models_Boundness_reduced_social.R
ADDED
@@ -0,0 +1,567 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Boundness predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st"),
|
63 |
+
|
64 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L2_prop"),
|
67 |
+
|
68 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
#unavailable
|
75 |
+
# c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log10:L2_prop"),
|
76 |
+
|
77 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "neighboring_languages_st"),
|
78 |
+
|
79 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Official"),
|
80 |
+
|
81 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Education"))
|
82 |
+
|
83 |
+
|
84 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
85 |
+
|
86 |
+
predterms <- t(as.data.frame(predterms))
|
87 |
+
|
88 |
+
predterms_short <- predterms
|
89 |
+
|
90 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
92 |
+
|
93 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
94 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
95 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
96 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
97 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
102 |
+
number = 1:length(predterms_short))
|
103 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
104 |
+
|
105 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
106 |
+
number = 1:length(predterms_short))
|
107 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
108 |
+
|
109 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
110 |
+
number = 1:length(predterms_short))
|
111 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
112 |
+
|
113 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
114 |
+
|
115 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
116 |
+
number = 1:length(predterms_short))
|
117 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
118 |
+
|
119 |
+
|
120 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
121 |
+
number = 1:length(predterms_short))
|
122 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
123 |
+
|
124 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
125 |
+
number = 1:length(predterms_short))
|
126 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
127 |
+
#unnecessary
|
128 |
+
#L2_prop_element <- L2_prop_element[-length(L2_prop_element)] #making sure that the interaction term (introduced below) is not treated as belonging to this isolated element
|
129 |
+
|
130 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
131 |
+
number = 1:length(predterms_short))
|
132 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
133 |
+
|
134 |
+
#unnecessary
|
135 |
+
#can use only part of the interaction term within grepl() function
|
136 |
+
# interaction_element <- data.frame("judgement" = grepl(":L2 proportion", predterms_short),
|
137 |
+
# number = 1:length(predterms_short))
|
138 |
+
# interaction_element <- interaction_element[interaction_element$judgement == TRUE,]$number
|
139 |
+
|
140 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
141 |
+
number = 1:length(predterms_short))
|
142 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
143 |
+
|
144 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
145 |
+
number = 1:length(predterms_short))
|
146 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
147 |
+
|
148 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
149 |
+
number = 1:length(predterms_short))
|
150 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
151 |
+
|
152 |
+
models_number <- length(predterms_short)
|
153 |
+
|
154 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
155 |
+
phy_effects_matrix <- matrix(NA, models_number, 5)
|
156 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
157 |
+
spa_effects_matrix <- matrix(NA, models_number, 5)
|
158 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
159 |
+
|
160 |
+
intercept_matrix <- matrix(NA, models_number, 5)
|
161 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
162 |
+
|
163 |
+
social_effects_matrix_L1 <- matrix(NA, models_number, 5)
|
164 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
165 |
+
social_effects_matrix_L1_nl <- matrix(NA, models_number, 5)
|
166 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
167 |
+
social_effects_matrix_L2_prop <- matrix(NA, models_number, 5)
|
168 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
169 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, models_number, 5)
|
170 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
171 |
+
social_effects_matrix_N <- matrix(NA, models_number, 5)
|
172 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
173 |
+
social_effects_matrix_O <- matrix(NA, models_number, 5)
|
174 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
175 |
+
social_effects_matrix_E <- matrix(NA, models_number, 5)
|
176 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
177 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, models_number, 5)
|
178 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
179 |
+
|
180 |
+
#fitted values
|
181 |
+
fitted_list <- vector("list", models_number)
|
182 |
+
names(fitted_list) <- predterms_short
|
183 |
+
|
184 |
+
#marginals of hyperparameters
|
185 |
+
marginals_hyperpar_list_gaussian <- vector("list", models_number)
|
186 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
187 |
+
|
188 |
+
marginals_hyperpar_list_phy <- vector("list", models_number)
|
189 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
190 |
+
|
191 |
+
marginals_hyperpar_list_spa <- vector("list", models_number)
|
192 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
193 |
+
|
194 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", models_number)
|
195 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
196 |
+
|
197 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", models_number)
|
198 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
199 |
+
|
200 |
+
|
201 |
+
#marginals of fixed effects
|
202 |
+
marginals_fixed_list_Intercept <- vector("list", models_number)
|
203 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
204 |
+
|
205 |
+
marginals_fixed_list_L1 <- vector("list", models_number)
|
206 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
207 |
+
|
208 |
+
marginals_fixed_list_L2_prop <- vector("list", models_number)
|
209 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
210 |
+
|
211 |
+
marginals_fixed_list_O <- vector("list", models_number)
|
212 |
+
names(marginals_fixed_list_O) <- predterms_short
|
213 |
+
|
214 |
+
marginals_fixed_list_N <- vector("list", models_number)
|
215 |
+
names(marginals_fixed_list_N) <- predterms_short
|
216 |
+
|
217 |
+
marginals_fixed_list_E <- vector("list", models_number)
|
218 |
+
names(marginals_fixed_list_E) <- predterms_short
|
219 |
+
|
220 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", models_number)
|
221 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
222 |
+
|
223 |
+
|
224 |
+
|
225 |
+
|
226 |
+
#summary statistics of random effects
|
227 |
+
summary_random_list_phy <- vector("list", models_number)
|
228 |
+
names(summary_random_list_phy) <- predterms_short
|
229 |
+
|
230 |
+
summary_random_list_spa <- vector("list", models_number)
|
231 |
+
names(summary_random_list_spa) <- predterms_short
|
232 |
+
|
233 |
+
summary_random_list_social_L1_nl <- vector("list", models_number)
|
234 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
235 |
+
|
236 |
+
summary_random_list_social_L2_prop_nl <- vector("list", models_number)
|
237 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
238 |
+
|
239 |
+
|
240 |
+
coefm <- matrix(NA,models_number,1)
|
241 |
+
result <- vector("list",models_number)
|
242 |
+
|
243 |
+
for(i in 1:models_number){
|
244 |
+
formula <- as.formula(paste("boundness_st ~ ",predterms[[i]]))
|
245 |
+
result[[i]] <- inla(formula, family="gaussian",
|
246 |
+
control.family = list(hyper = pcprior_hyper),
|
247 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
248 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
249 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
250 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
251 |
+
|
252 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
253 |
+
|
254 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
255 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
256 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
257 |
+
|
258 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
259 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
260 |
+
|
261 |
+
if(i %in% phylogenetic_element) {
|
262 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
263 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
264 |
+
method = "linear") %>%
|
265 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
266 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
267 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
268 |
+
}
|
269 |
+
|
270 |
+
if(i %in% spatial_element) {
|
271 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
272 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
273 |
+
method = "linear") %>%
|
274 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
275 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
276 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
277 |
+
}
|
278 |
+
|
279 |
+
|
280 |
+
if(i %in% L1_nl_element){
|
281 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
282 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
283 |
+
method = "linear") %>%
|
284 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
285 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
286 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
287 |
+
}
|
288 |
+
|
289 |
+
if(i %in% L2_prop_nl_element){
|
290 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
291 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
292 |
+
method = "linear") %>%
|
293 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
294 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
295 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
296 |
+
}
|
297 |
+
|
298 |
+
if(i %in% L1_element) {
|
299 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
300 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
301 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
302 |
+
|
303 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
304 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
305 |
+
}
|
306 |
+
|
307 |
+
if(i %in% L2_prop_element) {
|
308 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
309 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
310 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
311 |
+
|
312 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
313 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
314 |
+
}
|
315 |
+
|
316 |
+
#unavailable
|
317 |
+
# if(i %in% interaction_element) {
|
318 |
+
# social_effects_matrix_L1_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.975quant`)
|
319 |
+
# social_effects_matrix_L1_L2_prop[i, 4] <- predterms_short[[i]]
|
320 |
+
# social_effects_matrix_L1_L2_prop[i, 5] <- result[[i]]$waic$waic
|
321 |
+
|
322 |
+
# marginals_fixed_list_L1_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log10:L2_prop"]]))
|
323 |
+
# colnames(marginals_fixed_list_L1_L2_prop[[i]]) <- c("x for L1*L2 proportion", "y for L1*L2 proportion")
|
324 |
+
# }
|
325 |
+
|
326 |
+
if(i %in% neighbour_element) {
|
327 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
328 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
329 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
330 |
+
|
331 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
332 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
333 |
+
}
|
334 |
+
|
335 |
+
if(i %in% official_element) {
|
336 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
337 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
338 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
339 |
+
|
340 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
341 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
342 |
+
}
|
343 |
+
|
344 |
+
if(i %in% education_element) {
|
345 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
346 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
347 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
348 |
+
|
349 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
350 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
351 |
+
}
|
352 |
+
|
353 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
354 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
355 |
+
mutate(across(where(is.numeric), round, 2))
|
356 |
+
|
357 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
358 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
359 |
+
|
360 |
+
if(i %in% phylogenetic_element){
|
361 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
362 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
363 |
+
}
|
364 |
+
|
365 |
+
if(i %in% spatial_element){
|
366 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
367 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
368 |
+
}
|
369 |
+
|
370 |
+
if(i %in% L1_nl_element){
|
371 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
372 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
373 |
+
}
|
374 |
+
|
375 |
+
if(i %in% L2_prop_nl_element){
|
376 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
377 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
378 |
+
}
|
379 |
+
|
380 |
+
if(i %in% phylogenetic_element){
|
381 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
382 |
+
rename(phy_id = ID) %>%
|
383 |
+
as.data.frame() %>%
|
384 |
+
mutate(across(where(is.numeric), round, 2))
|
385 |
+
}
|
386 |
+
|
387 |
+
if(i %in% spatial_element){
|
388 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
389 |
+
rename(sp_id = ID) %>%
|
390 |
+
as.data.frame() %>%
|
391 |
+
mutate(across(where(is.numeric), round, 2))
|
392 |
+
}
|
393 |
+
}
|
394 |
+
|
395 |
+
#beepr::beep(5)
|
396 |
+
|
397 |
+
save(result, file = "output_models_reduced/models_Boundness_social.RData")
|
398 |
+
load("output_models_reduced/models_Boundness_social.RData")
|
399 |
+
|
400 |
+
coefm_copy <- coefm
|
401 |
+
|
402 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
403 |
+
colnames(coefm) <- c("model", "WAIC")
|
404 |
+
coefm <- coefm %>%
|
405 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
406 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
407 |
+
arrange(WAIC)
|
408 |
+
|
409 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
410 |
+
coefm <- coefm[order(coefm$WAIC),]
|
411 |
+
|
412 |
+
coefm_path <- paste("output_tables_reduced/", "waics", "Boundness_social_models", ".csv", collapse = "")
|
413 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
414 |
+
|
415 |
+
for (i in 1:length(fitted_list)) {
|
416 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
417 |
+
}
|
418 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
419 |
+
fitted_list_path <- paste("output_tables_reduced/", "fitted_list", "Boundness_social_models", ".csv", collapse = "")
|
420 |
+
write.csv(fitted_list, fitted_list_path)
|
421 |
+
|
422 |
+
|
423 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
424 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
425 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
426 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
427 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
428 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
429 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
430 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
431 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
432 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
433 |
+
#unavailable
|
434 |
+
#interaction_effects <- as.data.frame(social_effects_matrix_L1_L2_prop)
|
435 |
+
|
436 |
+
phy_effects$effect <- "phylogenetic SD"
|
437 |
+
spa_effects$effect <- "spatial SD"
|
438 |
+
intercept_effects$effect <- "Intercept"
|
439 |
+
L1_effects$effect <- "L1"
|
440 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
441 |
+
L2_prop_effects$effect <- "L2 proportion"
|
442 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
443 |
+
N_effects$effect <- "Neighbours"
|
444 |
+
E_effects$effect <- "Education"
|
445 |
+
O_effects$effect <- "Official status"
|
446 |
+
#unavailable
|
447 |
+
#interaction_effects$effect <- "L1*L2 proportion"
|
448 |
+
|
449 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects))#unavailable: , interaction_effects))
|
450 |
+
effs <- effs %>%
|
451 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
452 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
453 |
+
na.omit() %>%
|
454 |
+
arrange(WAIC) %>%
|
455 |
+
relocate(model)
|
456 |
+
|
457 |
+
effs_path <- paste("output_tables_reduced/", "effects", "Boundness_social_models", ".csv", collapse = "")
|
458 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
459 |
+
|
460 |
+
effs <- read.csv("output_tables_reduced/ effects Boundness_social_models .csv")
|
461 |
+
|
462 |
+
effs_table_Main <- effs %>%
|
463 |
+
rename("2.5%"=2,
|
464 |
+
"50%" = 3,
|
465 |
+
"97.5%" = 4) %>%
|
466 |
+
filter(!grepl("nonlinear", model))
|
467 |
+
|
468 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
469 |
+
|
470 |
+
effs_table_Main <- effs_table_Main %>%
|
471 |
+
relocate(effect, .after = model) %>%
|
472 |
+
flextable() %>%
|
473 |
+
flextable::bold(~ (`2.5%` > 0 & `97.5%` > 0) | (`2.5%` < 0 & `97.5%` < 0), 2) %>%
|
474 |
+
autofit() %>%
|
475 |
+
merge_v(j=c("model", "WAIC")) %>%
|
476 |
+
fix_border_issues() %>%
|
477 |
+
border_inner_h()
|
478 |
+
|
479 |
+
save_as_docx(
|
480 |
+
"Effects in boundness models with fixed and random effects" = effs_table_Main,
|
481 |
+
path = "output_tables_reduced/table_Main_effects_Boundness_social_models.docx")
|
482 |
+
|
483 |
+
|
484 |
+
effs_plot <- effs %>%
|
485 |
+
#filter(WAIC <= top_9) %>%
|
486 |
+
rename(lower=2,
|
487 |
+
upper = 4,
|
488 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
489 |
+
#filter(!effect == "Intercept") %>%
|
490 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status"))) %>% #unavailable, "L1*L2 proportion"))) %>%
|
491 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
492 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
493 |
+
group_by(WAIC) %>%
|
494 |
+
arrange(WAIC) %>%
|
495 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
496 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
497 |
+
|
498 |
+
|
499 |
+
|
500 |
+
#plot modified from function ggregplot::Efxplot
|
501 |
+
cols = c(brewer.pal(12, "Paired"))
|
502 |
+
cols = c(cols[c(12, 10)], "gray50", cols[c(1:8)])
|
503 |
+
|
504 |
+
show_col(cols)
|
505 |
+
|
506 |
+
plot_1 <- ggplot(effs_plot,
|
507 |
+
aes(y = as.factor(model),
|
508 |
+
x = mean,
|
509 |
+
group = effect,
|
510 |
+
colour = effect)) +
|
511 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
512 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
513 |
+
scale_color_manual(values=cols) +
|
514 |
+
ylab("Model of boundness") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
515 |
+
theme(axis.text=element_text(size=50),
|
516 |
+
legend.text=element_text(size=50),
|
517 |
+
axis.title=element_text(size=50),
|
518 |
+
legend.title=element_text(size=50),
|
519 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
520 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
521 |
+
|
522 |
+
|
523 |
+
#plot_1
|
524 |
+
ggsave(filename = 'output_reduced/SP_models_plot_Boundness_social_models.jpg',
|
525 |
+
plot_1, height = 20, width = 45)
|
526 |
+
|
527 |
+
|
528 |
+
#saving hyperparameters: Gaussian observations
|
529 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
530 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
531 |
+
}
|
532 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
533 |
+
|
534 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables_reduced/Boundness_social_models_marginals_hyperpar_gaussian.csv")
|
535 |
+
|
536 |
+
#saving hyperparameters: phylogenetic
|
537 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
538 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
539 |
+
}
|
540 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
541 |
+
|
542 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables_reduced/Boundness_social_models_marginals_hyperpar_phylogenetic.csv")
|
543 |
+
|
544 |
+
#saving hyperparameters: spatial
|
545 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
546 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
547 |
+
}
|
548 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
549 |
+
|
550 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables_reduced/Boundness_social_models_marginals_hyperpar_spatial.csv")
|
551 |
+
|
552 |
+
|
553 |
+
#saving summaries of random effects: phylogenetic
|
554 |
+
for (i in 1:length(summary_random_list_phy)) {
|
555 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
556 |
+
}
|
557 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
558 |
+
|
559 |
+
write.csv(summary_random_list_phy, "output_tables_reduced/Boundness_social_models_summary_random_phy.csv")
|
560 |
+
|
561 |
+
#saving summaries of random effects: spatial
|
562 |
+
for (i in 1:length(summary_random_list_spa)) {
|
563 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
564 |
+
}
|
565 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
566 |
+
|
567 |
+
write.csv(summary_random_list_spa, "output_tables_reduced/Boundness_social_models_summary_random_spa.csv")
|
101/replication_package/models_Boundness_reduced_social_only.R
ADDED
@@ -0,0 +1,433 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Boundness predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("L1_log_st"),
|
63 |
+
|
64 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("L2_prop"),
|
67 |
+
|
68 |
+
c("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("neighboring_languages_st"),
|
75 |
+
|
76 |
+
c("Official"),
|
77 |
+
|
78 |
+
c("Education"))
|
79 |
+
|
80 |
+
|
81 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
82 |
+
|
83 |
+
predterms <- t(as.data.frame(predterms))
|
84 |
+
|
85 |
+
predterms_short <- predterms
|
86 |
+
|
87 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
88 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
89 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
92 |
+
|
93 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
94 |
+
number = 1:length(predterms_short))
|
95 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
96 |
+
|
97 |
+
|
98 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
99 |
+
number = 1:length(predterms_short))
|
100 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
101 |
+
|
102 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
103 |
+
number = 1:length(predterms_short))
|
104 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
105 |
+
|
106 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
107 |
+
number = 1:length(predterms_short))
|
108 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
109 |
+
|
110 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
111 |
+
number = 1:length(predterms_short))
|
112 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
113 |
+
|
114 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
115 |
+
number = 1:length(predterms_short))
|
116 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
117 |
+
|
118 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
119 |
+
number = 1:length(predterms_short))
|
120 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
121 |
+
|
122 |
+
models_number <- length(predterms_short)
|
123 |
+
|
124 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
125 |
+
intercept_matrix <- matrix(NA, models_number, 5)
|
126 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
127 |
+
|
128 |
+
social_effects_matrix_L1 <- matrix(NA, models_number, 5)
|
129 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
130 |
+
social_effects_matrix_L1_nl <- matrix(NA, models_number, 5)
|
131 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
132 |
+
social_effects_matrix_L2_prop <- matrix(NA, models_number, 5)
|
133 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
134 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, models_number, 5)
|
135 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
136 |
+
social_effects_matrix_N <- matrix(NA, models_number, 5)
|
137 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
138 |
+
social_effects_matrix_O <- matrix(NA, models_number, 5)
|
139 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
140 |
+
social_effects_matrix_E <- matrix(NA, models_number, 5)
|
141 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
142 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, models_number, 5)
|
143 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
144 |
+
|
145 |
+
#fitted values
|
146 |
+
fitted_list <- vector("list", models_number)
|
147 |
+
names(fitted_list) <- predterms_short
|
148 |
+
|
149 |
+
#marginals of hyperparameters
|
150 |
+
marginals_hyperpar_list_gaussian <- vector("list", models_number)
|
151 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
152 |
+
|
153 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", models_number)
|
154 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
155 |
+
|
156 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", models_number)
|
157 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
158 |
+
|
159 |
+
|
160 |
+
#marginals of fixed effects
|
161 |
+
marginals_fixed_list_Intercept <- vector("list", models_number)
|
162 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
163 |
+
|
164 |
+
marginals_fixed_list_L1 <- vector("list", models_number)
|
165 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
166 |
+
|
167 |
+
marginals_fixed_list_L2_prop <- vector("list", models_number)
|
168 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
169 |
+
|
170 |
+
marginals_fixed_list_O <- vector("list", models_number)
|
171 |
+
names(marginals_fixed_list_O) <- predterms_short
|
172 |
+
|
173 |
+
marginals_fixed_list_N <- vector("list", models_number)
|
174 |
+
names(marginals_fixed_list_N) <- predterms_short
|
175 |
+
|
176 |
+
marginals_fixed_list_E <- vector("list", models_number)
|
177 |
+
names(marginals_fixed_list_E) <- predterms_short
|
178 |
+
|
179 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", models_number)
|
180 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
181 |
+
|
182 |
+
|
183 |
+
#summary statistics of random effects
|
184 |
+
summary_random_list_social_L1_nl <- vector("list", models_number)
|
185 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
186 |
+
|
187 |
+
summary_random_list_social_L2_prop_nl <- vector("list", models_number)
|
188 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
189 |
+
|
190 |
+
|
191 |
+
coefm <- matrix(NA,models_number,1)
|
192 |
+
result <- vector("list",models_number)
|
193 |
+
|
194 |
+
for(i in 1:models_number){
|
195 |
+
formula <- as.formula(paste("boundness_st ~ ",predterms[[i]]))
|
196 |
+
result[[i]] <- inla(formula, family="gaussian",
|
197 |
+
control.family = list(hyper = pcprior_hyper),
|
198 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
199 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
200 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
201 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
202 |
+
|
203 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
204 |
+
|
205 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
206 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
207 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
208 |
+
|
209 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
210 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
211 |
+
|
212 |
+
if(i %in% L1_nl_element){
|
213 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
214 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
215 |
+
method = "linear") %>%
|
216 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
217 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
218 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
219 |
+
}
|
220 |
+
|
221 |
+
if(i %in% L2_prop_nl_element){
|
222 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
223 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
224 |
+
method = "linear") %>%
|
225 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
226 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
227 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
228 |
+
}
|
229 |
+
|
230 |
+
if(i %in% L1_element) {
|
231 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
232 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
233 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
234 |
+
|
235 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
236 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
237 |
+
}
|
238 |
+
|
239 |
+
if(i %in% L2_prop_element) {
|
240 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
241 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
242 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
243 |
+
|
244 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
245 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
246 |
+
}
|
247 |
+
|
248 |
+
if(i %in% neighbour_element) {
|
249 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
250 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
251 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
252 |
+
|
253 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
254 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
255 |
+
}
|
256 |
+
|
257 |
+
if(i %in% official_element) {
|
258 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
259 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
260 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
261 |
+
|
262 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
263 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
264 |
+
}
|
265 |
+
|
266 |
+
if(i %in% education_element) {
|
267 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
268 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
269 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
270 |
+
|
271 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
272 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
273 |
+
}
|
274 |
+
|
275 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
276 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
277 |
+
mutate(across(where(is.numeric), round, 2))
|
278 |
+
|
279 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
280 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
281 |
+
|
282 |
+
if(i %in% L1_nl_element){
|
283 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
284 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
285 |
+
}
|
286 |
+
|
287 |
+
if(i %in% L2_prop_nl_element){
|
288 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
289 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
290 |
+
}
|
291 |
+
}
|
292 |
+
|
293 |
+
#beepr::beep(5)
|
294 |
+
|
295 |
+
save(result, file = "output_models/models_Boundness_social_only.RData")
|
296 |
+
#load("output_models/models_Boundness_social_only.RData")
|
297 |
+
|
298 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
299 |
+
colnames(coefm) <- c("model", "WAIC")
|
300 |
+
coefm <- coefm %>%
|
301 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
302 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
303 |
+
arrange(WAIC)
|
304 |
+
|
305 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
306 |
+
coefm <- coefm[order(coefm$WAIC),]
|
307 |
+
|
308 |
+
coefm_path <- paste("output_tables_reduced/", "waics", "Boundness_social_only_models", ".csv", collapse = "")
|
309 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
310 |
+
|
311 |
+
for (i in 1:length(fitted_list)) {
|
312 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
313 |
+
}
|
314 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
315 |
+
fitted_list_path <- paste("output_tables_reduced/", "fitted_list", "Boundness_social_only_models", ".csv", collapse = "")
|
316 |
+
write.csv(fitted_list, fitted_list_path)
|
317 |
+
|
318 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
319 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
320 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
321 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
322 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
323 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
324 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
325 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
326 |
+
|
327 |
+
intercept_effects$effect <- "Intercept"
|
328 |
+
L1_effects$effect <- "L1"
|
329 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
330 |
+
L2_prop_effects$effect <- "L2 proportion"
|
331 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
332 |
+
N_effects$effect <- "Neighbours"
|
333 |
+
E_effects$effect <- "Education"
|
334 |
+
O_effects$effect <- "Official status"
|
335 |
+
|
336 |
+
effs <- as.data.frame(rbind(intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects))
|
337 |
+
effs <- effs %>%
|
338 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
339 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
340 |
+
na.omit() %>%
|
341 |
+
arrange(WAIC) %>%
|
342 |
+
relocate(model)
|
343 |
+
|
344 |
+
effs_path <- paste("output_tables_reduced/", "effects", "Boundness_social_only_models", ".csv", collapse = "")
|
345 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
346 |
+
|
347 |
+
effs <- read.csv("output_tables_reduced/ effects Boundness_social_only_models .csv")
|
348 |
+
|
349 |
+
effs_table_SM <- effs %>%
|
350 |
+
rename("2.5%"=2,
|
351 |
+
"50%" = 4,
|
352 |
+
"97.5%" = 3) %>%
|
353 |
+
flextable() %>%
|
354 |
+
autofit() %>%
|
355 |
+
merge_v(j=c("model", "WAIC")) %>%
|
356 |
+
fix_border_issues() %>%
|
357 |
+
border_inner_h()
|
358 |
+
|
359 |
+
save_as_docx(
|
360 |
+
"Effects in boundness models with fixed and random effects (including non-linear implementations of some fixed effects)" = effs_table_SM,
|
361 |
+
path = "output_tables_reduced/table_SM_effects_Boundness_social_only_models.docx")
|
362 |
+
|
363 |
+
effs_table_Main <- effs %>%
|
364 |
+
rename("2.5%"=2,
|
365 |
+
"50%" = 4,
|
366 |
+
"97.5%" = 3) %>%
|
367 |
+
filter(!grepl("nonlinear", model))
|
368 |
+
|
369 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
370 |
+
|
371 |
+
effs_table_Main <- effs_table_Main %>%
|
372 |
+
flextable() %>%
|
373 |
+
autofit() %>%
|
374 |
+
merge_v(j=c("model", "WAIC")) %>%
|
375 |
+
fix_border_issues() %>%
|
376 |
+
border_inner_h()
|
377 |
+
|
378 |
+
save_as_docx(
|
379 |
+
"Effects in boundness models with fixed and random effects" = effs_table_Main,
|
380 |
+
path = "output_tables_reduced/table_Main_effects_Boundness_social_only_models.docx")
|
381 |
+
|
382 |
+
|
383 |
+
effs_plot <- effs %>%
|
384 |
+
#filter(WAIC <= top_9) %>%
|
385 |
+
rename(lower=2,
|
386 |
+
upper = 4,
|
387 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
388 |
+
#filter(!effect == "Intercept") %>%
|
389 |
+
mutate(effect = factor(effect, levels=c("Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status"))) %>%
|
390 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
391 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
392 |
+
group_by(WAIC) %>%
|
393 |
+
arrange(WAIC) %>%
|
394 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
395 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
396 |
+
|
397 |
+
|
398 |
+
#plot modified from function ggregplot::Efxplot
|
399 |
+
cols = c(brewer.pal(12, "Paired"))
|
400 |
+
cols = c("gray50", cols[c(1:8)])
|
401 |
+
|
402 |
+
show_col(cols)
|
403 |
+
|
404 |
+
plot_1 <- ggplot(effs_plot,
|
405 |
+
aes(y = as.factor(model),
|
406 |
+
x = mean,
|
407 |
+
group = effect,
|
408 |
+
colour = effect)) +
|
409 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
410 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
411 |
+
scale_color_manual(values=cols) +
|
412 |
+
ylab("Model of boundness") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
413 |
+
theme(axis.text=element_text(size=50),
|
414 |
+
legend.text=element_text(size=50),
|
415 |
+
axis.title=element_text(size=50),
|
416 |
+
legend.title=element_text(size=50),
|
417 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
418 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
419 |
+
|
420 |
+
|
421 |
+
|
422 |
+
#plot_1
|
423 |
+
ggsave(filename = 'output_reduced/SP_models_plot_Boundness_social_only_models.jpg',
|
424 |
+
plot_1, height = 20, width = 45)
|
425 |
+
|
426 |
+
|
427 |
+
#saving hyperparameters: Gaussian observations
|
428 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
429 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
430 |
+
}
|
431 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
432 |
+
|
433 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables_reduced/Boundness_social_only_models_marginals_hyperpar_gaussian.csv")
|
101/replication_package/models_Boundness_social.R
ADDED
@@ -0,0 +1,560 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Boundness predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st"),
|
63 |
+
|
64 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L2_prop"),
|
67 |
+
|
68 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log10:L2_prop"),
|
75 |
+
|
76 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "neighboring_languages_st"),
|
77 |
+
|
78 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Official"),
|
79 |
+
|
80 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Education"))
|
81 |
+
|
82 |
+
|
83 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
84 |
+
|
85 |
+
predterms <- t(as.data.frame(predterms))
|
86 |
+
|
87 |
+
predterms_short <- predterms
|
88 |
+
|
89 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
91 |
+
|
92 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
93 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
94 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
95 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
96 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
101 |
+
number = 1:length(predterms_short))
|
102 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
103 |
+
|
104 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
105 |
+
number = 1:length(predterms_short))
|
106 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
107 |
+
|
108 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
109 |
+
number = 1:length(predterms_short))
|
110 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
111 |
+
|
112 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
113 |
+
|
114 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
115 |
+
number = 1:length(predterms_short))
|
116 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
117 |
+
|
118 |
+
|
119 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
120 |
+
number = 1:length(predterms_short))
|
121 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
122 |
+
|
123 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
124 |
+
number = 1:length(predterms_short))
|
125 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
126 |
+
L2_prop_element <- L2_prop_element[-length(L2_prop_element)] #making sure that the interaction term (introduced below) is not treated as belonging to this isolated element
|
127 |
+
|
128 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
129 |
+
number = 1:length(predterms_short))
|
130 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
131 |
+
|
132 |
+
#can use only part of the interaction term within grepl() function
|
133 |
+
interaction_element <- data.frame("judgement" = grepl(":L2 proportion", predterms_short),
|
134 |
+
number = 1:length(predterms_short))
|
135 |
+
interaction_element <- interaction_element[interaction_element$judgement == TRUE,]$number
|
136 |
+
|
137 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
138 |
+
number = 1:length(predterms_short))
|
139 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
140 |
+
|
141 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
142 |
+
number = 1:length(predterms_short))
|
143 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
144 |
+
|
145 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
146 |
+
number = 1:length(predterms_short))
|
147 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
148 |
+
|
149 |
+
|
150 |
+
|
151 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
152 |
+
phy_effects_matrix <- matrix(NA, 10, 5)
|
153 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
154 |
+
spa_effects_matrix <- matrix(NA, 10, 5)
|
155 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
156 |
+
|
157 |
+
intercept_matrix <- matrix(NA, 10, 5)
|
158 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
159 |
+
|
160 |
+
social_effects_matrix_L1 <- matrix(NA, 10, 5)
|
161 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
162 |
+
social_effects_matrix_L1_nl <- matrix(NA, 10, 5)
|
163 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
164 |
+
social_effects_matrix_L2_prop <- matrix(NA, 10, 5)
|
165 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
166 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, 10, 5)
|
167 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
168 |
+
social_effects_matrix_N <- matrix(NA, 10, 5)
|
169 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
170 |
+
social_effects_matrix_O <- matrix(NA, 10, 5)
|
171 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
172 |
+
social_effects_matrix_E <- matrix(NA, 10, 5)
|
173 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
174 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, 10, 5)
|
175 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
176 |
+
|
177 |
+
#fitted values
|
178 |
+
fitted_list <- vector("list", 10)
|
179 |
+
names(fitted_list) <- predterms_short
|
180 |
+
|
181 |
+
#marginals of hyperparameters
|
182 |
+
marginals_hyperpar_list_gaussian <- vector("list", 10)
|
183 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
184 |
+
|
185 |
+
marginals_hyperpar_list_phy <- vector("list", 10)
|
186 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
187 |
+
|
188 |
+
marginals_hyperpar_list_spa <- vector("list", 10)
|
189 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
190 |
+
|
191 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", 10)
|
192 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
193 |
+
|
194 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", 10)
|
195 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
196 |
+
|
197 |
+
|
198 |
+
#marginals of fixed effects
|
199 |
+
marginals_fixed_list_Intercept <- vector("list", 10)
|
200 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
201 |
+
|
202 |
+
marginals_fixed_list_L1 <- vector("list", 10)
|
203 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
204 |
+
|
205 |
+
marginals_fixed_list_L2_prop <- vector("list", 10)
|
206 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
207 |
+
|
208 |
+
marginals_fixed_list_O <- vector("list", 10)
|
209 |
+
names(marginals_fixed_list_O) <- predterms_short
|
210 |
+
|
211 |
+
marginals_fixed_list_N <- vector("list", 10)
|
212 |
+
names(marginals_fixed_list_N) <- predterms_short
|
213 |
+
|
214 |
+
marginals_fixed_list_E <- vector("list", 10)
|
215 |
+
names(marginals_fixed_list_E) <- predterms_short
|
216 |
+
|
217 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", 10)
|
218 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
219 |
+
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
#summary statistics of random effects
|
224 |
+
summary_random_list_phy <- vector("list", 10)
|
225 |
+
names(summary_random_list_phy) <- predterms_short
|
226 |
+
|
227 |
+
summary_random_list_spa <- vector("list", 10)
|
228 |
+
names(summary_random_list_spa) <- predterms_short
|
229 |
+
|
230 |
+
summary_random_list_social_L1_nl <- vector("list", 10)
|
231 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
232 |
+
|
233 |
+
summary_random_list_social_L2_prop_nl <- vector("list", 10)
|
234 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
235 |
+
|
236 |
+
|
237 |
+
coefm <- matrix(NA,10,1)
|
238 |
+
result <- vector("list",10)
|
239 |
+
|
240 |
+
for(i in 1:10){
|
241 |
+
formula <- as.formula(paste("boundness_st ~ ",predterms[[i]]))
|
242 |
+
result[[i]] <- inla(formula, family="gaussian",
|
243 |
+
control.family = list(hyper = pcprior_hyper),
|
244 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
245 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
246 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
247 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
248 |
+
|
249 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
250 |
+
|
251 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
252 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
253 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
254 |
+
|
255 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
256 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
257 |
+
|
258 |
+
if(i %in% phylogenetic_element) {
|
259 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
260 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
261 |
+
method = "linear") %>%
|
262 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
263 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
264 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
265 |
+
}
|
266 |
+
|
267 |
+
if(i %in% spatial_element) {
|
268 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
269 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
270 |
+
method = "linear") %>%
|
271 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
272 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
273 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
274 |
+
}
|
275 |
+
|
276 |
+
|
277 |
+
if(i %in% L1_nl_element){
|
278 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
279 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
280 |
+
method = "linear") %>%
|
281 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
282 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
283 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
284 |
+
}
|
285 |
+
|
286 |
+
if(i %in% L2_prop_nl_element){
|
287 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
288 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
289 |
+
method = "linear") %>%
|
290 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
291 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
292 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
293 |
+
}
|
294 |
+
|
295 |
+
if(i %in% L1_element) {
|
296 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
297 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
298 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
299 |
+
|
300 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
301 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
302 |
+
}
|
303 |
+
|
304 |
+
if(i %in% L2_prop_element) {
|
305 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
306 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
307 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
308 |
+
|
309 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
310 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
311 |
+
}
|
312 |
+
|
313 |
+
if(i %in% interaction_element) {
|
314 |
+
social_effects_matrix_L1_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.975quant`)
|
315 |
+
social_effects_matrix_L1_L2_prop[i, 4] <- predterms_short[[i]]
|
316 |
+
social_effects_matrix_L1_L2_prop[i, 5] <- result[[i]]$waic$waic
|
317 |
+
|
318 |
+
marginals_fixed_list_L1_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log10:L2_prop"]]))
|
319 |
+
colnames(marginals_fixed_list_L1_L2_prop[[i]]) <- c("x for L1*L2 proportion", "y for L1*L2 proportion")
|
320 |
+
}
|
321 |
+
|
322 |
+
if(i %in% neighbour_element) {
|
323 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
324 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
325 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
326 |
+
|
327 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
328 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
329 |
+
}
|
330 |
+
|
331 |
+
if(i %in% official_element) {
|
332 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
333 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
334 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
335 |
+
|
336 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
337 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
338 |
+
}
|
339 |
+
|
340 |
+
if(i %in% education_element) {
|
341 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
342 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
343 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
344 |
+
|
345 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
346 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
347 |
+
}
|
348 |
+
|
349 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
350 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
351 |
+
mutate(across(where(is.numeric), round, 2))
|
352 |
+
|
353 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
354 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
355 |
+
|
356 |
+
if(i %in% phylogenetic_element){
|
357 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
358 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
359 |
+
}
|
360 |
+
|
361 |
+
if(i %in% spatial_element){
|
362 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
363 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
364 |
+
}
|
365 |
+
|
366 |
+
if(i %in% L1_nl_element){
|
367 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
368 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
369 |
+
}
|
370 |
+
|
371 |
+
if(i %in% L2_prop_nl_element){
|
372 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
373 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
374 |
+
}
|
375 |
+
|
376 |
+
if(i %in% phylogenetic_element){
|
377 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
378 |
+
rename(phy_id = ID) %>%
|
379 |
+
as.data.frame() %>%
|
380 |
+
mutate(across(where(is.numeric), round, 2))
|
381 |
+
}
|
382 |
+
|
383 |
+
if(i %in% spatial_element){
|
384 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
385 |
+
rename(sp_id = ID) %>%
|
386 |
+
as.data.frame() %>%
|
387 |
+
mutate(across(where(is.numeric), round, 2))
|
388 |
+
}
|
389 |
+
}
|
390 |
+
|
391 |
+
#beepr::beep(5)
|
392 |
+
|
393 |
+
save(result, file = "output_models/models_Boundness_social.RData")
|
394 |
+
load("output_models/models_Boundness_social.RData")
|
395 |
+
|
396 |
+
|
397 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
398 |
+
colnames(coefm) <- c("model", "WAIC")
|
399 |
+
coefm <- coefm %>%
|
400 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
401 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
402 |
+
arrange(WAIC)
|
403 |
+
|
404 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
405 |
+
coefm <- coefm[order(coefm$WAIC),]
|
406 |
+
|
407 |
+
coefm_path <- paste("output_tables/", "waics", "Boundness_social_models", ".csv", collapse = "")
|
408 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
409 |
+
|
410 |
+
for (i in 1:length(fitted_list)) {
|
411 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
412 |
+
}
|
413 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
414 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Boundness_social_models", ".csv", collapse = "")
|
415 |
+
write.csv(fitted_list, fitted_list_path)
|
416 |
+
|
417 |
+
|
418 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
419 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
420 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
421 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
422 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
423 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
424 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
425 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
426 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
427 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
428 |
+
interaction_effects <- as.data.frame(social_effects_matrix_L1_L2_prop)
|
429 |
+
|
430 |
+
phy_effects$effect <- "phylogenetic SD"
|
431 |
+
spa_effects$effect <- "spatial SD"
|
432 |
+
intercept_effects$effect <- "Intercept"
|
433 |
+
L1_effects$effect <- "L1"
|
434 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
435 |
+
L2_prop_effects$effect <- "L2 proportion"
|
436 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
437 |
+
N_effects$effect <- "Neighbours"
|
438 |
+
E_effects$effect <- "Education"
|
439 |
+
O_effects$effect <- "Official status"
|
440 |
+
interaction_effects$effect <- "L1*L2 proportion"
|
441 |
+
|
442 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects, interaction_effects))
|
443 |
+
effs <- effs %>%
|
444 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
445 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
446 |
+
na.omit() %>%
|
447 |
+
arrange(WAIC) %>%
|
448 |
+
relocate(model)
|
449 |
+
|
450 |
+
effs_path <- paste("output_tables/", "effects", "Boundness_social_models", ".csv", collapse = "")
|
451 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
452 |
+
|
453 |
+
effs <- read.csv("output_tables/ effects Boundness_social_models .csv")
|
454 |
+
|
455 |
+
effs_table_Main <- effs %>%
|
456 |
+
rename("2.5%"=2,
|
457 |
+
"50%" = 3,
|
458 |
+
"97.5%" = 4) %>%
|
459 |
+
filter(!grepl("nonlinear", model))
|
460 |
+
|
461 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
462 |
+
|
463 |
+
effs_table_Main <- effs_table_Main %>%
|
464 |
+
relocate(effect, .after = model) %>%
|
465 |
+
flextable() %>%
|
466 |
+
flextable::bold(~ (`2.5%` > 0 & `97.5%` > 0) | (`2.5%` < 0 & `97.5%` < 0), 2) %>%
|
467 |
+
autofit() %>%
|
468 |
+
merge_v(j=c("model", "WAIC")) %>%
|
469 |
+
fix_border_issues() %>%
|
470 |
+
border_inner_h()
|
471 |
+
|
472 |
+
save_as_docx(
|
473 |
+
"Effects in boundness models with fixed and random effects" = effs_table_Main,
|
474 |
+
path = "output_tables/table_Main_effects_Boundness_social_models.docx")
|
475 |
+
|
476 |
+
|
477 |
+
effs_plot <- effs %>%
|
478 |
+
#filter(WAIC <= top_9) %>%
|
479 |
+
rename(lower=2,
|
480 |
+
upper = 4,
|
481 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
482 |
+
#filter(!effect == "Intercept") %>%
|
483 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status", "L1*L2 proportion"))) %>%
|
484 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
485 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
486 |
+
group_by(WAIC) %>%
|
487 |
+
arrange(WAIC) %>%
|
488 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
489 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
#plot modified from function ggregplot::Efxplot
|
494 |
+
cols = c(brewer.pal(12, "Paired"))
|
495 |
+
cols = c(cols[c(12, 10)], "gray50", cols[c(1:8)])
|
496 |
+
|
497 |
+
show_col(cols)
|
498 |
+
|
499 |
+
plot_1 <- ggplot(effs_plot,
|
500 |
+
aes(y = as.factor(model),
|
501 |
+
x = mean,
|
502 |
+
group = effect,
|
503 |
+
colour = effect)) +
|
504 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
505 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
506 |
+
scale_color_manual(values=cols) +
|
507 |
+
ylab("Model of boundness") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
508 |
+
theme(axis.text=element_text(size=50),
|
509 |
+
legend.text=element_text(size=50),
|
510 |
+
axis.title=element_text(size=50),
|
511 |
+
legend.title=element_text(size=50),
|
512 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
513 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
514 |
+
|
515 |
+
|
516 |
+
#plot_1
|
517 |
+
ggsave(filename = 'output/SP_models_plot_Boundness_social_models.jpg',
|
518 |
+
plot_1, height = 20, width = 45)
|
519 |
+
|
520 |
+
|
521 |
+
#saving hyperparameters: Gaussian observations
|
522 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
523 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
524 |
+
}
|
525 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
526 |
+
|
527 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Boundness_social_models_marginals_hyperpar_gaussian.csv")
|
528 |
+
|
529 |
+
#saving hyperparameters: phylogenetic
|
530 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
531 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
532 |
+
}
|
533 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
534 |
+
|
535 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables/Boundness_social_models_marginals_hyperpar_phylogenetic.csv")
|
536 |
+
|
537 |
+
#saving hyperparameters: spatial
|
538 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
539 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
540 |
+
}
|
541 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
542 |
+
|
543 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables/Boundness_social_models_marginals_hyperpar_spatial.csv")
|
544 |
+
|
545 |
+
|
546 |
+
#saving summaries of random effects: phylogenetic
|
547 |
+
for (i in 1:length(summary_random_list_phy)) {
|
548 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
549 |
+
}
|
550 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
551 |
+
|
552 |
+
write.csv(summary_random_list_phy, "output_tables/Boundness_social_models_summary_random_phy.csv")
|
553 |
+
|
554 |
+
#saving summaries of random effects: spatial
|
555 |
+
for (i in 1:length(summary_random_list_spa)) {
|
556 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
557 |
+
}
|
558 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
559 |
+
|
560 |
+
write.csv(summary_random_list_spa, "output_tables/Boundness_social_models_summary_random_spa.csv")
|
101/replication_package/models_Boundness_social_only.R
ADDED
@@ -0,0 +1,452 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Boundness predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("L1_log_st"),
|
63 |
+
|
64 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("L2_prop"),
|
67 |
+
|
68 |
+
c("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("L1_log10:L2_prop"),
|
75 |
+
|
76 |
+
c("neighboring_languages_st"),
|
77 |
+
|
78 |
+
c("Official"),
|
79 |
+
|
80 |
+
c("Education"))
|
81 |
+
|
82 |
+
|
83 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
84 |
+
|
85 |
+
predterms <- t(as.data.frame(predterms))
|
86 |
+
|
87 |
+
predterms_short <- predterms
|
88 |
+
|
89 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
92 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
93 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
94 |
+
|
95 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
96 |
+
number = 1:length(predterms_short))
|
97 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
98 |
+
|
99 |
+
|
100 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
101 |
+
number = 1:length(predterms_short))
|
102 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
103 |
+
|
104 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
105 |
+
number = 1:length(predterms_short))
|
106 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
107 |
+
L2_prop_element <- L2_prop_element[-length(L2_prop_element)] #making sure that the interaction term (introduced below) is not treated as belonging to this isolated element
|
108 |
+
|
109 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
110 |
+
number = 1:length(predterms_short))
|
111 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
112 |
+
|
113 |
+
#can use only part of the interaction term within grepl() function
|
114 |
+
interaction_element <- data.frame("judgement" = grepl(":L2 proportion", predterms_short),
|
115 |
+
number = 1:length(predterms_short))
|
116 |
+
interaction_element <- interaction_element[interaction_element$judgement == TRUE,]$number
|
117 |
+
|
118 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
119 |
+
number = 1:length(predterms_short))
|
120 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
121 |
+
|
122 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
123 |
+
number = 1:length(predterms_short))
|
124 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
125 |
+
|
126 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
127 |
+
number = 1:length(predterms_short))
|
128 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
133 |
+
intercept_matrix <- matrix(NA, 10, 5)
|
134 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
135 |
+
|
136 |
+
social_effects_matrix_L1 <- matrix(NA, 10, 5)
|
137 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
138 |
+
social_effects_matrix_L1_nl <- matrix(NA, 10, 5)
|
139 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
140 |
+
social_effects_matrix_L2_prop <- matrix(NA, 10, 5)
|
141 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
142 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, 10, 5)
|
143 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
144 |
+
social_effects_matrix_N <- matrix(NA, 10, 5)
|
145 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
146 |
+
social_effects_matrix_O <- matrix(NA, 10, 5)
|
147 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
148 |
+
social_effects_matrix_E <- matrix(NA, 10, 5)
|
149 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
150 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, 10, 5)
|
151 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
152 |
+
|
153 |
+
#fitted values
|
154 |
+
fitted_list <- vector("list", 10)
|
155 |
+
names(fitted_list) <- predterms_short
|
156 |
+
|
157 |
+
#marginals of hyperparameters
|
158 |
+
marginals_hyperpar_list_gaussian <- vector("list", 10)
|
159 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
160 |
+
|
161 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", 10)
|
162 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
163 |
+
|
164 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", 10)
|
165 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
166 |
+
|
167 |
+
|
168 |
+
#marginals of fixed effects
|
169 |
+
marginals_fixed_list_Intercept <- vector("list", 10)
|
170 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
171 |
+
|
172 |
+
marginals_fixed_list_L1 <- vector("list", 10)
|
173 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
174 |
+
|
175 |
+
marginals_fixed_list_L2_prop <- vector("list", 10)
|
176 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
177 |
+
|
178 |
+
marginals_fixed_list_O <- vector("list", 10)
|
179 |
+
names(marginals_fixed_list_O) <- predterms_short
|
180 |
+
|
181 |
+
marginals_fixed_list_N <- vector("list", 10)
|
182 |
+
names(marginals_fixed_list_N) <- predterms_short
|
183 |
+
|
184 |
+
marginals_fixed_list_E <- vector("list", 10)
|
185 |
+
names(marginals_fixed_list_E) <- predterms_short
|
186 |
+
|
187 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", 10)
|
188 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
189 |
+
|
190 |
+
|
191 |
+
#summary statistics of random effects
|
192 |
+
summary_random_list_social_L1_nl <- vector("list", 10)
|
193 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
194 |
+
|
195 |
+
summary_random_list_social_L2_prop_nl <- vector("list", 10)
|
196 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
197 |
+
|
198 |
+
|
199 |
+
coefm <- matrix(NA,10,1)
|
200 |
+
result <- vector("list",10)
|
201 |
+
|
202 |
+
for(i in 1:10){
|
203 |
+
formula <- as.formula(paste("boundness_st ~ ",predterms[[i]]))
|
204 |
+
result[[i]] <- inla(formula, family="gaussian",
|
205 |
+
control.family = list(hyper = pcprior_hyper),
|
206 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
207 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
208 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
209 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
210 |
+
|
211 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
212 |
+
|
213 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
214 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
215 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
216 |
+
|
217 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
218 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
219 |
+
|
220 |
+
if(i %in% L1_nl_element){
|
221 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
222 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
223 |
+
method = "linear") %>%
|
224 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
225 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
226 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
227 |
+
}
|
228 |
+
|
229 |
+
if(i %in% L2_prop_nl_element){
|
230 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
231 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
232 |
+
method = "linear") %>%
|
233 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
234 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
235 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
236 |
+
}
|
237 |
+
|
238 |
+
if(i %in% L1_element) {
|
239 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
240 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
241 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
242 |
+
|
243 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
244 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
245 |
+
}
|
246 |
+
|
247 |
+
if(i %in% L2_prop_element) {
|
248 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
249 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
250 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
251 |
+
|
252 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
253 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
254 |
+
}
|
255 |
+
|
256 |
+
if(i %in% interaction_element) {
|
257 |
+
social_effects_matrix_L1_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.975quant`)
|
258 |
+
social_effects_matrix_L1_L2_prop[i, 4] <- predterms_short[[i]]
|
259 |
+
social_effects_matrix_L1_L2_prop[i, 5] <- result[[i]]$waic$waic
|
260 |
+
|
261 |
+
marginals_fixed_list_L1_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log10:L2_prop"]]))
|
262 |
+
colnames(marginals_fixed_list_L1_L2_prop[[i]]) <- c("x for L1*L2 proportion", "y for L1*L2 proportion")
|
263 |
+
}
|
264 |
+
|
265 |
+
if(i %in% neighbour_element) {
|
266 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
267 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
268 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
269 |
+
|
270 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
271 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
272 |
+
}
|
273 |
+
|
274 |
+
if(i %in% official_element) {
|
275 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
276 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
277 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
278 |
+
|
279 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
280 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
281 |
+
}
|
282 |
+
|
283 |
+
if(i %in% education_element) {
|
284 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
285 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
286 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
287 |
+
|
288 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
289 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
290 |
+
}
|
291 |
+
|
292 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
293 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
294 |
+
mutate(across(where(is.numeric), round, 2))
|
295 |
+
|
296 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
297 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
298 |
+
|
299 |
+
if(i %in% L1_nl_element){
|
300 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
301 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
302 |
+
}
|
303 |
+
|
304 |
+
if(i %in% L2_prop_nl_element){
|
305 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
306 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
307 |
+
}
|
308 |
+
}
|
309 |
+
|
310 |
+
#beepr::beep(5)
|
311 |
+
|
312 |
+
save(result, file = "output_models/models_Boundness_social_only.RData")
|
313 |
+
#load("output_models/models_Boundness_social_only.RData")
|
314 |
+
|
315 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
316 |
+
colnames(coefm) <- c("model", "WAIC")
|
317 |
+
coefm <- coefm %>%
|
318 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
319 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
320 |
+
arrange(WAIC)
|
321 |
+
|
322 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
323 |
+
coefm <- coefm[order(coefm$WAIC),]
|
324 |
+
|
325 |
+
coefm_path <- paste("output_tables/", "waics", "Boundness_social_only_models", ".csv", collapse = "")
|
326 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
327 |
+
|
328 |
+
for (i in 1:length(fitted_list)) {
|
329 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
330 |
+
}
|
331 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
332 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Boundness_social_only_models", ".csv", collapse = "")
|
333 |
+
write.csv(fitted_list, fitted_list_path)
|
334 |
+
|
335 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
336 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
337 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
338 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
339 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
340 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
341 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
342 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
343 |
+
interaction_effects <- as.data.frame(social_effects_matrix_L1_L2_prop)
|
344 |
+
|
345 |
+
intercept_effects$effect <- "Intercept"
|
346 |
+
L1_effects$effect <- "L1"
|
347 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
348 |
+
L2_prop_effects$effect <- "L2 proportion"
|
349 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
350 |
+
N_effects$effect <- "Neighbours"
|
351 |
+
E_effects$effect <- "Education"
|
352 |
+
O_effects$effect <- "Official status"
|
353 |
+
interaction_effects$effect <- "L1*L2 proportion"
|
354 |
+
|
355 |
+
effs <- as.data.frame(rbind(intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects, interaction_effects))
|
356 |
+
effs <- effs %>%
|
357 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
358 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
359 |
+
na.omit() %>%
|
360 |
+
arrange(WAIC) %>%
|
361 |
+
relocate(model)
|
362 |
+
|
363 |
+
effs_path <- paste("output_tables/", "effects", "Boundness_social_only_models", ".csv", collapse = "")
|
364 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
365 |
+
|
366 |
+
effs <- read.csv("output_tables/ effects Boundness_social_only_models .csv")
|
367 |
+
|
368 |
+
effs_table_SM <- effs %>%
|
369 |
+
rename("2.5%"=2,
|
370 |
+
"50%" = 4,
|
371 |
+
"97.5%" = 3) %>%
|
372 |
+
flextable() %>%
|
373 |
+
autofit() %>%
|
374 |
+
merge_v(j=c("model", "WAIC")) %>%
|
375 |
+
fix_border_issues() %>%
|
376 |
+
border_inner_h()
|
377 |
+
|
378 |
+
save_as_docx(
|
379 |
+
"Effects in boundness models with fixed and random effects (including non-linear implementations of some fixed effects)" = effs_table_SM,
|
380 |
+
path = "output_tables/table_SM_effects_Boundness_social_only_models.docx")
|
381 |
+
|
382 |
+
effs_table_Main <- effs %>%
|
383 |
+
rename("2.5%"=2,
|
384 |
+
"50%" = 4,
|
385 |
+
"97.5%" = 3) %>%
|
386 |
+
filter(!grepl("nonlinear", model))
|
387 |
+
|
388 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
389 |
+
|
390 |
+
effs_table_Main <- effs_table_Main %>%
|
391 |
+
flextable() %>%
|
392 |
+
autofit() %>%
|
393 |
+
merge_v(j=c("model", "WAIC")) %>%
|
394 |
+
fix_border_issues() %>%
|
395 |
+
border_inner_h()
|
396 |
+
|
397 |
+
save_as_docx(
|
398 |
+
"Effects in boundness models with fixed and random effects" = effs_table_Main,
|
399 |
+
path = "output_tables/table_Main_effects_Boundness_social_only_models.docx")
|
400 |
+
|
401 |
+
|
402 |
+
effs_plot <- effs %>%
|
403 |
+
#filter(WAIC <= top_9) %>%
|
404 |
+
rename(lower=2,
|
405 |
+
upper = 4,
|
406 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
407 |
+
#filter(!effect == "Intercept") %>%
|
408 |
+
mutate(effect = factor(effect, levels=c("Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status", "L1*L2 proportion"))) %>%
|
409 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
410 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
411 |
+
group_by(WAIC) %>%
|
412 |
+
arrange(WAIC) %>%
|
413 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
414 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
415 |
+
|
416 |
+
|
417 |
+
#plot modified from function ggregplot::Efxplot
|
418 |
+
cols = c(brewer.pal(12, "Paired"))
|
419 |
+
cols = c("gray50", cols[c(1:8)])
|
420 |
+
|
421 |
+
show_col(cols)
|
422 |
+
|
423 |
+
plot_1 <- ggplot(effs_plot,
|
424 |
+
aes(y = as.factor(model),
|
425 |
+
x = mean,
|
426 |
+
group = effect,
|
427 |
+
colour = effect)) +
|
428 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
429 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
430 |
+
scale_color_manual(values=cols) +
|
431 |
+
ylab("Model of boundness") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
432 |
+
theme(axis.text=element_text(size=50),
|
433 |
+
legend.text=element_text(size=50),
|
434 |
+
axis.title=element_text(size=50),
|
435 |
+
legend.title=element_text(size=50),
|
436 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
437 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
438 |
+
|
439 |
+
|
440 |
+
|
441 |
+
#plot_1
|
442 |
+
ggsave(filename = 'output/SP_models_plot_Boundness_social_only_models.jpg',
|
443 |
+
plot_1, height = 20, width = 45)
|
444 |
+
|
445 |
+
|
446 |
+
#saving hyperparameters: Gaussian observations
|
447 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
448 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
449 |
+
}
|
450 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
451 |
+
|
452 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Boundness_social_only_models_marginals_hyperpar_gaussian.csv")
|
101/replication_package/models_Informativity_phylogenetic_spatial.R
ADDED
@@ -0,0 +1,414 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Informativity predicted by combinations of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
#"regional" set of parameters
|
54 |
+
spatial_covar_mat_2 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")], cov.pars = phi_2, kappa = kappa)$varcov
|
55 |
+
typical_variance_spatial_2 = exp(mean(log(diag(spatial_covar_mat_2))))
|
56 |
+
spatial_cov_std_2 = spatial_covar_mat_2 / typical_variance_spatial_2
|
57 |
+
spatial_prec_mat_2 = solve(spatial_cov_std_2)
|
58 |
+
dimnames(spatial_prec_mat_2) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
59 |
+
|
60 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
61 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
62 |
+
metrics_joined$phy_id = phy_id
|
63 |
+
|
64 |
+
## Other effects are in the same order they appear in the dataset
|
65 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
66 |
+
|
67 |
+
#Preparing the formulas for 7 competing models to be used in inla() call
|
68 |
+
listcombo <- list(#phylogenetic and spatial effects in isolation
|
69 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)"),
|
70 |
+
c("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)"),
|
71 |
+
c("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)"),
|
72 |
+
c("f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)"),
|
73 |
+
#phylogenetic and distinct spatial effects in combination
|
74 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
75 |
+
"f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)"),
|
76 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
77 |
+
"f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)"),
|
78 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)",
|
79 |
+
"f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)"))
|
80 |
+
|
81 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
82 |
+
|
83 |
+
predterms <- t(as.data.frame(predterms))
|
84 |
+
|
85 |
+
predterms_short <- predterms
|
86 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
87 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
88 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_2, constr = TRUE, hyper = pcprior_hyper)", "Spatial: regional", predterms_short, fixed=TRUE)
|
89 |
+
predterms_short <- gsub("f(AUTOTYP_area, model = 'iid', hyper = pcprior_hyper)", "Areal", predterms_short, fixed=TRUE)
|
90 |
+
|
91 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
92 |
+
number = 1:length(predterms_short))
|
93 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
94 |
+
|
95 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
96 |
+
number = 1:length(predterms_short))
|
97 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
98 |
+
|
99 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
100 |
+
number = 1:length(predterms_short))
|
101 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
102 |
+
|
103 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
104 |
+
|
105 |
+
areal_element <- data.frame("judgement" = grepl("Areal", predterms_short),
|
106 |
+
number = 1:length(predterms_short))
|
107 |
+
areal_element <- areal_element[areal_element$judgement == TRUE,]$number
|
108 |
+
|
109 |
+
|
110 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
111 |
+
phy_effects_matrix <- matrix(NA, 7, 5)
|
112 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
113 |
+
spa_effects_matrix <- matrix(NA, 7, 5)
|
114 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
115 |
+
area_effects_matrix <- matrix(NA, 7, 5)
|
116 |
+
colnames(area_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
117 |
+
|
118 |
+
intercept_matrix <- matrix(NA, 7, 5)
|
119 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
120 |
+
|
121 |
+
#fitted values
|
122 |
+
fitted_list <- vector("list", 7)
|
123 |
+
names(fitted_list) <- predterms_short
|
124 |
+
|
125 |
+
#marginals of hyperparameters
|
126 |
+
marginals_hyperpar_list_gaussian <- vector("list", 7)
|
127 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
128 |
+
|
129 |
+
marginals_hyperpar_list_phy <- vector("list", 7)
|
130 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
131 |
+
|
132 |
+
marginals_hyperpar_list_spa <- vector("list", 7)
|
133 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
134 |
+
|
135 |
+
marginals_hyperpar_list_area <- vector("list", 7)
|
136 |
+
names(marginals_hyperpar_list_area) <- predterms_short
|
137 |
+
|
138 |
+
|
139 |
+
#marginals of fixed effects
|
140 |
+
marginals_fixed_list_Intercept <- vector("list", 7)
|
141 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
142 |
+
|
143 |
+
|
144 |
+
#summary statistics of random effects
|
145 |
+
summary_random_list_phy <- vector("list", 7)
|
146 |
+
names(summary_random_list_phy) <- predterms_short
|
147 |
+
|
148 |
+
summary_random_list_spa <- vector("list", 7)
|
149 |
+
names(summary_random_list_spa) <- predterms_short
|
150 |
+
|
151 |
+
summary_random_list_area <- vector("list", 7)
|
152 |
+
names(summary_random_list_area) <- predterms_short
|
153 |
+
|
154 |
+
coefm <- matrix(NA,7,1)
|
155 |
+
result <- vector("list",7)
|
156 |
+
|
157 |
+
for(i in 1:7){
|
158 |
+
formula <- as.formula(paste("informativity_st ~ ",predterms[[i]]))
|
159 |
+
result[[i]] <- inla(formula, family="gaussian",
|
160 |
+
control.family = list(hyper = pcprior_hyper),
|
161 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
162 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
163 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
164 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
165 |
+
|
166 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
167 |
+
|
168 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
169 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
170 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
171 |
+
|
172 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
173 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
174 |
+
|
175 |
+
if(i %in% phylogenetic_element) {
|
176 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
177 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
178 |
+
method = "linear") %>%
|
179 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
180 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
181 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
182 |
+
}
|
183 |
+
|
184 |
+
if(i %in% spatial_element) {
|
185 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
186 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
187 |
+
method = "linear") %>%
|
188 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
189 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
190 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
191 |
+
}
|
192 |
+
|
193 |
+
if(i %in% areal_element){
|
194 |
+
area_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
195 |
+
result[[i]]$marginals.hyperpar$`Precision for AUTOTYP_area`,
|
196 |
+
method = "linear") %>%
|
197 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
198 |
+
area_effects_matrix[i, 4] <- predterms_short[[i]]
|
199 |
+
area_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
200 |
+
}
|
201 |
+
|
202 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
203 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
204 |
+
mutate(across(where(is.numeric), round, 2))
|
205 |
+
|
206 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
207 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
208 |
+
|
209 |
+
if(i %in% phylogenetic_element){
|
210 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
211 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
212 |
+
}
|
213 |
+
|
214 |
+
if(i %in% spatial_element){
|
215 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
216 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
217 |
+
}
|
218 |
+
|
219 |
+
if(i %in% areal_element){
|
220 |
+
marginals_hyperpar_list_area[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for AUTOTYP_area"]]))
|
221 |
+
colnames(marginals_hyperpar_list_area[[i]]) <- c("x for AUTOTYP_area", "y for AUTOTYP_area")
|
222 |
+
}
|
223 |
+
|
224 |
+
if(i %in% phylogenetic_element){
|
225 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
226 |
+
rename(phy_id = ID) %>%
|
227 |
+
as.data.frame() %>%
|
228 |
+
mutate(across(where(is.numeric), round, 2))
|
229 |
+
}
|
230 |
+
|
231 |
+
if(i %in% spatial_element){
|
232 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
233 |
+
rename(sp_id = ID) %>%
|
234 |
+
as.data.frame() %>%
|
235 |
+
mutate(across(where(is.numeric), round, 2))
|
236 |
+
}
|
237 |
+
|
238 |
+
if(i %in% areal_element){
|
239 |
+
summary_random_list_area[[i]] <- cbind(result[[i]]$summary.random$AUTOTYP_area) %>%
|
240 |
+
rename(AUTOTYP_area = ID) %>%
|
241 |
+
as.data.frame() %>%
|
242 |
+
mutate(across(where(is.numeric), round, 2))
|
243 |
+
}
|
244 |
+
}
|
245 |
+
|
246 |
+
#beepr::beep(5)
|
247 |
+
|
248 |
+
save(result, file = "output_models/models_Informativity_phylogenetic_spatial.RData")
|
249 |
+
|
250 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
251 |
+
colnames(coefm) <- c("model", "WAIC")
|
252 |
+
coefm <- coefm %>%
|
253 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
254 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
255 |
+
arrange(WAIC)
|
256 |
+
|
257 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
258 |
+
coefm <- coefm[order(coefm$WAIC),]
|
259 |
+
|
260 |
+
coefm_path <- paste("output_tables/", "waics", "Informativity_phylogenetic_spatial_models", ".csv", collapse = "")
|
261 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
262 |
+
|
263 |
+
for (i in 1:length(fitted_list)) {
|
264 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
265 |
+
}
|
266 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
267 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Informativity_phylogenetic_spatial_models", ".csv", collapse = "")
|
268 |
+
write.csv(fitted_list, fitted_list_path)
|
269 |
+
|
270 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
271 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
272 |
+
area_effects <- as.data.frame(area_effects_matrix)
|
273 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
274 |
+
|
275 |
+
phy_effects$effect <- "phylogenetic SD"
|
276 |
+
spa_effects$effect <- "spatial SD"
|
277 |
+
area_effects$effect <- "areal SD"
|
278 |
+
intercept_effects$effect <- "Intercept"
|
279 |
+
|
280 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, area_effects, intercept_effects))
|
281 |
+
effs <- effs %>%
|
282 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
283 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
284 |
+
na.omit() %>%
|
285 |
+
arrange(WAIC) %>%
|
286 |
+
relocate(model)
|
287 |
+
|
288 |
+
effs_path <- paste("output_tables/", "effects", "Informativity_phylogenetic_spatial_models", ".csv", collapse = "")
|
289 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
290 |
+
|
291 |
+
effs <- read.csv("output_tables/ effects Informativity_phylogenetic_spatial_models .csv")
|
292 |
+
|
293 |
+
effs_table_SM <- effs %>%
|
294 |
+
mutate(effect =
|
295 |
+
dplyr::recode(effect,
|
296 |
+
"areal SD" = "spatial SD")) %>%
|
297 |
+
rename("2.5%"=2,
|
298 |
+
"50%" = 4,
|
299 |
+
"97.5%" = 3) %>%
|
300 |
+
flextable() %>%
|
301 |
+
autofit() %>%
|
302 |
+
merge_v(j=c("model", "WAIC")) %>%
|
303 |
+
fix_border_issues() %>%
|
304 |
+
border_inner_h()
|
305 |
+
|
306 |
+
save_as_docx(
|
307 |
+
"Effects in informativity models with random effects" = effs_table_SM,
|
308 |
+
path = "output_tables/table_SM_effects_Informativity_phylogenetic_spatial_models.docx")
|
309 |
+
|
310 |
+
|
311 |
+
effs_plot <- effs %>%
|
312 |
+
#filter(WAIC <= top_9) %>%
|
313 |
+
rename(lower=2,
|
314 |
+
upper = 4,
|
315 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
316 |
+
#filter(!effect == "Intercept") %>%
|
317 |
+
mutate(effect =
|
318 |
+
dplyr::recode(effect,
|
319 |
+
"areal SD" = "spatial SD")) %>%
|
320 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "areal SD", "Intercept"))) %>%
|
321 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
322 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
323 |
+
group_by(WAIC) %>%
|
324 |
+
arrange(WAIC) %>%
|
325 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
326 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
327 |
+
|
328 |
+
|
329 |
+
|
330 |
+
#plot modified from function ggregplot::Efxplot
|
331 |
+
cols = c(brewer.pal(12, "Paired"))
|
332 |
+
cols = c(cols[c(12, 10)], "gray50")
|
333 |
+
|
334 |
+
show_col(cols)
|
335 |
+
|
336 |
+
plot_1 <- ggplot(effs_plot,
|
337 |
+
aes(y = as.factor(model),
|
338 |
+
x = mean,
|
339 |
+
group = effect,
|
340 |
+
colour = effect)) +
|
341 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
342 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
343 |
+
scale_color_manual(values=cols) +
|
344 |
+
ylab("Model of informativity") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
345 |
+
theme(axis.text=element_text(size=50),
|
346 |
+
legend.text=element_text(size=50),
|
347 |
+
axis.title=element_text(size=50),
|
348 |
+
legend.title=element_text(size=50),
|
349 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
350 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
351 |
+
|
352 |
+
|
353 |
+
|
354 |
+
#plot_1
|
355 |
+
ggsave(filename = 'output/SP_models_plot_Informativity_phylogenetic_spatial_models.jpg',
|
356 |
+
plot_1, height = 29, width = 33)
|
357 |
+
|
358 |
+
|
359 |
+
#saving hyperparameters: Gaussian observations
|
360 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
361 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
362 |
+
}
|
363 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
364 |
+
|
365 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Informativity_phylogenetic_spatial_models_marginals_hyperpar_gaussian.csv")
|
366 |
+
|
367 |
+
#saving hyperparameters: phylogenetic
|
368 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
369 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
370 |
+
}
|
371 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
372 |
+
|
373 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables/Informativity_phylogenetic_spatial_models_marginals_hyperpar_phylogenetic.csv")
|
374 |
+
|
375 |
+
#saving hyperparameters: spatial
|
376 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
377 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
378 |
+
}
|
379 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
380 |
+
|
381 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables/Informativity_phylogenetic_spatial_models_marginals_hyperpar_spatial.csv")
|
382 |
+
|
383 |
+
#saving hyperparameters: areas
|
384 |
+
for (i in 1:length(marginals_hyperpar_list_area)) {
|
385 |
+
marginals_hyperpar_list_area[[i]]$model <- names(marginals_hyperpar_list_area)[i]
|
386 |
+
}
|
387 |
+
marginals_hyperpar_list_area <- dplyr::bind_rows(marginals_hyperpar_list_area)
|
388 |
+
|
389 |
+
write.csv(marginals_hyperpar_list_area, "output_tables/Informativity_phylogenetic_spatial_models_marginals_hyperpar_areal.csv")
|
390 |
+
|
391 |
+
|
392 |
+
#saving summaries of random effects: phylogenetic
|
393 |
+
for (i in 1:length(summary_random_list_phy)) {
|
394 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
395 |
+
}
|
396 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
397 |
+
|
398 |
+
write.csv(summary_random_list_phy, "output_tables/Informativity_phylogenetic_spatial_models_summary_random_phy.csv")
|
399 |
+
|
400 |
+
#saving summaries of random effects: spatial
|
401 |
+
for (i in 1:length(summary_random_list_spa)) {
|
402 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
403 |
+
}
|
404 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
405 |
+
|
406 |
+
write.csv(summary_random_list_spa, "output_tables/Informativity_phylogenetic_spatial_models_summary_random_spa.csv")
|
407 |
+
|
408 |
+
#saving summaries of random effects: areas
|
409 |
+
for (i in 1:length(summary_random_list_area)) {
|
410 |
+
summary_random_list_area[[i]]$model <- names(summary_random_list_area)[i]
|
411 |
+
}
|
412 |
+
summary_random_list_area <- dplyr::bind_rows(summary_random_list_area)
|
413 |
+
|
414 |
+
write.csv(summary_random_list_area, "output_tables/Informativity_phylogenetic_spatial_models_summary_random_areas.csv")
|
101/replication_package/models_Informativity_reduced_social.R
ADDED
@@ -0,0 +1,537 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Informativity predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st"),
|
63 |
+
|
64 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L2_prop"),
|
67 |
+
|
68 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "neighboring_languages_st"),
|
75 |
+
|
76 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Official"),
|
77 |
+
|
78 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Education"))
|
79 |
+
|
80 |
+
|
81 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
82 |
+
|
83 |
+
predterms <- t(as.data.frame(predterms))
|
84 |
+
|
85 |
+
predterms_short <- predterms
|
86 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
87 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
88 |
+
|
89 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
92 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
93 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
94 |
+
|
95 |
+
|
96 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
97 |
+
number = 1:length(predterms_short))
|
98 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
99 |
+
|
100 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
101 |
+
number = 1:length(predterms_short))
|
102 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
103 |
+
|
104 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
105 |
+
number = 1:length(predterms_short))
|
106 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
107 |
+
|
108 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
109 |
+
|
110 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
111 |
+
number = 1:length(predterms_short))
|
112 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
113 |
+
|
114 |
+
|
115 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
116 |
+
number = 1:length(predterms_short))
|
117 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
118 |
+
|
119 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
120 |
+
number = 1:length(predterms_short))
|
121 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
122 |
+
|
123 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
124 |
+
number = 1:length(predterms_short))
|
125 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
126 |
+
|
127 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
128 |
+
number = 1:length(predterms_short))
|
129 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
130 |
+
|
131 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
132 |
+
number = 1:length(predterms_short))
|
133 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
134 |
+
|
135 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
136 |
+
number = 1:length(predterms_short))
|
137 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
138 |
+
|
139 |
+
models_number <- length(predterms_short)
|
140 |
+
|
141 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
142 |
+
phy_effects_matrix <- matrix(NA, models_number, 5)
|
143 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
144 |
+
spa_effects_matrix <- matrix(NA, models_number, 5)
|
145 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
146 |
+
|
147 |
+
intercept_matrix <- matrix(NA, models_number, 5)
|
148 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
149 |
+
|
150 |
+
social_effects_matrix_L1 <- matrix(NA, models_number, 5)
|
151 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
152 |
+
social_effects_matrix_L1_nl <- matrix(NA, models_number, 5)
|
153 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
154 |
+
social_effects_matrix_L2_prop <- matrix(NA, models_number, 5)
|
155 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
156 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, models_number, 5)
|
157 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
158 |
+
social_effects_matrix_N <- matrix(NA, models_number, 5)
|
159 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
160 |
+
social_effects_matrix_O <- matrix(NA, models_number, 5)
|
161 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
162 |
+
social_effects_matrix_E <- matrix(NA, models_number, 5)
|
163 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
164 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, models_number, 5)
|
165 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
166 |
+
|
167 |
+
#fitted values
|
168 |
+
fitted_list <- vector("list", models_number)
|
169 |
+
names(fitted_list) <- predterms_short
|
170 |
+
|
171 |
+
#marginals of hyperparameters
|
172 |
+
marginals_hyperpar_list_gaussian <- vector("list", models_number)
|
173 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
174 |
+
|
175 |
+
marginals_hyperpar_list_phy <- vector("list", models_number)
|
176 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
177 |
+
|
178 |
+
marginals_hyperpar_list_spa <- vector("list", models_number)
|
179 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
180 |
+
|
181 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", models_number)
|
182 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
183 |
+
|
184 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", models_number)
|
185 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
186 |
+
|
187 |
+
|
188 |
+
#marginals of fixed effects
|
189 |
+
marginals_fixed_list_Intercept <- vector("list", models_number)
|
190 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
191 |
+
|
192 |
+
marginals_fixed_list_L1 <- vector("list", models_number)
|
193 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
194 |
+
|
195 |
+
marginals_fixed_list_L2_prop <- vector("list", models_number)
|
196 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
197 |
+
|
198 |
+
marginals_fixed_list_O <- vector("list", models_number)
|
199 |
+
names(marginals_fixed_list_O) <- predterms_short
|
200 |
+
|
201 |
+
marginals_fixed_list_N <- vector("list", models_number)
|
202 |
+
names(marginals_fixed_list_N) <- predterms_short
|
203 |
+
|
204 |
+
marginals_fixed_list_E <- vector("list", models_number)
|
205 |
+
names(marginals_fixed_list_E) <- predterms_short
|
206 |
+
|
207 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", models_number)
|
208 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
#summary statistics of random effects
|
214 |
+
summary_random_list_phy <- vector("list", models_number)
|
215 |
+
names(summary_random_list_phy) <- predterms_short
|
216 |
+
|
217 |
+
summary_random_list_spa <- vector("list", models_number)
|
218 |
+
names(summary_random_list_spa) <- predterms_short
|
219 |
+
|
220 |
+
summary_random_list_social_L1_nl <- vector("list", models_number)
|
221 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
222 |
+
|
223 |
+
summary_random_list_social_L2_prop_nl <- vector("list", models_number)
|
224 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
225 |
+
|
226 |
+
|
227 |
+
coefm <- matrix(NA,models_number,1)
|
228 |
+
result <- vector("list",models_number)
|
229 |
+
|
230 |
+
for(i in 1:models_number){
|
231 |
+
formula <- as.formula(paste("informativity_st ~ ",predterms[[i]]))
|
232 |
+
result[[i]] <- inla(formula, family="gaussian",
|
233 |
+
control.family = list(hyper = pcprior_hyper),
|
234 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
235 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
236 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
237 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
238 |
+
|
239 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
240 |
+
|
241 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
242 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
243 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
244 |
+
|
245 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
246 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
247 |
+
|
248 |
+
if(i %in% phylogenetic_element) {
|
249 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
250 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
251 |
+
method = "linear") %>%
|
252 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
253 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
254 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
255 |
+
}
|
256 |
+
|
257 |
+
if(i %in% spatial_element) {
|
258 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
259 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
260 |
+
method = "linear") %>%
|
261 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
262 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
263 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
264 |
+
}
|
265 |
+
|
266 |
+
|
267 |
+
if(i %in% L1_nl_element){
|
268 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
269 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
270 |
+
method = "linear") %>%
|
271 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
272 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
273 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
274 |
+
}
|
275 |
+
|
276 |
+
if(i %in% L2_prop_nl_element){
|
277 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
278 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
279 |
+
method = "linear") %>%
|
280 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
281 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
282 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
283 |
+
}
|
284 |
+
|
285 |
+
if(i %in% L1_element) {
|
286 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
287 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
288 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
289 |
+
|
290 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
291 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
292 |
+
}
|
293 |
+
|
294 |
+
if(i %in% L2_prop_element) {
|
295 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
296 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
297 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
298 |
+
|
299 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
300 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
301 |
+
}
|
302 |
+
|
303 |
+
if(i %in% neighbour_element) {
|
304 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
305 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
306 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
307 |
+
|
308 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
309 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
310 |
+
}
|
311 |
+
|
312 |
+
if(i %in% official_element) {
|
313 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
314 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
315 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
316 |
+
|
317 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
318 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
319 |
+
}
|
320 |
+
|
321 |
+
if(i %in% education_element) {
|
322 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
323 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
324 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
325 |
+
|
326 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
327 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
328 |
+
}
|
329 |
+
|
330 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
331 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
332 |
+
mutate(across(where(is.numeric), round, 2))
|
333 |
+
|
334 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
335 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
336 |
+
|
337 |
+
if(i %in% phylogenetic_element){
|
338 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
339 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
340 |
+
}
|
341 |
+
|
342 |
+
if(i %in% spatial_element){
|
343 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
344 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
345 |
+
}
|
346 |
+
|
347 |
+
if(i %in% L1_nl_element){
|
348 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
349 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
350 |
+
}
|
351 |
+
|
352 |
+
if(i %in% L2_prop_nl_element){
|
353 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
354 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
355 |
+
}
|
356 |
+
|
357 |
+
if(i %in% phylogenetic_element){
|
358 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
359 |
+
rename(phy_id = ID) %>%
|
360 |
+
as.data.frame() %>%
|
361 |
+
mutate(across(where(is.numeric), round, 2))
|
362 |
+
}
|
363 |
+
|
364 |
+
if(i %in% spatial_element){
|
365 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
366 |
+
rename(sp_id = ID) %>%
|
367 |
+
as.data.frame() %>%
|
368 |
+
mutate(across(where(is.numeric), round, 2))
|
369 |
+
}
|
370 |
+
}
|
371 |
+
|
372 |
+
#beepr::beep(5)
|
373 |
+
|
374 |
+
save(result, file = "output_models_reduced/models_Informativity_social.RData")
|
375 |
+
|
376 |
+
|
377 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
378 |
+
colnames(coefm) <- c("model", "WAIC")
|
379 |
+
coefm <- coefm %>%
|
380 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
381 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
382 |
+
arrange(WAIC)
|
383 |
+
|
384 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
385 |
+
coefm <- coefm[order(coefm$WAIC),]
|
386 |
+
|
387 |
+
coefm_path <- paste("output_tables_reduced/", "waics", "Informativity_social_models", ".csv", collapse = "")
|
388 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
389 |
+
|
390 |
+
for (i in 1:length(fitted_list)) {
|
391 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
392 |
+
}
|
393 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
394 |
+
fitted_list_path <- paste("output_tables_reduced/", "fitted_list", "Informativity_social_models", ".csv", collapse = "")
|
395 |
+
write.csv(fitted_list, fitted_list_path)
|
396 |
+
|
397 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
398 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
399 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
400 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
401 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
402 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
403 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
404 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
405 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
406 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
407 |
+
|
408 |
+
phy_effects$effect <- "phylogenetic SD"
|
409 |
+
spa_effects$effect <- "spatial SD"
|
410 |
+
intercept_effects$effect <- "Intercept"
|
411 |
+
L1_effects$effect <- "L1"
|
412 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
413 |
+
L2_prop_effects$effect <- "L2 proportion"
|
414 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
415 |
+
N_effects$effect <- "Neighbours"
|
416 |
+
E_effects$effect <- "Education"
|
417 |
+
O_effects$effect <- "Official status"
|
418 |
+
|
419 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects))
|
420 |
+
effs <- effs %>%
|
421 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
422 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
423 |
+
na.omit() %>%
|
424 |
+
arrange(WAIC) %>%
|
425 |
+
relocate(model)
|
426 |
+
|
427 |
+
effs_path <- paste("output_tables_reduced/", "effects", "Informativity_social_models", ".csv", collapse = "")
|
428 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
429 |
+
|
430 |
+
effs <- read.csv("output_tables_reduced/ effects Informativity_social_models .csv")
|
431 |
+
|
432 |
+
effs_table_Main <- effs %>%
|
433 |
+
rename("2.5%"=2,
|
434 |
+
"50%" = 3,
|
435 |
+
"97.5%" = 4) %>%
|
436 |
+
filter(!grepl("nonlinear", model))
|
437 |
+
|
438 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
439 |
+
|
440 |
+
effs_table_Main <- effs_table_Main %>%
|
441 |
+
relocate(effect, .after = model) %>%
|
442 |
+
flextable() %>%
|
443 |
+
flextable::bold(~ (`2.5%` > 0 & `97.5%` > 0) | (`2.5%` < 0 & `97.5%` < 0), 2) %>%
|
444 |
+
autofit() %>%
|
445 |
+
merge_v(j=c("model", "WAIC")) %>%
|
446 |
+
fix_border_issues() %>%
|
447 |
+
border_inner_h()
|
448 |
+
|
449 |
+
save_as_docx(
|
450 |
+
"Effects in informativity models with fixed and random effects" = effs_table_Main,
|
451 |
+
path = "output_tables_reduced/table_Main_effects_Informativity_social_models.docx")
|
452 |
+
|
453 |
+
|
454 |
+
effs_plot <- effs %>%
|
455 |
+
#filter(WAIC <= top_9) %>%
|
456 |
+
rename(lower=2,
|
457 |
+
upper = 4,
|
458 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
459 |
+
#filter(!effect == "Intercept") %>%
|
460 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status"))) %>%
|
461 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
462 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
463 |
+
group_by(WAIC) %>%
|
464 |
+
arrange(WAIC) %>%
|
465 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
466 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
467 |
+
|
468 |
+
|
469 |
+
#plot modified from function ggregplot::Efxplot
|
470 |
+
cols = c(brewer.pal(12, "Paired"))
|
471 |
+
cols = c(cols[c(12, 10)], "gray50", cols[c(1:8)])
|
472 |
+
|
473 |
+
show_col(cols)
|
474 |
+
|
475 |
+
plot_1 <- ggplot(effs_plot,
|
476 |
+
aes(y = as.factor(model),
|
477 |
+
x = mean,
|
478 |
+
group = effect,
|
479 |
+
colour = effect)) +
|
480 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
481 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
482 |
+
scale_color_manual(values=cols) +
|
483 |
+
ylab("Model of informativity") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
484 |
+
theme(axis.text=element_text(size=50),
|
485 |
+
legend.text=element_text(size=50),
|
486 |
+
axis.title=element_text(size=50),
|
487 |
+
legend.title=element_text(size=50),
|
488 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
489 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
490 |
+
|
491 |
+
|
492 |
+
|
493 |
+
#plot_1
|
494 |
+
ggsave(filename = 'output_reduced/SP_models_plot_Informativity_social_models.jpg',
|
495 |
+
plot_1, height = 20, width = 45)
|
496 |
+
|
497 |
+
|
498 |
+
#saving hyperparameters: Gaussian observations
|
499 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
500 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
501 |
+
}
|
502 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
503 |
+
|
504 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables_reduced/Informativity_social_models_marginals_hyperpar_gaussian.csv")
|
505 |
+
|
506 |
+
#saving hyperparameters: phylogenetic
|
507 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
508 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
509 |
+
}
|
510 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
511 |
+
|
512 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables_reduced/Informativity_social_models_marginals_hyperpar_phylogenetic.csv")
|
513 |
+
|
514 |
+
#saving hyperparameters: spatial
|
515 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
516 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
517 |
+
}
|
518 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
519 |
+
|
520 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables_reduced/Informativity_social_models_marginals_hyperpar_spatial.csv")
|
521 |
+
|
522 |
+
|
523 |
+
#saving summaries of random effects: phylogenetic
|
524 |
+
for (i in 1:length(summary_random_list_phy)) {
|
525 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
526 |
+
}
|
527 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
528 |
+
|
529 |
+
write.csv(summary_random_list_phy, "output_tables_reduced/Informativity_social_models_summary_random_phy.csv")
|
530 |
+
|
531 |
+
#saving summaries of random effects: spatial
|
532 |
+
for (i in 1:length(summary_random_list_spa)) {
|
533 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
534 |
+
}
|
535 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
536 |
+
|
537 |
+
write.csv(summary_random_list_spa, "output_tables_reduced/Informativity_social_models_summary_random_spa.csv")
|
101/replication_package/models_Informativity_reduced_social_only.R
ADDED
@@ -0,0 +1,435 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Informativity predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("L1_log_st"),
|
63 |
+
|
64 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("L2_prop"),
|
67 |
+
|
68 |
+
c("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("neighboring_languages_st"),
|
75 |
+
|
76 |
+
c("Official"),
|
77 |
+
|
78 |
+
c("Education"))
|
79 |
+
|
80 |
+
|
81 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
82 |
+
|
83 |
+
predterms <- t(as.data.frame(predterms))
|
84 |
+
|
85 |
+
predterms_short <- predterms
|
86 |
+
|
87 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
88 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
89 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
92 |
+
|
93 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
94 |
+
number = 1:length(predterms_short))
|
95 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
96 |
+
|
97 |
+
|
98 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
99 |
+
number = 1:length(predterms_short))
|
100 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
101 |
+
|
102 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
103 |
+
number = 1:length(predterms_short))
|
104 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
105 |
+
|
106 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
107 |
+
number = 1:length(predterms_short))
|
108 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
109 |
+
|
110 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
111 |
+
number = 1:length(predterms_short))
|
112 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
113 |
+
|
114 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
115 |
+
number = 1:length(predterms_short))
|
116 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
117 |
+
|
118 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
119 |
+
number = 1:length(predterms_short))
|
120 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
121 |
+
|
122 |
+
models_number <- length(predterms_short)
|
123 |
+
|
124 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
125 |
+
intercept_matrix <- matrix(NA, models_number, 5)
|
126 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
127 |
+
|
128 |
+
social_effects_matrix_L1 <- matrix(NA, models_number, 5)
|
129 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
130 |
+
social_effects_matrix_L1_nl <- matrix(NA, models_number, 5)
|
131 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
132 |
+
social_effects_matrix_L2_prop <- matrix(NA, models_number, 5)
|
133 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
134 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, models_number, 5)
|
135 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
136 |
+
social_effects_matrix_N <- matrix(NA, models_number, 5)
|
137 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
138 |
+
social_effects_matrix_O <- matrix(NA, models_number, 5)
|
139 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
140 |
+
social_effects_matrix_E <- matrix(NA, models_number, 5)
|
141 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
142 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, models_number, 5)
|
143 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
144 |
+
|
145 |
+
#fitted values
|
146 |
+
fitted_list <- vector("list", models_number)
|
147 |
+
names(fitted_list) <- predterms_short
|
148 |
+
|
149 |
+
#marginals of hyperparameters
|
150 |
+
marginals_hyperpar_list_gaussian <- vector("list", models_number)
|
151 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
152 |
+
|
153 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", models_number)
|
154 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
155 |
+
|
156 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", models_number)
|
157 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
158 |
+
|
159 |
+
|
160 |
+
#marginals of fixed effects
|
161 |
+
marginals_fixed_list_Intercept <- vector("list", models_number)
|
162 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
163 |
+
|
164 |
+
marginals_fixed_list_L1 <- vector("list", models_number)
|
165 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
166 |
+
|
167 |
+
marginals_fixed_list_L2_prop <- vector("list", models_number)
|
168 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
169 |
+
|
170 |
+
marginals_fixed_list_O <- vector("list", models_number)
|
171 |
+
names(marginals_fixed_list_O) <- predterms_short
|
172 |
+
|
173 |
+
marginals_fixed_list_N <- vector("list", models_number)
|
174 |
+
names(marginals_fixed_list_N) <- predterms_short
|
175 |
+
|
176 |
+
marginals_fixed_list_E <- vector("list", models_number)
|
177 |
+
names(marginals_fixed_list_E) <- predterms_short
|
178 |
+
|
179 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", models_number)
|
180 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
#summary statistics of random effects
|
186 |
+
summary_random_list_social_L1_nl <- vector("list", models_number)
|
187 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
188 |
+
|
189 |
+
summary_random_list_social_L2_prop_nl <- vector("list", models_number)
|
190 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
191 |
+
|
192 |
+
|
193 |
+
coefm <- matrix(NA,models_number,1)
|
194 |
+
result <- vector("list",models_number)
|
195 |
+
|
196 |
+
for(i in 1:models_number){
|
197 |
+
formula <- as.formula(paste("informativity_st ~ ",predterms[[i]]))
|
198 |
+
result[[i]] <- inla(formula, family="gaussian",
|
199 |
+
control.family = list(hyper = pcprior_hyper),
|
200 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
201 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
202 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
203 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
204 |
+
|
205 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
206 |
+
|
207 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
208 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
209 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
210 |
+
|
211 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
212 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
213 |
+
|
214 |
+
if(i %in% L1_nl_element){
|
215 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
216 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
217 |
+
method = "linear") %>%
|
218 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
219 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
220 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
221 |
+
}
|
222 |
+
|
223 |
+
if(i %in% L2_prop_nl_element){
|
224 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
225 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
226 |
+
method = "linear") %>%
|
227 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
228 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
229 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
230 |
+
}
|
231 |
+
|
232 |
+
if(i %in% L1_element) {
|
233 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
234 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
235 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
236 |
+
|
237 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
238 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
239 |
+
}
|
240 |
+
|
241 |
+
if(i %in% L2_prop_element) {
|
242 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
243 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
244 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
245 |
+
|
246 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
247 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
248 |
+
}
|
249 |
+
|
250 |
+
if(i %in% neighbour_element) {
|
251 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
252 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
253 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
254 |
+
|
255 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
256 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
257 |
+
}
|
258 |
+
|
259 |
+
if(i %in% official_element) {
|
260 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
261 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
262 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
263 |
+
|
264 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
265 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
266 |
+
}
|
267 |
+
|
268 |
+
if(i %in% education_element) {
|
269 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
270 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
271 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
272 |
+
|
273 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
274 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
275 |
+
}
|
276 |
+
|
277 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
278 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
279 |
+
mutate(across(where(is.numeric), round, 2))
|
280 |
+
|
281 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
282 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
283 |
+
|
284 |
+
if(i %in% L1_nl_element){
|
285 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
286 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
287 |
+
}
|
288 |
+
|
289 |
+
if(i %in% L2_prop_nl_element){
|
290 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
291 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
292 |
+
}
|
293 |
+
}
|
294 |
+
|
295 |
+
#beepr::beep(5)
|
296 |
+
|
297 |
+
save(result, file = "output_models_reduced/models_Informativity_social_only.RData")
|
298 |
+
|
299 |
+
|
300 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
301 |
+
colnames(coefm) <- c("model", "WAIC")
|
302 |
+
coefm <- coefm %>%
|
303 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
304 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
305 |
+
arrange(WAIC)
|
306 |
+
|
307 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
308 |
+
coefm <- coefm[order(coefm$WAIC),]
|
309 |
+
|
310 |
+
coefm_path <- paste("output_tables_reduced/", "waics", "Informativity_social_only_models", ".csv", collapse = "")
|
311 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
312 |
+
|
313 |
+
for (i in 1:length(fitted_list)) {
|
314 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
315 |
+
}
|
316 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
317 |
+
fitted_list_path <- paste("output_tables_reduced/", "fitted_list", "Informativity_social_only_models", ".csv", collapse = "")
|
318 |
+
write.csv(fitted_list, fitted_list_path)
|
319 |
+
|
320 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
321 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
322 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
323 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
324 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
325 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
326 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
327 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
328 |
+
|
329 |
+
intercept_effects$effect <- "Intercept"
|
330 |
+
L1_effects$effect <- "L1"
|
331 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
332 |
+
L2_prop_effects$effect <- "L2 proportion"
|
333 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
334 |
+
N_effects$effect <- "Neighbours"
|
335 |
+
E_effects$effect <- "Education"
|
336 |
+
O_effects$effect <- "Official status"
|
337 |
+
|
338 |
+
effs <- as.data.frame(rbind(intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects))
|
339 |
+
effs <- effs %>%
|
340 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
341 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
342 |
+
na.omit() %>%
|
343 |
+
arrange(WAIC) %>%
|
344 |
+
relocate(model)
|
345 |
+
|
346 |
+
effs_path <- paste("output_tables_reduced/", "effects", "Informativity_social_only_models", ".csv", collapse = "")
|
347 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
348 |
+
|
349 |
+
effs <- read.csv("output_tables_reduced/ effects Informativity_social_only_models .csv")
|
350 |
+
|
351 |
+
effs_table_SM <- effs %>%
|
352 |
+
rename("2.5%"=2,
|
353 |
+
"50%" = 4,
|
354 |
+
"97.5%" = 3) %>%
|
355 |
+
flextable() %>%
|
356 |
+
autofit() %>%
|
357 |
+
merge_v(j=c("model", "WAIC")) %>%
|
358 |
+
fix_border_issues() %>%
|
359 |
+
border_inner_h()
|
360 |
+
|
361 |
+
save_as_docx(
|
362 |
+
"Effects in informativity models with fixed and random effects (including non-linear implementations of some fixed effects)" = effs_table_SM,
|
363 |
+
path = "output_tables_reduced/table_SM_effects_Informativity_social_only_models.docx")
|
364 |
+
|
365 |
+
effs_table_Main <- effs %>%
|
366 |
+
rename("2.5%"=2,
|
367 |
+
"50%" = 4,
|
368 |
+
"97.5%" = 3) %>%
|
369 |
+
filter(!grepl("nonlinear", model))
|
370 |
+
|
371 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
372 |
+
|
373 |
+
effs_table_Main <- effs_table_Main %>%
|
374 |
+
flextable() %>%
|
375 |
+
autofit() %>%
|
376 |
+
merge_v(j=c("model", "WAIC")) %>%
|
377 |
+
fix_border_issues() %>%
|
378 |
+
border_inner_h()
|
379 |
+
|
380 |
+
save_as_docx(
|
381 |
+
"Effects in informativity models with fixed and random effects" = effs_table_Main,
|
382 |
+
path = "output_tables_reduced/table_Main_effects_Informativity_social_only_models.docx")
|
383 |
+
|
384 |
+
|
385 |
+
effs_plot <- effs %>%
|
386 |
+
#filter(WAIC <= top_9) %>%
|
387 |
+
rename(lower=2,
|
388 |
+
upper = 4,
|
389 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
390 |
+
#filter(!effect == "Intercept") %>%
|
391 |
+
mutate(effect = factor(effect, levels=c("Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status"))) %>%
|
392 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
393 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
394 |
+
group_by(WAIC) %>%
|
395 |
+
arrange(WAIC) %>%
|
396 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
397 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
398 |
+
|
399 |
+
|
400 |
+
#plot modified from function ggregplot::Efxplot
|
401 |
+
cols = c(brewer.pal(12, "Paired"))
|
402 |
+
cols = c("gray50", cols[c(1:8)])
|
403 |
+
|
404 |
+
show_col(cols)
|
405 |
+
|
406 |
+
plot_1 <- ggplot(effs_plot,
|
407 |
+
aes(y = as.factor(model),
|
408 |
+
x = mean,
|
409 |
+
group = effect,
|
410 |
+
colour = effect)) +
|
411 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
412 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
413 |
+
scale_color_manual(values=cols) +
|
414 |
+
ylab("Model of informativity") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
415 |
+
theme(axis.text=element_text(size=50),
|
416 |
+
legend.text=element_text(size=50),
|
417 |
+
axis.title=element_text(size=50),
|
418 |
+
legend.title=element_text(size=50),
|
419 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
420 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
421 |
+
|
422 |
+
|
423 |
+
|
424 |
+
#plot_1
|
425 |
+
ggsave(filename = 'output_reduced/SP_models_plot_Informativity_social_only_models.jpg',
|
426 |
+
plot_1, height = 20, width = 45)
|
427 |
+
|
428 |
+
|
429 |
+
#saving hyperparameters: Gaussian observations
|
430 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
431 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
432 |
+
}
|
433 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
434 |
+
|
435 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables_reduced/Informativity_social_only_models_marginals_hyperpar_gaussian.csv")
|
101/replication_package/models_Informativity_social.R
ADDED
@@ -0,0 +1,556 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Informativity predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st"),
|
63 |
+
|
64 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L2_prop"),
|
67 |
+
|
68 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "L1_log10:L2_prop"),
|
75 |
+
|
76 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "neighboring_languages_st"),
|
77 |
+
|
78 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Official"),
|
79 |
+
|
80 |
+
c("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Education"))
|
81 |
+
|
82 |
+
|
83 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
84 |
+
|
85 |
+
predterms <- t(as.data.frame(predterms))
|
86 |
+
|
87 |
+
predterms_short <- predterms
|
88 |
+
predterms_short <- gsub("f(phy_id, model = 'generic0', Cmatrix = phylo_prec_mat, constr = TRUE, hyper = pcprior_hyper)", "Phylogenetic", predterms_short, fixed=TRUE)
|
89 |
+
predterms_short <- gsub("f(sp_id, model = 'generic0', Cmatrix = spatial_prec_mat_1, constr = TRUE, hyper = pcprior_hyper)", "Spatial: local", predterms_short, fixed=TRUE)
|
90 |
+
|
91 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
92 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
93 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
94 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
95 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
96 |
+
|
97 |
+
|
98 |
+
phylogenetic_element <- data.frame("judgement" = grepl("Phylogenetic", predterms_short),
|
99 |
+
number = 1:length(predterms_short))
|
100 |
+
phylogenetic_element <- phylogenetic_element[phylogenetic_element$judgement == TRUE,]$number
|
101 |
+
|
102 |
+
spatial_element_local <- data.frame("judgement" = grepl("local", predterms_short),
|
103 |
+
number = 1:length(predterms_short))
|
104 |
+
spatial_element_local <- spatial_element_local[spatial_element_local$judgement == TRUE,]$number
|
105 |
+
|
106 |
+
spatial_element_regional <- data.frame("judgement" = grepl("regional", predterms_short),
|
107 |
+
number = 1:length(predterms_short))
|
108 |
+
spatial_element_regional <- spatial_element_regional[spatial_element_regional$judgement == TRUE,]$number
|
109 |
+
|
110 |
+
spatial_element <- c(spatial_element_local, spatial_element_regional)
|
111 |
+
|
112 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
113 |
+
number = 1:length(predterms_short))
|
114 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
115 |
+
|
116 |
+
|
117 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
118 |
+
number = 1:length(predterms_short))
|
119 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
120 |
+
|
121 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
122 |
+
number = 1:length(predterms_short))
|
123 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
124 |
+
L2_prop_element <- L2_prop_element[-length(L2_prop_element)] #making sure that the interaction term (introduced below) is not treated as belonging to this isolated element
|
125 |
+
|
126 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
127 |
+
number = 1:length(predterms_short))
|
128 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
129 |
+
|
130 |
+
#can use only part of the interaction term within grepl() function
|
131 |
+
interaction_element <- data.frame("judgement" = grepl(":L2 proportion", predterms_short),
|
132 |
+
number = 1:length(predterms_short))
|
133 |
+
interaction_element <- interaction_element[interaction_element$judgement == TRUE,]$number
|
134 |
+
|
135 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
136 |
+
number = 1:length(predterms_short))
|
137 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
138 |
+
|
139 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
140 |
+
number = 1:length(predterms_short))
|
141 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
142 |
+
|
143 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
144 |
+
number = 1:length(predterms_short))
|
145 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
150 |
+
phy_effects_matrix <- matrix(NA, 10, 5)
|
151 |
+
colnames(phy_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
152 |
+
spa_effects_matrix <- matrix(NA, 10, 5)
|
153 |
+
colnames(spa_effects_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
154 |
+
|
155 |
+
intercept_matrix <- matrix(NA, 10, 5)
|
156 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
157 |
+
|
158 |
+
social_effects_matrix_L1 <- matrix(NA, 10, 5)
|
159 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
160 |
+
social_effects_matrix_L1_nl <- matrix(NA, 10, 5)
|
161 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
162 |
+
social_effects_matrix_L2_prop <- matrix(NA, 10, 5)
|
163 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
164 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, 10, 5)
|
165 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
166 |
+
social_effects_matrix_N <- matrix(NA, 10, 5)
|
167 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
168 |
+
social_effects_matrix_O <- matrix(NA, 10, 5)
|
169 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
170 |
+
social_effects_matrix_E <- matrix(NA, 10, 5)
|
171 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
172 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, 10, 5)
|
173 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
174 |
+
|
175 |
+
#fitted values
|
176 |
+
fitted_list <- vector("list", 10)
|
177 |
+
names(fitted_list) <- predterms_short
|
178 |
+
|
179 |
+
#marginals of hyperparameters
|
180 |
+
marginals_hyperpar_list_gaussian <- vector("list", 10)
|
181 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
182 |
+
|
183 |
+
marginals_hyperpar_list_phy <- vector("list", 10)
|
184 |
+
names(marginals_hyperpar_list_phy) <- predterms_short
|
185 |
+
|
186 |
+
marginals_hyperpar_list_spa <- vector("list", 10)
|
187 |
+
names(marginals_hyperpar_list_spa) <- predterms_short
|
188 |
+
|
189 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", 10)
|
190 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
191 |
+
|
192 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", 10)
|
193 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
194 |
+
|
195 |
+
|
196 |
+
#marginals of fixed effects
|
197 |
+
marginals_fixed_list_Intercept <- vector("list", 10)
|
198 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
199 |
+
|
200 |
+
marginals_fixed_list_L1 <- vector("list", 10)
|
201 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
202 |
+
|
203 |
+
marginals_fixed_list_L2_prop <- vector("list", 10)
|
204 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
205 |
+
|
206 |
+
marginals_fixed_list_O <- vector("list", 10)
|
207 |
+
names(marginals_fixed_list_O) <- predterms_short
|
208 |
+
|
209 |
+
marginals_fixed_list_N <- vector("list", 10)
|
210 |
+
names(marginals_fixed_list_N) <- predterms_short
|
211 |
+
|
212 |
+
marginals_fixed_list_E <- vector("list", 10)
|
213 |
+
names(marginals_fixed_list_E) <- predterms_short
|
214 |
+
|
215 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", 10)
|
216 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
217 |
+
|
218 |
+
|
219 |
+
|
220 |
+
|
221 |
+
#summary statistics of random effects
|
222 |
+
summary_random_list_phy <- vector("list", 10)
|
223 |
+
names(summary_random_list_phy) <- predterms_short
|
224 |
+
|
225 |
+
summary_random_list_spa <- vector("list", 10)
|
226 |
+
names(summary_random_list_spa) <- predterms_short
|
227 |
+
|
228 |
+
summary_random_list_social_L1_nl <- vector("list", 10)
|
229 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
230 |
+
|
231 |
+
summary_random_list_social_L2_prop_nl <- vector("list", 10)
|
232 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
233 |
+
|
234 |
+
|
235 |
+
coefm <- matrix(NA,10,1)
|
236 |
+
result <- vector("list",10)
|
237 |
+
|
238 |
+
for(i in 1:10){
|
239 |
+
formula <- as.formula(paste("informativity_st ~ ",predterms[[i]]))
|
240 |
+
result[[i]] <- inla(formula, family="gaussian",
|
241 |
+
control.family = list(hyper = pcprior_hyper),
|
242 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
243 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
244 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
245 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
246 |
+
|
247 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
248 |
+
|
249 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
250 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
251 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
252 |
+
|
253 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
254 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
255 |
+
|
256 |
+
if(i %in% phylogenetic_element) {
|
257 |
+
phy_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
258 |
+
result[[i]]$marginals.hyperpar$`Precision for phy_id`,
|
259 |
+
method = "linear") %>%
|
260 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
261 |
+
phy_effects_matrix[i, 4] <- predterms_short[[i]]
|
262 |
+
phy_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
263 |
+
}
|
264 |
+
|
265 |
+
if(i %in% spatial_element) {
|
266 |
+
spa_effects_matrix[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
267 |
+
result[[i]]$marginals.hyperpar$`Precision for sp_id`,
|
268 |
+
method = "linear") %>%
|
269 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
270 |
+
spa_effects_matrix[i, 4] <- predterms_short[[i]]
|
271 |
+
spa_effects_matrix[i, 5] <- result[[i]]$waic$waic
|
272 |
+
}
|
273 |
+
|
274 |
+
|
275 |
+
if(i %in% L1_nl_element){
|
276 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
277 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
278 |
+
method = "linear") %>%
|
279 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
280 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
281 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
282 |
+
}
|
283 |
+
|
284 |
+
if(i %in% L2_prop_nl_element){
|
285 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
286 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
287 |
+
method = "linear") %>%
|
288 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
289 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
290 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
291 |
+
}
|
292 |
+
|
293 |
+
if(i %in% L1_element) {
|
294 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
295 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
296 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
297 |
+
|
298 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
299 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
300 |
+
}
|
301 |
+
|
302 |
+
if(i %in% L2_prop_element) {
|
303 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
304 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
305 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
306 |
+
|
307 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
308 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
309 |
+
}
|
310 |
+
|
311 |
+
if(i %in% interaction_element) {
|
312 |
+
social_effects_matrix_L1_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.975quant`)
|
313 |
+
social_effects_matrix_L1_L2_prop[i, 4] <- predterms_short[[i]]
|
314 |
+
social_effects_matrix_L1_L2_prop[i, 5] <- result[[i]]$waic$waic
|
315 |
+
|
316 |
+
marginals_fixed_list_L1_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log10:L2_prop"]]))
|
317 |
+
colnames(marginals_fixed_list_L1_L2_prop[[i]]) <- c("x for L1*L2 proportion", "y for L1*L2 proportion")
|
318 |
+
}
|
319 |
+
|
320 |
+
if(i %in% neighbour_element) {
|
321 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
322 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
323 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
324 |
+
|
325 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
326 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
327 |
+
}
|
328 |
+
|
329 |
+
if(i %in% official_element) {
|
330 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
331 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
332 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
333 |
+
|
334 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
335 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
336 |
+
}
|
337 |
+
|
338 |
+
if(i %in% education_element) {
|
339 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
340 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
341 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
342 |
+
|
343 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
344 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
345 |
+
}
|
346 |
+
|
347 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
348 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
349 |
+
mutate(across(where(is.numeric), round, 2))
|
350 |
+
|
351 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
352 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
353 |
+
|
354 |
+
if(i %in% phylogenetic_element){
|
355 |
+
marginals_hyperpar_list_phy[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for phy_id"]]))
|
356 |
+
colnames(marginals_hyperpar_list_phy[[i]]) <- c("x for phy_id", "y for phy_id")
|
357 |
+
}
|
358 |
+
|
359 |
+
if(i %in% spatial_element){
|
360 |
+
marginals_hyperpar_list_spa[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for sp_id"]]))
|
361 |
+
colnames(marginals_hyperpar_list_spa[[i]]) <- c("x for sp_id", "y for sp_id")
|
362 |
+
}
|
363 |
+
|
364 |
+
if(i %in% L1_nl_element){
|
365 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
366 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
367 |
+
}
|
368 |
+
|
369 |
+
if(i %in% L2_prop_nl_element){
|
370 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
371 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
372 |
+
}
|
373 |
+
|
374 |
+
if(i %in% phylogenetic_element){
|
375 |
+
summary_random_list_phy[[i]] <- cbind(result[[i]]$summary.random$phy_id) %>%
|
376 |
+
rename(phy_id = ID) %>%
|
377 |
+
as.data.frame() %>%
|
378 |
+
mutate(across(where(is.numeric), round, 2))
|
379 |
+
}
|
380 |
+
|
381 |
+
if(i %in% spatial_element){
|
382 |
+
summary_random_list_spa[[i]] <- cbind(result[[i]]$summary.random$sp_id) %>%
|
383 |
+
rename(sp_id = ID) %>%
|
384 |
+
as.data.frame() %>%
|
385 |
+
mutate(across(where(is.numeric), round, 2))
|
386 |
+
}
|
387 |
+
}
|
388 |
+
|
389 |
+
#beepr::beep(5)
|
390 |
+
|
391 |
+
save(result, file = "output_models/models_Informativity_social.RData")
|
392 |
+
|
393 |
+
|
394 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
395 |
+
colnames(coefm) <- c("model", "WAIC")
|
396 |
+
coefm <- coefm %>%
|
397 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
398 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
399 |
+
arrange(WAIC)
|
400 |
+
|
401 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
402 |
+
coefm <- coefm[order(coefm$WAIC),]
|
403 |
+
|
404 |
+
coefm_path <- paste("output_tables/", "waics", "Informativity_social_models", ".csv", collapse = "")
|
405 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
406 |
+
|
407 |
+
for (i in 1:length(fitted_list)) {
|
408 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
409 |
+
}
|
410 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
411 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Informativity_social_models", ".csv", collapse = "")
|
412 |
+
write.csv(fitted_list, fitted_list_path)
|
413 |
+
|
414 |
+
phy_effects<-as.data.frame(phy_effects_matrix)
|
415 |
+
spa_effects<-as.data.frame(spa_effects_matrix)
|
416 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
417 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
418 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
419 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
420 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
421 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
422 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
423 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
424 |
+
interaction_effects <- as.data.frame(social_effects_matrix_L1_L2_prop)
|
425 |
+
|
426 |
+
phy_effects$effect <- "phylogenetic SD"
|
427 |
+
spa_effects$effect <- "spatial SD"
|
428 |
+
intercept_effects$effect <- "Intercept"
|
429 |
+
L1_effects$effect <- "L1"
|
430 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
431 |
+
L2_prop_effects$effect <- "L2 proportion"
|
432 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
433 |
+
N_effects$effect <- "Neighbours"
|
434 |
+
E_effects$effect <- "Education"
|
435 |
+
O_effects$effect <- "Official status"
|
436 |
+
interaction_effects$effect <- "L1*L2 proportion"
|
437 |
+
|
438 |
+
effs <- as.data.frame(rbind(phy_effects, spa_effects, intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects, interaction_effects))
|
439 |
+
effs <- effs %>%
|
440 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
441 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
442 |
+
na.omit() %>%
|
443 |
+
arrange(WAIC) %>%
|
444 |
+
relocate(model)
|
445 |
+
|
446 |
+
effs_path <- paste("output_tables/", "effects", "Informativity_social_models", ".csv", collapse = "")
|
447 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
448 |
+
|
449 |
+
effs <- read.csv("output_tables/ effects Informativity_social_models .csv")
|
450 |
+
|
451 |
+
effs_table_Main <- effs %>%
|
452 |
+
rename("2.5%"=2,
|
453 |
+
"50%" = 3,
|
454 |
+
"97.5%" = 4) %>%
|
455 |
+
filter(!grepl("nonlinear", model))
|
456 |
+
|
457 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
458 |
+
|
459 |
+
effs_table_Main <- effs_table_Main %>%
|
460 |
+
relocate(effect, .after = model) %>%
|
461 |
+
flextable() %>%
|
462 |
+
flextable::bold(~ (`2.5%` > 0 & `97.5%` > 0) | (`2.5%` < 0 & `97.5%` < 0), 2) %>%
|
463 |
+
autofit() %>%
|
464 |
+
merge_v(j=c("model", "WAIC")) %>%
|
465 |
+
fix_border_issues() %>%
|
466 |
+
border_inner_h()
|
467 |
+
|
468 |
+
save_as_docx(
|
469 |
+
"Effects in informativity models with fixed and random effects" = effs_table_Main,
|
470 |
+
path = "output_tables/table_Main_effects_Informativity_social_models.docx")
|
471 |
+
|
472 |
+
|
473 |
+
effs_plot <- effs %>%
|
474 |
+
#filter(WAIC <= top_9) %>%
|
475 |
+
rename(lower=2,
|
476 |
+
upper = 4,
|
477 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
478 |
+
#filter(!effect == "Intercept") %>%
|
479 |
+
mutate(effect = factor(effect, levels=c("phylogenetic SD", "spatial SD", "Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status", "L1*L2 proportion"))) %>%
|
480 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
481 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
482 |
+
group_by(WAIC) %>%
|
483 |
+
arrange(WAIC) %>%
|
484 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
485 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
486 |
+
|
487 |
+
|
488 |
+
#plot modified from function ggregplot::Efxplot
|
489 |
+
cols = c(brewer.pal(12, "Paired"))
|
490 |
+
cols = c(cols[c(12, 10)], "gray50", cols[c(1:8)])
|
491 |
+
|
492 |
+
show_col(cols)
|
493 |
+
|
494 |
+
plot_1 <- ggplot(effs_plot,
|
495 |
+
aes(y = as.factor(model),
|
496 |
+
x = mean,
|
497 |
+
group = effect,
|
498 |
+
colour = effect)) +
|
499 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
500 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
501 |
+
scale_color_manual(values=cols) +
|
502 |
+
ylab("Model of informativity") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
503 |
+
theme(axis.text=element_text(size=50),
|
504 |
+
legend.text=element_text(size=50),
|
505 |
+
axis.title=element_text(size=50),
|
506 |
+
legend.title=element_text(size=50),
|
507 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
508 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
509 |
+
|
510 |
+
|
511 |
+
|
512 |
+
#plot_1
|
513 |
+
ggsave(filename = 'output/SP_models_plot_Informativity_social_models.jpg',
|
514 |
+
plot_1, height = 20, width = 45)
|
515 |
+
|
516 |
+
|
517 |
+
#saving hyperparameters: Gaussian observations
|
518 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
519 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
520 |
+
}
|
521 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
522 |
+
|
523 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Informativity_social_models_marginals_hyperpar_gaussian.csv")
|
524 |
+
|
525 |
+
#saving hyperparameters: phylogenetic
|
526 |
+
for (i in 1:length(marginals_hyperpar_list_phy)) {
|
527 |
+
marginals_hyperpar_list_phy[[i]]$model <- names(marginals_hyperpar_list_phy)[i]
|
528 |
+
}
|
529 |
+
marginals_hyperpar_list_phy <- dplyr::bind_rows(marginals_hyperpar_list_phy)
|
530 |
+
|
531 |
+
write.csv(marginals_hyperpar_list_phy, "output_tables/Informativity_social_models_marginals_hyperpar_phylogenetic.csv")
|
532 |
+
|
533 |
+
#saving hyperparameters: spatial
|
534 |
+
for (i in 1:length(marginals_hyperpar_list_spa)) {
|
535 |
+
marginals_hyperpar_list_spa[[i]]$model <- names(marginals_hyperpar_list_spa)[i]
|
536 |
+
}
|
537 |
+
marginals_hyperpar_list_spa <- dplyr::bind_rows(marginals_hyperpar_list_spa)
|
538 |
+
|
539 |
+
write.csv(marginals_hyperpar_list_spa, "output_tables/Informativity_social_models_marginals_hyperpar_spatial.csv")
|
540 |
+
|
541 |
+
|
542 |
+
#saving summaries of random effects: phylogenetic
|
543 |
+
for (i in 1:length(summary_random_list_phy)) {
|
544 |
+
summary_random_list_phy[[i]]$model <- names(summary_random_list_phy)[i]
|
545 |
+
}
|
546 |
+
summary_random_list_phy <- dplyr::bind_rows(summary_random_list_phy)
|
547 |
+
|
548 |
+
write.csv(summary_random_list_phy, "output_tables/Informativity_social_models_summary_random_phy.csv")
|
549 |
+
|
550 |
+
#saving summaries of random effects: spatial
|
551 |
+
for (i in 1:length(summary_random_list_spa)) {
|
552 |
+
summary_random_list_spa[[i]]$model <- names(summary_random_list_spa)[i]
|
553 |
+
}
|
554 |
+
summary_random_list_spa <- dplyr::bind_rows(summary_random_list_spa)
|
555 |
+
|
556 |
+
write.csv(summary_random_list_spa, "output_tables/Informativity_social_models_summary_random_spa.csv")
|
101/replication_package/models_Informativity_social_only.R
ADDED
@@ -0,0 +1,454 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#model fitting: Informativity predicted by social effects on top of random phylogenetic and spatial factors
|
2 |
+
|
3 |
+
#Script was written by Sam Passmore and modified by Olena Shcherbakova
|
4 |
+
|
5 |
+
source("requirements.R")
|
6 |
+
|
7 |
+
source("install_and_load_INLA.R")
|
8 |
+
|
9 |
+
source("set_up_inla.R")
|
10 |
+
|
11 |
+
metrics_joined <- metrics_joined %>%
|
12 |
+
filter(!is.na(L1_log10_st)) %>%
|
13 |
+
rename(L1_log_st = L1_log10_st) %>%
|
14 |
+
mutate(L1_copy = L1_log_st) %>%
|
15 |
+
filter(!is.na(L2_prop)) %>%
|
16 |
+
mutate(L2_copy = L2_prop) %>%
|
17 |
+
filter(!is.na(neighboring_languages_st)) %>%
|
18 |
+
filter(!is.na(Official)) %>%
|
19 |
+
filter(!is.na(Education)) %>%
|
20 |
+
filter(!is.na(boundness_st)) %>%
|
21 |
+
filter(!is.na(informativity_st))
|
22 |
+
|
23 |
+
#dropping tips not in Grambank
|
24 |
+
metrics_joined <- metrics_joined[metrics_joined$Language_ID %in% tree$tip.label, ]
|
25 |
+
tree <- keep.tip(tree, metrics_joined$Language_ID)
|
26 |
+
|
27 |
+
x <- assert_that(all(tree$tip.label %in% metrics_joined$Language_ID), msg = "The data and phylogeny taxa do not match")
|
28 |
+
|
29 |
+
## Building standardized phylogenetic precision matrix
|
30 |
+
tree_scaled <- tree
|
31 |
+
|
32 |
+
tree_vcv = vcv.phylo(tree_scaled)
|
33 |
+
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))
|
34 |
+
|
35 |
+
#We opt for a sparse phylogenetic precision matrix (i.e. it is quantified using all nodes and tips), since sparse matrices make the analysis in INLA less time-intensive
|
36 |
+
tree_scaled$edge.length <- tree_scaled$edge.length/typical_phylogenetic_variance
|
37 |
+
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
|
38 |
+
nodes = "ALL",
|
39 |
+
scale = FALSE)$Ainv
|
40 |
+
|
41 |
+
metrics_joined = metrics_joined[order(match(metrics_joined$Language_ID, rownames(phylo_prec_mat))),]
|
42 |
+
|
43 |
+
#"local" set of parameters
|
44 |
+
## Create spatial covariance matrix using the matern covariance function
|
45 |
+
spatial_covar_mat_1 = varcov.spatial(metrics_joined[,c("Longitude", "Latitude")],
|
46 |
+
cov.pars = phi_1, kappa = kappa)$varcov
|
47 |
+
# Calculate and standardize by the typical variance
|
48 |
+
typical_variance_spatial_1 = exp(mean(log(diag(spatial_covar_mat_1))))
|
49 |
+
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
|
50 |
+
spatial_prec_mat_1 = solve(spatial_cov_std_1)
|
51 |
+
dimnames(spatial_prec_mat_1) = list(metrics_joined$Language_ID, metrics_joined$Language_ID)
|
52 |
+
|
53 |
+
## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
|
54 |
+
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
|
55 |
+
metrics_joined$phy_id = phy_id
|
56 |
+
|
57 |
+
## Other effects are in the same order they appear in the dataset
|
58 |
+
metrics_joined$sp_id = 1:nrow(spatial_prec_mat_1)
|
59 |
+
|
60 |
+
#Preparing the formulas for 10 competing models to be used in inla() call
|
61 |
+
listcombo <- list(
|
62 |
+
c("L1_log_st"),
|
63 |
+
|
64 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)"),
|
65 |
+
|
66 |
+
c("L2_prop"),
|
67 |
+
|
68 |
+
c("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
69 |
+
|
70 |
+
c("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "f(inla.group(L2_copy), model='rw2', scale.model = TRUE)"),
|
71 |
+
|
72 |
+
c("L1_log_st", "L2_prop"),
|
73 |
+
|
74 |
+
c("L1_log10:L2_prop"),
|
75 |
+
|
76 |
+
c("neighboring_languages_st"),
|
77 |
+
|
78 |
+
c("Official"),
|
79 |
+
|
80 |
+
c("Education"))
|
81 |
+
|
82 |
+
|
83 |
+
predterms <- lapply(listcombo, function(x) paste(x, collapse="+"))
|
84 |
+
|
85 |
+
predterms <- t(as.data.frame(predterms))
|
86 |
+
|
87 |
+
predterms_short <- predterms
|
88 |
+
|
89 |
+
predterms_short <- gsub("f(inla.group(L1_copy), model='rw2', scale.model = TRUE)", "L1 speakers (nonlinear)", predterms_short, fixed=TRUE)
|
90 |
+
predterms_short <- gsub("L1_log_st", "L1 speakers (linear)", predterms_short, fixed=TRUE)
|
91 |
+
predterms_short <- gsub("f(inla.group(L2_copy), model='rw2', scale.model = TRUE)", "L2 proportion (nonlinear)", predterms_short, fixed=TRUE)
|
92 |
+
predterms_short <- gsub("L2_prop", "L2 proportion (linear)", predterms_short, fixed=TRUE)
|
93 |
+
predterms_short <- gsub("neighboring_languages_st", "Neighbours", predterms_short, fixed=TRUE)
|
94 |
+
|
95 |
+
L1_element <- data.frame("judgement" = grepl("L1 speakers (linear)", predterms_short, fixed=TRUE),
|
96 |
+
number = 1:length(predterms_short))
|
97 |
+
L1_element <- L1_element[L1_element$judgement == TRUE,]$number
|
98 |
+
|
99 |
+
|
100 |
+
L1_nl_element <- data.frame("judgement" = grepl("L1 speakers (nonlinear)", predterms_short, fixed=TRUE),
|
101 |
+
number = 1:length(predterms_short))
|
102 |
+
L1_nl_element <- L1_nl_element[L1_nl_element$judgement == TRUE,]$number
|
103 |
+
|
104 |
+
L2_prop_element <- data.frame("judgement" = grepl("L2 proportion (linear)", predterms_short, fixed=TRUE),
|
105 |
+
number = 1:length(predterms_short))
|
106 |
+
L2_prop_element <- L2_prop_element[L2_prop_element$judgement == TRUE,]$number
|
107 |
+
L2_prop_element <- L2_prop_element[-length(L2_prop_element)] #making sure that the interaction term (introduced below) is not treated as belonging to this isolated element
|
108 |
+
|
109 |
+
L2_prop_nl_element <- data.frame("judgement" = grepl("L2 proportion (nonlinear)", predterms_short, fixed=TRUE),
|
110 |
+
number = 1:length(predterms_short))
|
111 |
+
L2_prop_nl_element <- L2_prop_nl_element[L2_prop_nl_element$judgement == TRUE,]$number
|
112 |
+
|
113 |
+
#can use only part of the interaction term within grepl() function
|
114 |
+
interaction_element <- data.frame("judgement" = grepl(":L2 proportion", predterms_short),
|
115 |
+
number = 1:length(predterms_short))
|
116 |
+
interaction_element <- interaction_element[interaction_element$judgement == TRUE,]$number
|
117 |
+
|
118 |
+
neighbour_element <- data.frame("judgement" = grepl("Neighbours", predterms_short),
|
119 |
+
number = 1:length(predterms_short))
|
120 |
+
neighbour_element <- neighbour_element[neighbour_element$judgement == TRUE,]$number
|
121 |
+
|
122 |
+
official_element <- data.frame("judgement" = grepl("Official", predterms_short),
|
123 |
+
number = 1:length(predterms_short))
|
124 |
+
official_element <- official_element[official_element$judgement == TRUE,]$number
|
125 |
+
|
126 |
+
education_element <- data.frame("judgement" = grepl("Education", predterms_short),
|
127 |
+
number = 1:length(predterms_short))
|
128 |
+
education_element <- education_element[education_element$judgement == TRUE,]$number
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
#preparing empty matrices to be filled with effect estimates (quantiles), model name, and WAIC value
|
133 |
+
intercept_matrix <- matrix(NA, 10, 5)
|
134 |
+
colnames(intercept_matrix) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
135 |
+
|
136 |
+
social_effects_matrix_L1 <- matrix(NA, 10, 5)
|
137 |
+
colnames(social_effects_matrix_L1) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
138 |
+
social_effects_matrix_L1_nl <- matrix(NA, 10, 5)
|
139 |
+
colnames(social_effects_matrix_L1_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
140 |
+
social_effects_matrix_L2_prop <- matrix(NA, 10, 5)
|
141 |
+
colnames(social_effects_matrix_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
142 |
+
social_effects_matrix_L2_prop_nl <- matrix(NA, 10, 5)
|
143 |
+
colnames(social_effects_matrix_L2_prop_nl) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
144 |
+
social_effects_matrix_N <- matrix(NA, 10, 5)
|
145 |
+
colnames(social_effects_matrix_N) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
146 |
+
social_effects_matrix_O <- matrix(NA, 10, 5)
|
147 |
+
colnames(social_effects_matrix_O) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
148 |
+
social_effects_matrix_E <- matrix(NA, 10, 5)
|
149 |
+
colnames(social_effects_matrix_E) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
150 |
+
social_effects_matrix_L1_L2_prop <- matrix(NA, 10, 5)
|
151 |
+
colnames(social_effects_matrix_L1_L2_prop) <- c("2.5%", "50%", "97.5%", "model", "WAIC")
|
152 |
+
|
153 |
+
#fitted values
|
154 |
+
fitted_list <- vector("list", 10)
|
155 |
+
names(fitted_list) <- predterms_short
|
156 |
+
|
157 |
+
#marginals of hyperparameters
|
158 |
+
marginals_hyperpar_list_gaussian <- vector("list", 10)
|
159 |
+
names(marginals_hyperpar_list_gaussian) <- predterms_short
|
160 |
+
|
161 |
+
marginals_hyperpar_list_social_L1_nl <- vector("list", 10)
|
162 |
+
names(marginals_hyperpar_list_social_L1_nl) <- predterms_short
|
163 |
+
|
164 |
+
marginals_hyperpar_list_social_L2_prop_nl <- vector("list", 10)
|
165 |
+
names(marginals_hyperpar_list_social_L2_prop_nl) <- predterms_short
|
166 |
+
|
167 |
+
|
168 |
+
#marginals of fixed effects
|
169 |
+
marginals_fixed_list_Intercept <- vector("list", 10)
|
170 |
+
names(marginals_fixed_list_Intercept) <- predterms_short
|
171 |
+
|
172 |
+
marginals_fixed_list_L1 <- vector("list", 10)
|
173 |
+
names(marginals_fixed_list_L1) <- predterms_short
|
174 |
+
|
175 |
+
marginals_fixed_list_L2_prop <- vector("list", 10)
|
176 |
+
names(marginals_fixed_list_L2_prop) <- predterms_short
|
177 |
+
|
178 |
+
marginals_fixed_list_O <- vector("list", 10)
|
179 |
+
names(marginals_fixed_list_O) <- predterms_short
|
180 |
+
|
181 |
+
marginals_fixed_list_N <- vector("list", 10)
|
182 |
+
names(marginals_fixed_list_N) <- predterms_short
|
183 |
+
|
184 |
+
marginals_fixed_list_E <- vector("list", 10)
|
185 |
+
names(marginals_fixed_list_E) <- predterms_short
|
186 |
+
|
187 |
+
marginals_fixed_list_L1_L2_prop <- vector("list", 10)
|
188 |
+
names(marginals_fixed_list_L1_L2_prop) <- predterms_short
|
189 |
+
|
190 |
+
|
191 |
+
|
192 |
+
|
193 |
+
#summary statistics of random effects
|
194 |
+
summary_random_list_social_L1_nl <- vector("list", 10)
|
195 |
+
names(summary_random_list_social_L1_nl) <- predterms_short
|
196 |
+
|
197 |
+
summary_random_list_social_L2_prop_nl <- vector("list", 10)
|
198 |
+
names(summary_random_list_social_L2_prop_nl) <- predterms_short
|
199 |
+
|
200 |
+
|
201 |
+
coefm <- matrix(NA,10,1)
|
202 |
+
result <- vector("list",10)
|
203 |
+
|
204 |
+
for(i in 1:10){
|
205 |
+
formula <- as.formula(paste("informativity_st ~ ",predterms[[i]]))
|
206 |
+
result[[i]] <- inla(formula, family="gaussian",
|
207 |
+
control.family = list(hyper = pcprior_hyper),
|
208 |
+
#control.inla = list(tolerance = 1e-8, h = 0.0001),
|
209 |
+
#tolerance: the tolerance for the optimisation of the hyperparameters
|
210 |
+
#h: the step-length for the gradient calculations for the hyperparameters.
|
211 |
+
data=metrics_joined, control.compute=list(waic=TRUE))
|
212 |
+
|
213 |
+
coefm[i,1] <- round(result[[i]]$waic$waic, 2)
|
214 |
+
|
215 |
+
intercept_matrix[i, 1:3] <- c(result[[i]]$summary.fixed[1,]$`0.025quant`, result[[i]]$summary.fixed[1,]$`0.5quant`, result[[i]]$summary.fixed[1,]$`0.975quant`)
|
216 |
+
intercept_matrix[i, 4] <- predterms_short[[i]]
|
217 |
+
intercept_matrix[i, 5] <- result[[i]]$waic$waic
|
218 |
+
|
219 |
+
marginals_fixed_list_Intercept[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["(Intercept)"]]))
|
220 |
+
colnames(marginals_fixed_list_Intercept[[i]]) <- c("x for Intercept", "y for Intercept")
|
221 |
+
|
222 |
+
if(i %in% L1_nl_element){
|
223 |
+
social_effects_matrix_L1_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
224 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L1_copy)`,
|
225 |
+
method = "linear") %>%
|
226 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
227 |
+
social_effects_matrix_L1_nl[i, 4] <- predterms_short[[i]]
|
228 |
+
social_effects_matrix_L1_nl[i, 5] <- result[[i]]$waic$waic
|
229 |
+
}
|
230 |
+
|
231 |
+
if(i %in% L2_prop_nl_element){
|
232 |
+
social_effects_matrix_L2_prop_nl[i, 1:3] <- inla.tmarginal(function(x) 1/sqrt(x),
|
233 |
+
result[[i]]$marginals.hyperpar$`Precision for inla.group(L2_copy)`,
|
234 |
+
method = "linear") %>%
|
235 |
+
inla.qmarginal(c(0.025, 0.5, 0.975), .)
|
236 |
+
social_effects_matrix_L2_prop_nl[i, 4] <- predterms_short[[i]]
|
237 |
+
social_effects_matrix_L2_prop_nl[i, 5] <- result[[i]]$waic$waic
|
238 |
+
}
|
239 |
+
|
240 |
+
if(i %in% L1_element) {
|
241 |
+
social_effects_matrix_L1[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log_st",]$`0.025quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.5quant`, result[[i]]$summary.fixed["L1_log_st",]$`0.975quant`)
|
242 |
+
social_effects_matrix_L1[i, 4] <- predterms_short[[i]]
|
243 |
+
social_effects_matrix_L1[i, 5] <- result[[i]]$waic$waic
|
244 |
+
|
245 |
+
marginals_fixed_list_L1[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log_st"]]))
|
246 |
+
colnames(marginals_fixed_list_L1[[i]]) <- c("x for L1", "y for L1")
|
247 |
+
}
|
248 |
+
|
249 |
+
if(i %in% L2_prop_element) {
|
250 |
+
social_effects_matrix_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L2_prop",]$`0.975quant`)
|
251 |
+
social_effects_matrix_L2_prop[i, 4] <- predterms_short[[i]]
|
252 |
+
social_effects_matrix_L2_prop[i, 5] <- result[[i]]$waic$waic
|
253 |
+
|
254 |
+
marginals_fixed_list_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L2_prop"]]))
|
255 |
+
colnames(marginals_fixed_list_L2_prop[[i]]) <- c("x for L2 proportion", "y for L2 proportion")
|
256 |
+
}
|
257 |
+
|
258 |
+
if(i %in% interaction_element) {
|
259 |
+
social_effects_matrix_L1_L2_prop[i, 1:3] <- c(result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.025quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.5quant`, result[[i]]$summary.fixed["L1_log10:L2_prop",]$`0.975quant`)
|
260 |
+
social_effects_matrix_L1_L2_prop[i, 4] <- predterms_short[[i]]
|
261 |
+
social_effects_matrix_L1_L2_prop[i, 5] <- result[[i]]$waic$waic
|
262 |
+
|
263 |
+
marginals_fixed_list_L1_L2_prop[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][["L1_log10:L2_prop"]]))
|
264 |
+
colnames(marginals_fixed_list_L1_L2_prop[[i]]) <- c("x for L1*L2 proportion", "y for L1*L2 proportion")
|
265 |
+
}
|
266 |
+
|
267 |
+
if(i %in% neighbour_element) {
|
268 |
+
social_effects_matrix_N[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
269 |
+
social_effects_matrix_N[i, 4] <- predterms_short[[i]]
|
270 |
+
social_effects_matrix_N[i, 5] <- result[[i]]$waic$waic
|
271 |
+
|
272 |
+
marginals_fixed_list_N[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
273 |
+
colnames(marginals_fixed_list_N[[i]]) <- c("x for Neighbours", "y for Neighbours")
|
274 |
+
}
|
275 |
+
|
276 |
+
if(i %in% official_element) {
|
277 |
+
social_effects_matrix_O[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
278 |
+
social_effects_matrix_O[i, 4] <- predterms_short[[i]]
|
279 |
+
social_effects_matrix_O[i, 5] <- result[[i]]$waic$waic
|
280 |
+
|
281 |
+
marginals_fixed_list_O[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
282 |
+
colnames(marginals_fixed_list_O[[i]]) <- c("x for Official", "y for Official")
|
283 |
+
}
|
284 |
+
|
285 |
+
if(i %in% education_element) {
|
286 |
+
social_effects_matrix_E[i, 1:3] <- c(result[[i]]$summary.fixed[2,]$`0.025quant`, result[[i]]$summary.fixed[2,]$`0.5quant`, result[[i]]$summary.fixed[2,]$`0.975quant`)
|
287 |
+
social_effects_matrix_E[i, 4] <- predterms_short[[i]]
|
288 |
+
social_effects_matrix_E[i, 5] <- result[[i]]$waic$waic
|
289 |
+
|
290 |
+
marginals_fixed_list_E[[i]] <- as.data.frame(cbind(result[[i]][["marginals.fixed"]][[2]]))
|
291 |
+
colnames(marginals_fixed_list_E[[i]]) <- c("x for Education", "y for Education")
|
292 |
+
}
|
293 |
+
|
294 |
+
fitted_list[[i]] <- result[[i]]$summary.fitted.values
|
295 |
+
fitted_list[[i]] <- fitted_list[[i]] %>%
|
296 |
+
mutate(across(where(is.numeric), round, 2))
|
297 |
+
|
298 |
+
marginals_hyperpar_list_gaussian[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for the Gaussian observations"]]))
|
299 |
+
colnames(marginals_hyperpar_list_gaussian[[i]]) <- c("x for the Gaussian observations", "y for the Gaussian observations")
|
300 |
+
|
301 |
+
if(i %in% L1_nl_element){
|
302 |
+
marginals_hyperpar_list_social_L1_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L1_copy)"]]))
|
303 |
+
colnames(marginals_hyperpar_list_social_L1_nl[[i]]) <- c("x for inla.group(L1_copy)", "y for inla.group(L1_copy)")
|
304 |
+
}
|
305 |
+
|
306 |
+
if(i %in% L2_prop_nl_element){
|
307 |
+
marginals_hyperpar_list_social_L2_prop_nl[[i]] <- as.data.frame(cbind(result[[i]]$marginals.hyperpar[["Precision for inla.group(L2_copy)"]]))
|
308 |
+
colnames(marginals_hyperpar_list_social_L2_prop_nl[[i]]) <- c("x for inla.group(L2_copy)", "y for inla.group(L2_copy)")
|
309 |
+
}
|
310 |
+
}
|
311 |
+
|
312 |
+
#beepr::beep(5)
|
313 |
+
|
314 |
+
save(result, file = "output_models/models_Informativity_social_only.RData")
|
315 |
+
|
316 |
+
|
317 |
+
coefm <- as.data.frame(cbind(predterms_short, coefm))
|
318 |
+
colnames(coefm) <- c("model", "WAIC")
|
319 |
+
coefm <- coefm %>%
|
320 |
+
mutate(across(.cols=2, as.numeric)) %>%
|
321 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
322 |
+
arrange(WAIC)
|
323 |
+
|
324 |
+
coefm$WAIC <- as.numeric(coefm$WAIC)
|
325 |
+
coefm <- coefm[order(coefm$WAIC),]
|
326 |
+
|
327 |
+
coefm_path <- paste("output_tables/", "waics", "Informativity_social_only_models", ".csv", collapse = "")
|
328 |
+
write.csv(coefm, coefm_path, row.names=FALSE)
|
329 |
+
|
330 |
+
for (i in 1:length(fitted_list)) {
|
331 |
+
fitted_list[[i]]$model <- names(fitted_list)[i]
|
332 |
+
}
|
333 |
+
fitted_list <- dplyr::bind_rows(fitted_list)
|
334 |
+
fitted_list_path <- paste("output_tables/", "fitted_list", "Informativity_social_only_models", ".csv", collapse = "")
|
335 |
+
write.csv(fitted_list, fitted_list_path)
|
336 |
+
|
337 |
+
intercept_effects <- as.data.frame(intercept_matrix)
|
338 |
+
L1_effects <- as.data.frame(social_effects_matrix_L1)
|
339 |
+
L1_nl_effects <- as.data.frame(social_effects_matrix_L1_nl)
|
340 |
+
L2_prop_effects <- as.data.frame(social_effects_matrix_L2_prop)
|
341 |
+
L2_prop_nl_effects <- as.data.frame(social_effects_matrix_L2_prop_nl)
|
342 |
+
N_effects<-as.data.frame(social_effects_matrix_N)
|
343 |
+
E_effects<-as.data.frame(social_effects_matrix_E)
|
344 |
+
O_effects<-as.data.frame(social_effects_matrix_O)
|
345 |
+
interaction_effects <- as.data.frame(social_effects_matrix_L1_L2_prop)
|
346 |
+
|
347 |
+
intercept_effects$effect <- "Intercept"
|
348 |
+
L1_effects$effect <- "L1"
|
349 |
+
L1_nl_effects$effect <- "social SD:\nL1"
|
350 |
+
L2_prop_effects$effect <- "L2 proportion"
|
351 |
+
L2_prop_nl_effects$effect <- "social SD:\nL2 proportion"
|
352 |
+
N_effects$effect <- "Neighbours"
|
353 |
+
E_effects$effect <- "Education"
|
354 |
+
O_effects$effect <- "Official status"
|
355 |
+
interaction_effects$effect <- "L1*L2 proportion"
|
356 |
+
|
357 |
+
effs <- as.data.frame(rbind(intercept_effects, L1_effects, L1_nl_effects, L2_prop_effects, L2_prop_nl_effects, N_effects, O_effects, E_effects, interaction_effects))
|
358 |
+
effs <- effs %>%
|
359 |
+
mutate(across(.cols=c(1:3, 5), as.numeric)) %>%
|
360 |
+
mutate(across(where(is.numeric), round, 2)) %>%
|
361 |
+
na.omit() %>%
|
362 |
+
arrange(WAIC) %>%
|
363 |
+
relocate(model)
|
364 |
+
|
365 |
+
effs_path <- paste("output_tables/", "effects", "Informativity_social_only_models", ".csv", collapse = "")
|
366 |
+
write.csv(effs, effs_path, row.names=FALSE)
|
367 |
+
|
368 |
+
effs <- read.csv("output_tables/ effects Informativity_social_only_models .csv")
|
369 |
+
|
370 |
+
effs_table_SM <- effs %>%
|
371 |
+
rename("2.5%"=2,
|
372 |
+
"50%" = 4,
|
373 |
+
"97.5%" = 3) %>%
|
374 |
+
flextable() %>%
|
375 |
+
autofit() %>%
|
376 |
+
merge_v(j=c("model", "WAIC")) %>%
|
377 |
+
fix_border_issues() %>%
|
378 |
+
border_inner_h()
|
379 |
+
|
380 |
+
save_as_docx(
|
381 |
+
"Effects in informativity models with fixed and random effects (including non-linear implementations of some fixed effects)" = effs_table_SM,
|
382 |
+
path = "output_tables/table_SM_effects_Informativity_social_only_models.docx")
|
383 |
+
|
384 |
+
effs_table_Main <- effs %>%
|
385 |
+
rename("2.5%"=2,
|
386 |
+
"50%" = 4,
|
387 |
+
"97.5%" = 3) %>%
|
388 |
+
filter(!grepl("nonlinear", model))
|
389 |
+
|
390 |
+
effs_table_Main$model <- gsub("(\\s*\\(\\w+\\))", "", effs_table_Main$model)
|
391 |
+
|
392 |
+
effs_table_Main <- effs_table_Main %>%
|
393 |
+
flextable() %>%
|
394 |
+
autofit() %>%
|
395 |
+
merge_v(j=c("model", "WAIC")) %>%
|
396 |
+
fix_border_issues() %>%
|
397 |
+
border_inner_h()
|
398 |
+
|
399 |
+
save_as_docx(
|
400 |
+
"Effects in informativity models with fixed and random effects" = effs_table_Main,
|
401 |
+
path = "output_tables/table_Main_effects_Informativity_social_only_models.docx")
|
402 |
+
|
403 |
+
|
404 |
+
effs_plot <- effs %>%
|
405 |
+
#filter(WAIC <= top_9) %>%
|
406 |
+
rename(lower=2,
|
407 |
+
upper = 4,
|
408 |
+
mean = 3) %>% #mean here refers to 0.5 quantile
|
409 |
+
#filter(!effect == "Intercept") %>%
|
410 |
+
mutate(effect = factor(effect, levels=c("Intercept", "social SD:\nL1", "L1", "social SD:\nL2 proportion", "L2 proportion", "Neighbours", "Education", "Official status", "L1*L2 proportion"))) %>%
|
411 |
+
mutate(WAIC = round(WAIC, 2)) %>%
|
412 |
+
unite("model", model, WAIC, sep = ",\nWAIC: ", remove=FALSE) %>%
|
413 |
+
group_by(WAIC) %>%
|
414 |
+
arrange(WAIC) %>%
|
415 |
+
mutate(model = forcats::fct_reorder(as.factor(model), WAIC)) %>% #reordering levels within model based on WAIC values
|
416 |
+
mutate(model = factor(model, levels=rev(levels(model)))) #reversing the order
|
417 |
+
|
418 |
+
|
419 |
+
#plot modified from function ggregplot::Efxplot
|
420 |
+
cols = c(brewer.pal(12, "Paired"))
|
421 |
+
cols = c("gray50", cols[c(1:8)])
|
422 |
+
|
423 |
+
show_col(cols)
|
424 |
+
|
425 |
+
plot_1 <- ggplot(effs_plot,
|
426 |
+
aes(y = as.factor(model),
|
427 |
+
x = mean,
|
428 |
+
group = effect,
|
429 |
+
colour = effect)) +
|
430 |
+
geom_pointrangeh(aes(xmin = lower, xmax = upper), position = position_dodge(w = 0.9), size = 1.5) +
|
431 |
+
geom_vline(aes(xintercept = 0),lty = 2) + labs(x = NULL) + #coord_flip() +
|
432 |
+
scale_color_manual(values=cols) +
|
433 |
+
ylab("Model of informativity") + xlab("Estimate") + labs(color = "Effect") + theme_classic() +
|
434 |
+
theme(axis.text=element_text(size=50),
|
435 |
+
legend.text=element_text(size=50),
|
436 |
+
axis.title=element_text(size=50),
|
437 |
+
legend.title=element_text(size=50),
|
438 |
+
legend.spacing.y = unit(1.5, 'cm')) +
|
439 |
+
guides(color = guide_legend(reverse = TRUE, byrow = TRUE))
|
440 |
+
|
441 |
+
|
442 |
+
|
443 |
+
#plot_1
|
444 |
+
ggsave(filename = 'output/SP_models_plot_Informativity_social_only_models.jpg',
|
445 |
+
plot_1, height = 20, width = 45)
|
446 |
+
|
447 |
+
|
448 |
+
#saving hyperparameters: Gaussian observations
|
449 |
+
for (i in 1:length(marginals_hyperpar_list_gaussian)) {
|
450 |
+
marginals_hyperpar_list_gaussian[[i]]$model <- names(marginals_hyperpar_list_gaussian)[i]
|
451 |
+
}
|
452 |
+
marginals_hyperpar_list_gaussian <- dplyr::bind_rows(marginals_hyperpar_list_gaussian)
|
453 |
+
|
454 |
+
write.csv(marginals_hyperpar_list_gaussian, "output_tables/Informativity_social_only_models_marginals_hyperpar_gaussian.csv")
|
101/replication_package/output/Bound_morph/bound_morph_score.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ecd0d3d93eb4ab4d6e6878d47926bfdf88d788d4d95405bb7afc948079ca2951
|
3 |
+
size 69462
|
101/replication_package/output/Informativity/informativity_score.tsv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:118f1ae7e51f874190a34faeef6a146e728c4cfeb200d6aa46f1d638a6c33e16
|
3 |
+
size 69173
|
101/replication_package/output_tables/ effects Boundness_phylogenetic_spatial_models .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cfed9a25867240132c4fe5904e664139fbd556356b471fa8b4eab904770d5f93
|
3 |
+
size 1024
|
101/replication_package/output_tables/ effects Boundness_social_models .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bee79ae8c8236dd59b5d42bad83276e804026345fb63236d437f28a303c84ce5
|
3 |
+
size 3976
|
101/replication_package/output_tables/ effects Boundness_social_models prior_0.01 .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f2e7d56a2f09b6efd8ffd98fa108ff67b2f204830629595847a27aea81149939
|
3 |
+
size 3959
|
101/replication_package/output_tables/ effects Boundness_social_models prior_0.5 .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:19e65e55557d35a540bb0b12107b4b4e1eeb81a7fda23a566493687a8476fead
|
3 |
+
size 3966
|
101/replication_package/output_tables/ effects Boundness_social_models prior_0.99 .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d25006009459bee25ec627686ed01fd8a872b8e05311a17995cdc2bd341ade26
|
3 |
+
size 3964
|
101/replication_package/output_tables/ effects Boundness_social_only_models .csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bc3187df105f34b9934146aa4f8e2d4d067be5046b32843981bb2f0db02b3048
|
3 |
+
size 1503
|