The posterior predictive distribution is the distribution of the outcome implied by the model after using the observed data to update our beliefs about the unknown parameters in the model. Simulating data from the posterior predictive distribution using the observed predictors is useful for checking the fit of the model. Drawing from the posterior predictive distribution at interesting values of the predictors also lets us visualize how a manipulation of a predictor affects (a function of) the outcome(s). With new observations of predictor variables we can use the posterior predictive distribution to generate predicted outcomes.

# S3 method for stapreg
posterior_predict(object, newsubjdata = NULL,
  newdistdata = NULL, newtimedata = NULL, draws = NULL,
  subject_ID = NULL, group_ID = NULL, re.form = NULL, fun = NULL,
  seed = NULL, offset = NULL, ...)

Arguments

object

A fitted model object returned by one of the rstap modeling functions. See stapreg-objects.

newsubjdata

Optionally, a data frame of the subject-specific data in which to look for variables with which to predict. If omitted, the original datasets are used. If newsubjdata is provided and any variables were transformed (e.g. rescaled) in the data used to fit the model, then these variables must also be transformed in newsubjdata. This only applies if variables were transformed before passing the data to one of the modeling functions and not if transformations were specified inside the model formula. Also see the Note section below for a note about using the newdata argument with with binomial models.

newdistdata

If newsubjdata is provided a data frame of the subject-distance must also be given for models with a spatial component

newtimedata

If newsubjdata is provided, a data frame of the subject-time data must also be given for models with a temporal component

draws

An integer indicating the number of draws to return. The default and maximum number of draws is the size of the posterior sample.

subject_ID

name of column to join on between subject_data and bef_data

group_ID

name of column to join on between subject_data and bef_data that uniquely identifies the correlated groups (e.g. visits,schools). Currently only one group (e.g. a measurement ID) can be accounted for in a spatial temporal setting.

re.form

If object contains group-level parameters, a formula indicating which group-level parameters to condition on when making predictions. re.form is specified in the same form as for predict.merMod. The default, NULL, indicates that all estimated group-level parameters are conditioned on. To refrain from conditioning on any group-level parameters, specify NA or ~0. The newdata argument may include new levels of the grouping factors that were specified when the model was estimated, in which case the resulting posterior predictions marginalize over the relevant variables.

fun

An optional function to apply to the results. fun is found by a call to match.fun and so can be specified as a function object, a string naming a function, etc.

seed

An optional seed to use.

offset

A vector of offsets. Only required if newsubjdata is specified and an offset argument was specified when fitting the model.

...

optional arguments to pass to pp_args

Value

A draws by nrow(newdata) matrix of simulations from the posterior predictive distribution. Each row of the matrix is a vector of predictions generated using a single draw of the model parameters from the posterior distribution. The returned matrix will also have class "ppd" to indicate it contains draws from the posterior predictive distribution.

Note

For binomial models with a number of trials greater than one (i.e., not Bernoulli models), if newsubjdata is specified then it must include all variables needed for computing the number of binomial trials to use for the predictions. For example if the left-hand side of the model formula is cbind(successes, failures) then both successes and failures must be in newdata. The particular values of successes and failures in newdata do not matter so long as their sum is the desired number of trials. If the left-hand side of the model formula were cbind(successes, trials - successes) then both trials and successes would need to be in newsubjdata, probably with successes set to 0 and trials specifying the number of trials.

See also

Examples of posterior predictive checking can also be found in the rstanarm vignettes and demos.

predictive_error and predictive_interval.

Examples

