forked from jslefche/piecewiseSEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.R
129 lines (100 loc) · 2.62 KB
/
build.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
#####-----Build script
# generate documentation
roxygen2::roxygenise()
# build documentation and vignettes
document(piecewiseSEM)
#clean_vignettes(piecewiseSEM)
#build_vignettes(piecewiseSEM)
# create website for package
pkgdown::build_site()
# build package
piecewiseSEM <- as.package("./piecewiseSEM")
devtools::use_build_ignore(c("build.R", ".git", ".gitignore", "docs"),
pkg = "./piecewiseSEM")
# Load package
load_all(piecewiseSEM, reset = T)
### Check and build
check(piecewiseSEM, cran=TRUE)
build(piecewiseSEM, path="./piecewiseSEM/builds")
devtools::check_built("./piecewiseSEM_2.0.tar.gz")
install(piecewiseSEM)
#run_examples(piecewiseSEM)
### Build pkgdown site
library(pkgdown)
setwd("piecewiseSEM")
build_site()
#####-----Examples to verify functionality
library(piecewiseSEM)
data(keeley)
# fit model
mod <- psem(
lm(rich ~ cover, data=keeley),
lm(cover ~ firesev, data=keeley),
lm(firesev ~ age, data=keeley),
data = keeley)
mod
# d-sep tests
basisSet(mod)
dSep(mod)
fisherC(mod)
AIC(mod, AIC.type = "dsep")
# Chisq
LLchisq(mod)
AIC(mode, AIC.type = "loglik")
# Rsquared
rsquared(mod)
# get summary
summary(mod)
# get residuals
residuals(mod)
# plotting
plot(mod)
plot(mod, node_attrs = list(
shape = "rectangle", color = "black",
fillcolor = "orange", x = 3, y=1:4))
# add correlated error
mod2 <- psem(
lm(rich ~ cover, data=keeley),
lm(cover ~ firesev + age, data=keeley),
lm(firesev ~ age, data=keeley),
rich %~~% firesev,
data = keeley)
# get summary
summary(mod2)
# AIC of two models
anova(mod, mod2)
# test mixed models
library(lme4)
library(nlme)
# using lme
shipley_psem <- psem(
lme(DD ~ lat, random = ~ 1 | site / tree, na.action = na.omit,
data = shipley),
lme(Date ~ DD, random = ~ 1 | site / tree, na.action = na.omit,
data = shipley),
lme(Growth ~ Date, random = ~ 1 | site / tree, na.action = na.omit,
data = shipley),
glmmPQL(Live ~ Growth + (1 | site) + (1 | tree),
family = binomial(link = "logit"), data = shipley))
summary(shipley_psem)
# using lme4
shipley_psem_lme4 <- psem(
lmer(DD ~ lat + (1 | site / tree),
data = shipley),
lmer(Date ~ DD + (1 | site / tree),
data = shipley),
lmer(Growth ~ Date + (1 | site / tree),
data = shipley),
glmer(Live ~ Growth + (1 | site) + (1 | tree),
family = binomial(link = "logit"), data = shipley),
data = shipley)
summary(shipley_psem_lme4)
# multigroup
data(meadows)
meadows$grazed <- factor(meadows$grazed)
meadow_mod <- psem(
lm(mass ~ elev, data = meadows),
lm(rich ~ elev + mass, data = meadows),
data = meadows
)
multigroup(meadow_mod, group = "grazed")