Retrieve data.frame used to fit models via bnec, or directly from a bayesnecformula. formula should be of class bayesnecfit, bayesmanecfit or bayesnecformula.

# S3 method for bayesnecfit
model.frame(formula, ...)

# S3 method for bayesmanecfit
model.frame(formula, model, ...)

# S3 method for bayesnecformula
model.frame(formula, data, ...)

Arguments

formula

An model object of class bayesnecfit, bayesmanecfit, or a formula of class bayesnecformula.

...

Unused if formula is a bayesnecfit or a bayesmanecfit. Else, if formula is a bayesnecformula, additional arguments to be passed to check_formula.

model

A valid model string.

data

A data.frame containing the variables specified in formula.

Value

If formula is a bayesnecfit or a bayesmanecfit, a data.frame containing the data used to fit the model.

If, instead, formula is a bayesnecformula, a data.frame with additional attributes detailing the population-level variables (attribute "bnec_pop") (response y, predictor x, and, if binomial a formula, trials) and, if applicable, the group-level variables (attribute "bnec_group").

Details

If formula is a bayesnecformula and it contains transformations to variables x and y, these are evaluated and returned as part of the data.frame.

Examples

# \donttest{
library(bayesnec)
# if input is of class `bayesnecfit` or `bayesmanecfit`
model.frame(manec_example, model = "nec4param")
#>               y          x
#> 1    1.81753716 1.01874617
#> 2    2.31660095 0.81574746
#> 3    1.72290850 0.37133055
#> 4    2.47827471 0.40083228
#> 5    1.86355409 1.29454513
#> 6    2.39344847 1.38979430
#> 7    2.44867487 0.20807618
#> 8    1.53236967 0.63632398
#> 9    2.68271344 0.62667268
#> 10   2.75545171 0.74352161
#> 11  -0.72165471 2.10177950
#> 12   0.74223372 1.75578151
#> 13   1.92652136 0.76176644
#> 14  -0.24585004 1.98744470
#> 15   0.76099900 1.74139013
#> 16   2.15101769 1.08934727
#> 17   2.69104590 0.04505614
#> 18   2.49637234 0.80484962
#> 19   0.19280001 1.92552126
#> 20   2.62941229 1.48297852
#> 21   1.91996822 0.40368936
#> 22   2.20932082 1.18528684
#> 23   2.10261674 0.32513406
#> 24   1.67257283 1.11906119
#> 25   2.42730928 0.26519802
#> 26   1.76470022 0.62633844
#> 27   2.23128899 0.31244457
#> 28   1.71546355 0.12784117
#> 29   1.96525963 0.89823899
#> 30   1.66894887 0.74621947
#> 31   3.00870633 0.85374045
#> 32   2.43285909 0.92205393
#> 33  -0.48778119 1.96856634
#> 34   1.84597419 1.18492596
#> 35   1.92807736 0.37994358
#> 36   1.74175047 0.43551436
#> 37   2.35658367 1.36208723
#> 38   2.48674416 0.75908675
#> 39   2.45232814 0.67545599
#> 40   2.63202300 0.34843701
#> 41  -0.75737944 2.08655140
#> 42   1.66952275 0.23745512
#> 43   1.44637922 0.17133746
#> 44   0.51525902 1.83447390
#> 45   2.29255167 0.03234801
#> 46   2.70966110 0.97118466
#> 47   2.37563422 1.23252515
#> 48   2.16232277 0.69879132
#> 49   2.92560473 0.32238542
#> 50   0.98056568 1.65522764
#> 51   1.95465732 0.59936245
#> 52   2.01082448 0.66544343
#> 53  -0.85674115 2.36795395
#> 54  -6.90675478 3.13776710
#> 55   1.72611555 1.50581926
#> 56   0.02076562 1.78634238
#> 57   2.41890914 0.09778806
#> 58   2.22724962 1.53289699
#> 59   1.67129635 0.35410575
#> 60   1.55513976 1.29098749
#> 61   2.44416405 0.23759447
#> 62   1.89439817 0.54382372
#> 63   2.37042869 0.16967735
#> 64   1.83967579 1.34011564
#> 65  -0.47439313 2.06637640
#> 66  -0.89239448 2.21612584
#> 67   0.79149428 1.73569066
#> 68   1.58836486 0.51879138
#> 69   1.57267826 1.56274476
#> 70   1.59831797 0.24631971
#> 71   1.69118540 1.38092221
#> 72   1.94552283 0.43042725
#> 73   2.18164978 0.04844550
#> 74   2.70687710 0.78149645
#> 75   1.88646056 0.48993624
#> 76  -0.42290590 2.17270628
#> 77   2.22143943 0.47982702
#> 78   1.98709144 0.56961218
#> 79   2.63543591 0.05163864
#> 80  -1.30724886 2.52258634
#> 81   1.42229818 1.59282805
#> 82   2.43861218 0.77733849
#> 83   0.45069003 1.71289428
#> 84   0.64790451 1.71660083
#> 85   2.42482651 1.44024186
#> 86   3.89182030 1.15883062
#> 87   0.87668024 1.65976414
#> 88  -3.27854955 3.22051966
#> 89   2.46633324 0.18394507
#> 90   2.13714590 0.92604417
#> 91   2.16738296 0.58364533
#> 92   2.65975450 0.80851766
#> 93   1.84621881 1.06954478
#> 94  -0.86117420 2.15534832
#> 95   0.96084129 1.59495735
#> 96   1.98523295 0.41964511
#> 97   1.51921728 0.60667725
#> 98  -0.15391721 1.89292590
#> 99   1.70259760 1.14816796
#> 100 -1.00846203 2.22702839
nec4param <- pull_out(manec_example, "nec4param")
#> Pulling out model(s): nec4param
model.frame(nec4param)
#>               y          x
#> 1    1.81753716 1.01874617
#> 2    2.31660095 0.81574746
#> 3    1.72290850 0.37133055
#> 4    2.47827471 0.40083228
#> 5    1.86355409 1.29454513
#> 6    2.39344847 1.38979430
#> 7    2.44867487 0.20807618
#> 8    1.53236967 0.63632398
#> 9    2.68271344 0.62667268
#> 10   2.75545171 0.74352161
#> 11  -0.72165471 2.10177950
#> 12   0.74223372 1.75578151
#> 13   1.92652136 0.76176644
#> 14  -0.24585004 1.98744470
#> 15   0.76099900 1.74139013
#> 16   2.15101769 1.08934727
#> 17   2.69104590 0.04505614
#> 18   2.49637234 0.80484962
#> 19   0.19280001 1.92552126
#> 20   2.62941229 1.48297852
#> 21   1.91996822 0.40368936
#> 22   2.20932082 1.18528684
#> 23   2.10261674 0.32513406
#> 24   1.67257283 1.11906119
#> 25   2.42730928 0.26519802
#> 26   1.76470022 0.62633844
#> 27   2.23128899 0.31244457
#> 28   1.71546355 0.12784117
#> 29   1.96525963 0.89823899
#> 30   1.66894887 0.74621947
#> 31   3.00870633 0.85374045
#> 32   2.43285909 0.92205393
#> 33  -0.48778119 1.96856634
#> 34   1.84597419 1.18492596
#> 35   1.92807736 0.37994358
#> 36   1.74175047 0.43551436
#> 37   2.35658367 1.36208723
#> 38   2.48674416 0.75908675
#> 39   2.45232814 0.67545599
#> 40   2.63202300 0.34843701
#> 41  -0.75737944 2.08655140
#> 42   1.66952275 0.23745512
#> 43   1.44637922 0.17133746
#> 44   0.51525902 1.83447390
#> 45   2.29255167 0.03234801
#> 46   2.70966110 0.97118466
#> 47   2.37563422 1.23252515
#> 48   2.16232277 0.69879132
#> 49   2.92560473 0.32238542
#> 50   0.98056568 1.65522764
#> 51   1.95465732 0.59936245
#> 52   2.01082448 0.66544343
#> 53  -0.85674115 2.36795395
#> 54  -6.90675478 3.13776710
#> 55   1.72611555 1.50581926
#> 56   0.02076562 1.78634238
#> 57   2.41890914 0.09778806
#> 58   2.22724962 1.53289699
#> 59   1.67129635 0.35410575
#> 60   1.55513976 1.29098749
#> 61   2.44416405 0.23759447
#> 62   1.89439817 0.54382372
#> 63   2.37042869 0.16967735
#> 64   1.83967579 1.34011564
#> 65  -0.47439313 2.06637640
#> 66  -0.89239448 2.21612584
#> 67   0.79149428 1.73569066
#> 68   1.58836486 0.51879138
#> 69   1.57267826 1.56274476
#> 70   1.59831797 0.24631971
#> 71   1.69118540 1.38092221
#> 72   1.94552283 0.43042725
#> 73   2.18164978 0.04844550
#> 74   2.70687710 0.78149645
#> 75   1.88646056 0.48993624
#> 76  -0.42290590 2.17270628
#> 77   2.22143943 0.47982702
#> 78   1.98709144 0.56961218
#> 79   2.63543591 0.05163864
#> 80  -1.30724886 2.52258634
#> 81   1.42229818 1.59282805
#> 82   2.43861218 0.77733849
#> 83   0.45069003 1.71289428
#> 84   0.64790451 1.71660083
#> 85   2.42482651 1.44024186
#> 86   3.89182030 1.15883062
#> 87   0.87668024 1.65976414
#> 88  -3.27854955 3.22051966
#> 89   2.46633324 0.18394507
#> 90   2.13714590 0.92604417
#> 91   2.16738296 0.58364533
#> 92   2.65975450 0.80851766
#> 93   1.84621881 1.06954478
#> 94  -0.86117420 2.15534832
#> 95   0.96084129 1.59495735
#> 96   1.98523295 0.41964511
#> 97   1.51921728 0.60667725
#> 98  -0.15391721 1.89292590
#> 99   1.70259760 1.14816796
#> 100 -1.00846203 2.22702839
# if input is of class `bayesnecformula`
nec3param <- function(beta, nec, top, x) {
  top * exp(-exp(beta) * (x - nec) *
    ifelse(x - nec < 0, 0, 1))
}

