aboutsummaryrefslogtreecommitdiff
path: root/tests/testthat/test_plot_marginal.R
blob: f80f832a3fd7de610df9775d1cfcf4a0d7044bbb (plain)
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
context("Marginal effect plots")

on_mac <- tolower(Sys.info()[["sysname"]]) == "darwin"
on_cran <- !identical(Sys.getenv("NOT_CRAN"), "true")
skip_if(on_mac & on_cran, "On CRAN mac")

#Setup umf
set.seed(123)
sc <- data.frame(x1=rnorm(3), x2=factor(c("a","b","b")))
oc <- data.frame(x3=rnorm(9))
umf <- unmarkedFrameOccu(y=matrix(c(1,0,0,1,1,0,0,1,0), nrow=3),
        siteCovs=sc, obsCovs=oc)
#Fit model
good_fit <- TRUE
tryCatch({
fit <- suppressWarnings(stan_occu(~x3~x1+x2, umf,
                                  chains=2, iter=40, refresh=0))
fit2 <- suppressWarnings(stan_occu(~1~1, umf,
                                  chains=2, iter=40, refresh=0))
}, error=function(e){
  good_fit <<- FALSE
})
skip_if(!good_fit, "Test setup failed")

test_that("plot_marginal creates gg or grid object",{
  #Multiple covariates
  pdf(NULL)
  mp <- plot_marginal(fit, "state")
  expect_is(mp, "gtable")
  #Submodel with single covariate
  mp2 <- plot_marginal(fit, "det")
  expect_is(mp2, "gg")
  #Specific covariate
  mp3 <- plot_marginal(fit, "state", "x1")
  expect_is(mp3, "gg")
  #No covariates
  expect_error(plot_marginal(fit2, "state"))
  dev.off()
})

test_that("marginal_covariate_plot builds plot for either cov type",{
  pdf(NULL)
  mcp <- marginal_covariate_plot(fit,"state","x1", draws=100)
  expect_is(mcp, "gg")
  mcp2 <- marginal_covariate_plot(fit,"state","x2", draws=100)
  expect_is(mcp, "gg")
  #Covariate not in submodel
  expect_error(marginal_covariate_plot(fit,"state","fake"))
  dev.off()
})

test_that("marg_numeric_plot builds plot for numeric covariate",{
  pdf(NULL)
  mnp <- marg_numeric_plot(fit, "state", "x1", c(0.025,0.975), draws=100)
  expect_is(mnp, "gg")
  expect_error(marg_numeric_plot(fit,"state","x2",c(0.025,0.975)), draws=100)
  dev.off()
})

test_that("marg_factor_plot builds plot for factor covariate",{
  pdf(NULL)
  mfp <- marg_factor_plot(fit, "state", "x2", c(0.025,0.975), draws=100)
  expect_is(mfp, "gg")
  expect_error(marg_factor_plot(fit,"state","x1",c(0.025,0.975), draws=100))
  dev.off()
})

test_that("get_baseline_df gets baseline data frame for plot data",{
  base_df <- get_baseline_df(fit["state"])
  expect_equal(base_df, data.frame(x1=-0.2301775,
                                   x2=factor("a",levels=c("a","b"))), tol=1e-6)
})

test_that("col_is_factor identifies factor column",{
  expect_true(col_is_factor("x2", sc))
  expect_false(col_is_factor("x1", sc))
})

test_that("Marginal plot data is generated correctly",{
  nd <- data.frame(x1=c(-1,1), x2=c("a","b"))

  #Continuous
  mdata <- get_margplot_data(fit, "state", "x1", c(0.025,0.975),
                             samples=1:3, nd)
  expect_equal(dim(mdata), c(2,4))
  expect_is(mdata, "data.frame")
  expect_equal(names(mdata), c("covariate","mn","lower","upper"))

  #Factor
  mdata2 <- get_margplot_data(fit, "state", "x2", c(0.025,0.975),
                              samples=1:3, nd)
  expect_equal(dim(mdata), c(2,4))
  expect_is(mdata2, "data.frame")
  expect_equal(names(mdata2), c("covariate","mn","lower","upper"))
})