1
1
# Part of the rstanarm package for estimating model parameters
2
2
# Copyright (C) 2015, 2016, 2017 Trustees of Columbia University
3
- #
3
+ #
4
4
# This program is free software; you can redistribute it and/or
5
5
# modify it under the terms of the GNU General Public License
6
6
# as published by the Free Software Foundation; either version 3
7
7
# of the License, or (at your option) any later version.
8
- #
8
+ #
9
9
# This program is distributed in the hope that it will be useful,
10
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
12
# GNU General Public License for more details.
13
- #
13
+ #
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -24,7 +24,7 @@ ITER <- 400
24
24
threshold <- 0.21
25
25
REFRESH <- 0
26
26
27
- SW(fit <- stan_lm(mpg ~ . , data = mtcars , prior = R2(location = 0.75 ),
27
+ SW(fit <- stan_lm(mpg ~ . , data = mtcars , prior = R2(location = 0.75 ),
28
28
chains = CHAINS , iter = ITER , seed = SEED , refresh = 0 ))
29
29
30
30
@@ -36,12 +36,12 @@ test_that("stan_aov returns expected result for npk example", {
36
36
K = " contr.poly"
37
37
)
38
38
SW(fit <- stan_aov(yield ~ block + N * P * K , data = npk , contrasts = contrasts_list ,
39
- prior = R2(0.5 ), chains = CHAINS , iter = ITER , seed = SEED ,
39
+ prior = R2(0.5 ), chains = CHAINS , iter = ITER , seed = SEED ,
40
40
refresh = 0 ))
41
41
expect_stanreg(fit )
42
-
42
+
43
43
fit_sigma <- fit $ stan_summary [" sigma" , " mean" ]
44
- lm_sigma <- summary(lm(yield ~ block + N * P * K , data = npk ,
44
+ lm_sigma <- summary(lm(yield ~ block + N * P * K , data = npk ,
45
45
contrasts = contrasts_list ))$ sigma
46
46
expect_equal(fit_sigma , lm_sigma , tol = threshold )
47
47
expect_output(print(fit ), regexp = " stan_aov" )
@@ -70,27 +70,27 @@ test_that("stan_biglm returns expected result", {
70
70
y <- mtcars $ mpg
71
71
ybar <- mean(y )
72
72
s_y <- sd(y )
73
- SW(post <- stan_biglm(biglm , xbar , ybar , s_y , prior = R2(0.5 ),
73
+ SW(post <- stan_biglm(biglm , xbar , ybar , s_y , prior = R2(0.5 ),
74
74
chains = CHAINS , iter = ITER , seed = SEED , refresh = 0 ))
75
- expect_equal(coef(lm(mpg ~ wt + qsec + am , data = mtcars )),
75
+ expect_equal(coef(lm(mpg ~ wt + qsec + am , data = mtcars )),
76
76
rstan :: summary(post )$ summary [1 : 4 , " mean" ], tol = threshold )
77
77
})
78
78
79
79
test_that(" stan_lm returns expected result for mtcars example" , {
80
80
# example using mtcars dataset
81
81
expect_stanreg(fit )
82
-
82
+
83
83
fit_sigma <- fit $ stan_summary [" sigma" , " mean" ]
84
84
lm_sigma <- summary(lm(mpg ~ . , data = mtcars ))$ sigma
85
85
expect_equal(fit_sigma , lm_sigma , tol = threshold )
86
86
})
87
87
test_that(" stan_lm returns expected result for trees example" , {
88
88
# example using trees dataset
89
- SW(fit <- stan_lm(log(Volume ) ~ log(Girth ) + log(Height ), data = trees ,
89
+ SW(fit <- stan_lm(log(Volume ) ~ log(Girth ) + log(Height ), data = trees ,
90
90
prior = R2(location = 0.9 , what = " mean" ), refresh = 0 ,
91
91
chains = CHAINS , iter = ITER , seed = SEED , adapt_delta = 0.999 ))
92
92
expect_stanreg(fit )
93
-
93
+
94
94
fit_sigma <- fit $ stan_summary [" sigma" , " mean" ]
95
95
lm_sigma <- summary(lm(log(Volume ) ~ log(Girth ) + log(Height ),data = trees ))$ sigma
96
96
expect_equal(fit_sigma , lm_sigma , tol = threshold )
@@ -101,7 +101,7 @@ test_that("stan_lm doesn't break with less common priors", {
101
101
SW(fit <- stan_lm(mpg ~ - 1 + . , data = mtcars , prior = NULL ,
102
102
iter = 10 , chains = 1 , seed = SEED , refresh = 0 ))
103
103
expect_stanreg(fit )
104
-
104
+
105
105
# prior_intercept = normal()
106
106
SW(fit <- stan_lm(mpg ~ . , data = mtcars , refresh = 0 ,
107
107
prior = R2(0.75 ), prior_intercept = normal(),
@@ -110,34 +110,34 @@ test_that("stan_lm doesn't break with less common priors", {
110
110
})
111
111
112
112
test_that(" stan_lm doesn't break with vb algorithms" , {
113
- SW(fit <- stan_lm(mpg ~ . , data = mtcars ,
113
+ SW(fit <- stan_lm(mpg ~ . , data = mtcars ,
114
114
prior = R2(location = 0.75 ), refresh = 0 ,
115
115
algorithm = " meanfield" , seed = SEED ))
116
116
expect_stanreg(fit )
117
-
117
+
118
118
SW(fit2 <- update(fit , algorithm = " fullrank" ))
119
119
expect_stanreg(fit2 )
120
120
})
121
121
122
122
test_that(" stan_lm throws error if only intercept" , {
123
- expect_error(stan_lm(mpg ~ 1 , data = mtcars , prior = R2(location = 0.75 )),
123
+ expect_error(stan_lm(mpg ~ 1 , data = mtcars , prior = R2(location = 0.75 )),
124
124
regexp = " not suitable for estimating a mean" )
125
125
})
126
126
127
127
test_that(" stan_lm throws error if 'location' is a vector" , {
128
- expect_error(stan_lm(mpg ~ . , data = mtcars , prior = R2(location = c(0.25 , 0.5 ))),
128
+ expect_error(stan_lm(mpg ~ . , data = mtcars , prior = R2(location = c(0.25 , 0.5 ))),
129
129
regexp = " only accepts a single value for 'location'" )
130
130
})
131
131
132
132
test_that(" stan_lm throws error if N < K" , {
133
133
# NOTE: remove this test once N < K is enabled
134
- expect_error(stan_lm(mpg ~ . , data = mtcars [1 : 5 , ], prior = R2(0.75 )),
134
+ expect_error(stan_lm(mpg ~ . , data = mtcars [1 : 5 , ], prior = R2(0.75 )),
135
135
regexp = " more predictors than data points is not yet enabled" )
136
136
})
137
137
138
138
test_that(" stan_lm throws error if glmer syntax used" , {
139
- expect_error(stan_lm(mpg ~ wt + (1 | cyl ), data = mtcars ,
140
- prior = R2(0.5 , " mean" )),
139
+ expect_error(stan_lm(mpg ~ wt + (1 | cyl ), data = mtcars ,
140
+ prior = R2(0.5 , " mean" )),
141
141
regexp = " model formula not allowed" )
142
142
})
143
143
@@ -149,6 +149,7 @@ test_that("loo/waic for stan_lm works", {
149
149
})
150
150
151
151
test_that(" posterior_predict compatible with stan_lm" , {
152
+ skip_on_os(" mac" )
152
153
check_for_pp_errors(fit )
153
154
expect_linpred_equal(fit )
154
155
})
0 commit comments