If we pass in a variable that is not in the input data set, we would expect an error to be thrown: This allows the function to check if a warning is present when the Chi-squared test is performed, and return either the Chi-squared test p-value or the Fisher Exact test p-value from the subsequent if-else block. Now, when chisq_wrapper is called, it will return a list with components result, output, messages and warnings. The next line wraps the chisq_wrapper function in purrr::quietly, which captures the side effects of a function. The chisq.test and fisher.test functions, respectively, are then performed on those vectors and only the numeric p.value result is returned. These functions pull and store the specified variables from the input tbl as vectors. # treatment % dplyr::pull(treatment) %>% as.factor()įirst, there are two internal functions defined, chisq_wrapper and fisher_wrapper. treatment % dplyr::pull(var) %>% as.factor() The chifishr::chi_fisher_p function is a simple function that calculates a p-value from either a Chi-squared or Fisher Exact test, depending on if a warning is thrown from the Chi-squared test due to small expected counts leading to poor p-value approximations. We’ll create a simple data set with three binary variables, treatment, gender, and outcome. Let’s look at an example function to demonstrate the use of these tools. This can be done ad-hoc in a separate R script containing the function code, or using several built-in tools in RStudio, including the traceback function and debug mode. Look and/or walk through the code, step-by-step at that point.Stop the code where you suspect the bug/problem is arising.How can you determine where the code is not behaving? You can follow these general steps to answer this question: Many times, you may not even know what line of code the error is coming from. These four steps should be followed each time you encounter an unexpected error in a function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |