Next we’ll introduce a new function that you’ll be seeing a lot more of in the upcoming labs – a custom function that allows you to apply any statistical inference method that you’ll be learning in this course. Since this is a custom function, we need to load it first.
Writing a for loop every time you want to calculate a bootstrap interval or run a randomization test is cumbersome. This function automates the process. By default the
inferencefunction takes 10,000 bootstrap samples (instead of the 100 you’ve taken above), creates a bootstrap distribution, and calculates the confidence interval, using the percentile method.
inference(nc$gained, type="ci", method="simulation", conflevel=0.9, est="mean", boot_method="perc")
Next, we’ll use the
inferencefunction for evaluating whether there is a difference between the average birth weights of babies born to smoker and non-smoker mothers.
Let’s pause for a moment to go through the arguments of this custom function:
- The first argument is
y, which is the response variable that we are interested in:
- The second argument is the grouping variable,
x, which is the explanatory variable – the grouping variable across the levels of which we’re comparing the average value for the response variable, smokers and non-smokers:
- The third argument,
est, is the parameter we’re interested in:
"mean"(other options are
- Next we decide on the
typeof inference we want: a hypothesis test (
"ht") or a confidence interval(
- When performing a hypothesis test, we also need to supply the
nullvalue, which in this case is
0, since the null hypothesis sets the two population means equal to each other.
alternativehypothesis can be
- Lastly, the
methodof inference can be
By default the
inferencefunction sets the parameter of interest to be (μnonsmoker−μsmoker). We can easily change this order by using the order argument.
To set the order to μfirst−μsecond use:
order = c("first","second").
inference(y = nc$weight, x = nc$habit, est = “mean”, type = “ht”, null = 0, alternative = “twosided”, method = “theoretical”, order=c(“smoker”, “nonsmoker”))