data <- data.frame(x = seq(1, 20, length.out = 10), tr = 100, wght = c(1, 2),
                   group_1 = sample(c("a", "b"), 10, replace = TRUE),
                   group_2 = sample(c("c", "d"), 10, replace = TRUE))
data$y <- nec3param(beta = -0.2, nec = 4, top = 100, data$x)

f_1 <- y ~ crf(x, "nec3param")
f_2 <- "y | trials(tr) ~ crf(sqrt(x), \"nec3param\")"
f_3 <- y | trials(tr) ~ crf(x, "nec3param") + ogl(group_1) + pgl(group_2)
f_4 <- y | trials(tr) ~ crf(x, "nec3param") + (nec + top | group_1)

m_1 <- model.frame(bnf(f_1), data)
attr(m_1, "bnec_pop")
#> y_var x_var 
#>   "y"   "x" 
#> attr(,"na.action")
#> [1] 3
#> attr(,"class")
#> [1] "omit"
model.frame(bnf(f_2), data)
#>               y  sqrt(x) trials(tr)
#> 1  1.000000e+02 1.000000        100
#> 2  1.000000e+02 1.763834        100
#> 3  3.676327e+01 2.285218        100
#> 4  6.527785e+00 2.708013        100
#> 5  1.159091e+00 3.073181        100
#> 6  2.058113e-01 3.399346        100
#> 7  3.654441e-02 3.696846        100
#> 8  6.488923e-03 3.972125        100
#> 9  1.152191e-03 4.229526        100
#> 10 2.045861e-04 4.472136        100
m_3 <- model.frame(bnf(f_3), data)
attr(m_3, "bnec_group")
#> [1] "group_1" "group_2"
model.frame(bnf(f_4), data)
#>               y         x trials(tr) group_1
#> 1  1.000000e+02  1.000000        100       b
#> 2  1.000000e+02  3.111111        100       a
#> 3  3.676327e+01  5.222222        100       b
#> 4  6.527785e+00  7.333333        100       a
#> 5  1.159091e+00  9.444444        100       b
#> 6  2.058113e-01 11.555556        100       a
#> 7  3.654441e-02 13.666667        100       b
#> 8  6.488923e-03 15.777778        100       a
#> 9  1.152191e-03 17.888889        100       b
#> 10 2.045861e-04 20.000000        100       a
# }