File size: 4,346 Bytes
2f8aa81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
source("install_and_load_INLA.R")

#parameters
kappa = 1
phi_1 = c(1, 1.25) # "Local" version: (sigma, phi) First value is not used

WALS <- read_csv("data/complexity_data_WALS.csv") %>%
  dplyr::select("Name" = lang, roundComp, logpop2, "ISO_639" = silCode) %>%
  dplyr::mutate(ISO_639 = str_to_lower(ISO_639))

min_val <- min(WALS$roundComp)
max_val <- max(WALS$roundComp)

# Perform the rescaling
WALS$roundComp <- (WALS$roundComp - min_val) / (max_val - min_val)

pop_file_fn <-
  "data_wrangling/ethnologue_pop_SM_morph_compl_reanalysis.tsv"
L1 <-
  read_tsv(pop_file_fn, show_col_types = F) %>% dplyr::select(ISO_639, L1_log10_scaled)

glottolog_df <-
  read_tsv("data_wrangling/glottolog_cldf_wide_df.tsv", col_types = cols()) %>%
  dplyr::select(
    Glottocode,
    Language_ID,
    "ISO_639" = ISO639P3code,
    Language_level_ID,
    level,
    Family_ID,
    Longitude,
    Latitude
  ) %>%
  mutate(Language_level_ID = if_else(is.na(Language_level_ID), Glottocode, Language_level_ID))  %>%
  mutate(Family_ID = ifelse(is.na(Family_ID), Language_level_ID, Family_ID)) %>%
  dplyr::select(
    Glottocode,
    Language_ID,
    ISO_639,
    Language_level_ID,
    level,
    Family_ID,
    Longitude,
    Latitude
  )

WALS_df <- WALS %>%
  inner_join(L1,
             by = c("ISO_639")) %>%
  inner_join(glottolog_df, by = "ISO_639") %>%
  filter(!is.na(Latitude), !is.na(Longitude)) %>%
  dplyr::select(Language_ID = Glottocode,
                Name,
                roundComp,
                ISO_639,
                L1_log10_scaled,
                Longitude,
                Latitude)



tree <- read.tree(file.path("data_wrangling/wrangled.tree"))

#dropping tips not in Grambank
WALS_df <- WALS_df[WALS_df$Language_ID %in% tree$tip.label,]
tree <- keep.tip(tree, WALS_df$Language_ID)

x <-
  assert_that(all(tree$tip.label %in% WALS_df$Language_ID), msg = "The data and phylogeny taxa do not match")

## Building standardized phylogenetic precision matrix
tree_scaled <- tree

tree_vcv = vcv.phylo(tree_scaled)
typical_phylogenetic_variance = exp(mean(log(diag(tree_vcv))))

#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
tree_scaled$edge.length <-
  tree_scaled$edge.length / typical_phylogenetic_variance
phylo_prec_mat <- MCMCglmm::inverseA(tree_scaled,
                                     nodes = "ALL",
                                     scale = FALSE)$Ainv

WALS_df = WALS_df[order(match(WALS_df$Language_ID, rownames(phylo_prec_mat))), ]

#"local" set of parameters
## Create spatial covariance matrix using the matern covariance function
spatial_covar_mat_1 = varcov.spatial(WALS_df[, c("Longitude", "Latitude")],
                                     cov.pars = phi_1, kappa = kappa)$varcov
# Calculate and standardize by the typical variance
typical_variance_spatial_1 = exp(mean(log(diag(
  spatial_covar_mat_1
))))
spatial_cov_std_1 = spatial_covar_mat_1 / typical_variance_spatial_1
spatial_prec_mat_1 = solve(spatial_cov_std_1)
dimnames(spatial_prec_mat_1) = list(WALS_df$Language_ID, WALS_df$Language_ID)

## Since we are using a sparse phylogenetic matrix, we are matching taxa to rows in the matrix
phy_id = match(tree$tip.label, rownames(phylo_prec_mat))
WALS_df$phy_id = phy_id

## Other effects are in the same order they appear in the dataset
WALS_df$sp_id = 1:nrow(spatial_prec_mat_1)


formula <- as.formula(
  paste(
    "roundComp ~",
    "L1_log10_scaled +",
    "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)"
  )
)

result <- inla(
  formula,
  family = "gaussian",
  data = WALS_df,
  control.compute = list(waic = TRUE)
)
summary(result)

save(result, file = "output_models/model_WALS_controlled.RData")

social_effects_controlled <-
  c("morphological complexity ~ L1 + phylogenetic effect + spatial effect",
    round(
      c(
        result$summary.fixed[2,]$`0.025quant`,
        result$summary.fixed[2,]$`0.5quant`,
        result$summary.fixed[2,]$`0.975quant`,
        nrow(WALS_df)
      ),
      2
    ), "default (~10%)")

save(social_effects_controlled, file = "output_models/social_effects_controlled.RData")