if (!exists("example_model")) example(example_model) yrep <- posterior_predict(example_model) table(yrep)
#> yrep #> -1.56121305193056 -1.51260150453634 -1.36581885576293 #> 1 1 1 #> -1.23143230246084 -1.20295723758698 -1.18748739258829 #> 1 1 1 #> -1.09485760455758 -1.02104065991504 -0.995920249246662 #> 1 1 1 #> -0.989767959169445 -0.961964241644163 -0.882396716240474 #> 1 1 1 #> -0.870436891461432 -0.842401161672407 -0.833305295333916 #> 1 1 1 #> -0.799734224170841 -0.787396965429495 -0.776941618748788 #> 1 1 1 #> -0.67880268042907 -0.618311232620918 -0.588306736767002 #> 1 1 1 #> -0.570043068983559 -0.537883209873065 -0.532643209111764 #> 1 1 1 #> -0.487144826920698 -0.486128668350948 -0.47729321126397 #> 1 1 1 #> -0.454381225433641 -0.411606605568796 -0.367052710533112 #> 1 1 1 #> -0.363580416971552 -0.346794577560871 -0.346689384073015 #> 1 1 1 #> -0.316694157051106 -0.304218007850338 -0.299615359025132 #> 1 1 1 #> -0.297623629399992 -0.297017048434852 -0.290830860183995 #> 1 1 1 #> -0.28412538814261 -0.277831775660861 -0.277453312344114 #> 1 1 1 #> -0.259629165339297 -0.247767842061995 -0.235398954631208 #> 1 1 1 #> -0.229802396077182 -0.226414149589619 -0.209448258503866 #> 1 1 1 #> -0.201047541366406 -0.199848309531846 -0.189009418385884 #> 1 1 1 #> -0.186365982661922 -0.182133725054409 -0.181328235882668 #> 1 1 1 #> -0.166602214461003 -0.159428764221876 -0.156237256029573 #> 1 1 1 #> -0.154039874310267 -0.151102425274141 -0.13557999257301 #> 1 1 1 #> -0.116987791532484 -0.110932792673775 -0.108119951268614 #> 1 1 1 #> -0.10426495362817 -0.100763747121578 -0.0984966415927143 #> 1 1 1 #> -0.0905065838795602 -0.0886163742630338 -0.0779159844118233 #> 1 1 1 #> -0.0746155365490654 -0.0717879760755852 -0.0505338088400528 #> 1 1 1 #> -0.0478108740722654 -0.0427957672126146 -0.041239322019988 #> 1 1 1 #> -0.0137493141479996 -0.0116507974579152 -0.00685374677607178 #> 1 1 1 #> 0.00103579945941407 0.00129609829756727 0.00449247923809004 #> 1 1 1 #> 0.0110202918622 0.0131589566647963 0.0133499964736058 #> 1 1 1 #> 0.019864068871459 0.0237489740028268 0.0289795247230278 #> 1 1 1 #> 0.0402994309273898 0.040316970344215 0.0485058040274426 #> 1 1 1 #> 0.0524698090370548 0.0557440113094237 0.0586602242235666 #> 1 1 1 #> 0.0668050654291482 0.0723766232762382 0.098760997152421 #> 1 1 1 #> 0.104178356684027 0.105312495397071 0.114468107942755 #> 1 1 1 #> 0.141003264072105 0.141385793965765 0.148312375449719 #> 1 1 1 #> 0.164245772883006 0.172172941945281 0.182567558086226 #> 1 1 1 #> 0.183805723477791 0.187097234145005 0.188223680382852 #> 1 1 1 #> 0.196955796258652 0.197718490117178 0.223193952211702 #> 1 1 1 #> 0.226092907442888 0.230155582798823 0.233060255875694 #> 1 1 1 #> 0.235581919457903 0.237754467961783 0.248412402985819 #> 1 1 1 #> 0.248833522449507 0.26936055706249 0.27468148874106 #> 1 1 1 #> 0.279451392447041 0.292012268360065 0.300477239963677 #> 1 1 1 #> 0.304123441260394 0.307321150351286 0.313082081604342 #> 1 1 1 #> 0.322826568741814 0.337196934178861 0.337379997069838 #> 1 1 1 #> 0.351792331958604 0.368207315414222 0.381876057446015 #> 1 1 1 #> 0.38400846952877 0.388453343920144 0.391083044294151 #> 1 1 1 #> 0.418298670843656 0.420042743028621 0.423505756259125 #> 1 1 1 #> 0.428325863818027 0.430875780837145 0.460745577989145 #> 1 1 1 #> 0.465308150307247 0.481371218012568 0.48204640206887 #> 1 1 1 #> 0.48875379467155 0.505661613277656 0.521043100069898 #> 1 1 1 #> 0.527443900125527 0.530009594327974 0.534285522114534 #> 1 1 1 #> 0.541131188058688 0.546730272516843 0.551985033589305 #> 1 1 1 #> 0.564598483253033 0.565169919896918 0.570024169134145 #> 1 1 1 #> 0.587492716941603 0.602603534271075 0.603117403078572 #> 1 1 1 #> 0.604779935421077 0.610222259277672 0.614962497494446 #> 1 1 1 #> 0.640154677931618 0.644381625742996 0.651912729571681 #> 1 1 1 #> 0.652639553328162 0.655083084286669 0.66016677740588 #> 1 1 1 #> 0.671408945742108 0.686557937011904 0.689553988709152 #> 1 1 1 #> 0.695762162352558 0.696870247777969 0.706665216519109 #> 1 1 1 #> 0.725358743791551 0.726329498846274 0.730308977487934 #> 1 1 1 #> 0.763667440149696 0.796708339570275 0.825651374913288 #> 1 1 1 #> 0.83758616275315 0.844765025708219 0.846988856056213 #> 1 1 1 #> 0.855858651353348 0.864439997026151 0.882509885117328 #> 1 1 1 #> 0.902962053274589 0.920915289545053 0.929941625003709 #> 1 1 1 #> 0.930929836336306 0.932473864561299 0.935910057879046 #> 1 1 1 #> 0.936190825640063 0.936531140042397 0.937084130131066 #> 1 1 1 #> 0.954059942221004 0.961648391990672 0.967299638438835 #> 1 1 1 #> 0.96839712273756 0.983754328643101 0.984647674768299 #> 1 1 1 #> 0.989715062548555 0.991376216143369 1.00547444321716 #> 1 1 1 #> 1.01039046046705 1.0186088992923 1.03296626777405 #> 1 1 1 #> 1.03700816543369 1.05174522024298 1.0544670097854 #> 1 1 1 #> 1.07185977221917 1.09094955459539 1.09146862056273 #> 1 1 1 #> 1.0996092721982 1.10589029836181 1.10900484984465 #> 1 1 1 #> 1.14664957205183 1.15109901614316 1.15285679188766 #> 1 1 1 #> 1.15681619143348 1.16514986227824 1.16686298612297 #> 1 1 1 #> 1.17032972775837 1.19625302258107 1.22406649832746 #> 1 1 1 #> 1.24275781427237 1.25247030723248 1.25789027479147 #> 1 1 1 #> 1.33635072376987 1.363284963217 1.3749663448529 #> 1 1 1 #> 1.44163377990789 1.4835687543955 1.60050887288835 #> 1 1 1 #> 1.71387576373423 1.78020704730829 1.88988674345082 #> 1 1 1 #> 1.89098684778369 1.9141010928133 1.94316647683923 #> 1 1 1 #> 1.97527853968539 2.02846396816054 2.03059681649549 #> 1 1 1 #> 2.26694666231164 2.27412594015712 2.4731628798081 #> 1 1 1 #> 3.11321612546452 #> 1
# \donttest{ # If using new data the all pertinent data must be submitted to the function including subject_ID # The same distance and time datasets below are used in the original function # Which will associate the same spatio-temporal exposure to this subject's new fixed covariates. newdata <- data.frame(subj_ID = 1, measure_ID = 1, centered_income = 0, sex = 0, centered_age = 0) pps <- posterior_predict(example_model, newsubjdata = newdata, newdistdata= subset(distdata,subj_ID == 1, measure_ID == 1), newtimedata = subset(timedata, subj_ID == 1, measure_ID == 1), subject_ID = "subj_ID", group_ID = "measure_ID" )
#> Error in .subset(x, j): invalid subscript type 'list'
# }