Question: 3 Functions and Macros Homework General instructions. There are four exercises below. Three exercises are worth 10 points each, and are labelled (Either R, Python
3 Functions and Macros Homework General instructions. There are four exercises below. Three exercises are worth 10 points each, and are labelled (Either R, Python or Julia). For these exercises, you can implement a solution in the language of your choice. I've included code chunk templates for all three languages, but you only need to fill in the chunks for one language. One exercise is labelled (R, Python and Julia). This exercise is worth 20 points, and you must provide a solution using all three languages. This exercise also includes code chunk templates for all three languages. Some of the exercises have questions relating to the results. Write your answers in the narrative part of the text, formatted as italicized text, bold text or quoted text. Change the name of this file to match your user name on D2L, keeping the 'Rmd' extension, and include week number in the title (for example, Peter.Claussen.3.Rmd). Upload this file to D2L. Typeset this file to Word or PDF and upload the result to D2L as well. Exercise 1 (Either R, Python or Julia) As before, using values from Table 1, show how Wansink and Payne calculate percent increase in calories per recipe (43.7%), calories per serving (63.0%) and servings per recipe (-2.0). That is, confirm the values in the final column of Table 1 (Change from 1936 to 2006, %, p. 292). For this exercise, write a function to perform these calculations. Name the function PercentIncrease with parameters m1 and m2. Check that this function duplicates the values you calculated for Homework 2. Answer Function definition R # function definition Python # function definition Julia # function definition 1 Calories per Recipe R Python Julia Calories per Serving R Python Julia Servings per Recipe R Python Julia Exercise 2 (Either R, Python or Julia) Consider this quotation from Wansink Over the past 70 years, the total caloric content increased for 14 of the 18 recipes. Because of changes in ingredients, the mean average calories in a recipe increased by 928.1 (from 2123.8 calories [95% CI, 1638.7 to 2608.9 calories] to 3051.9 calories [CI, 2360.7 to 3743.1 calories]), representing a 43.7% increase (P < 0.001). As the Table indicates, mean average calories per serving increased for 17 of 18 recipes and was influenced by both changes in ingredients and changes in serving size. The resulting increase of 168.8 calories (from 268.1 calories [CI, 210.4 to 325.8 calories] to 436.9 calories [CI, 359.1 to 514.7 calories]) represents a 63.0% increase (P < 0.001) in calories per serving. Write a function named ConfidenceInterval that returns list or tuple comprised of the Lower and Upper bounds. You may use the ConfidenceBound function from course outline to produce confidence intervals or write your own version. You function should accept as arguments mean, sd and n, and an optional parameter alpha=0.05. Confirm the confidence intervals reported as [95% CI, 1638.7 to 2608.9 calories] [CI, 2360.7 to 3743.1 calories] [CI, 210.4 to 325.8 calories] [CI, 359.1 to 514.7 calories] Use the appropriate values from Table 1. Answer Define your function(s) in the code chunk below, the call the function with appropriate parameters in the following sections R # function definitions 2 Python # function definitions Julia # function definitions [95% CI, 1638.7 to 2608.9 calories] R Python Julia [CI, 2360.7 to 3743.1 calories] R Python Julia [CI, 210.4 to 325.8 calories] R Python Julia [CI, 359.1 to 514.7 calories] R Python Julia Where you able to reproduce Wansink and Payne's reported confidence intervals? Exercise 3 (Either R, Python or Julia) Implement the probability density function for the normal distribution as a function. f(x, , 2 ) = 1 2 e (x) 2 22 Define and as optional parameters, taking values mu=0 and sigma=1. Name this function NormPdf. Answer Define your function(s) in the code chunk below, the call the function with appropriate parameters in the following sections. Use = 1, = 2 and x = {1, 0, 1} and compare with previous homework. R # function definition 3 Python # function definition Julia # function definition x = 1 R Python Julia x = 0 R Python Julia x = 1 R Python Julia Exercise 4 (R, Python and Julia) The probability mass function for for the binomial distribution is given by n! k!(n k)! k (1 ) (nk) where is the probability of success and k is the number of successes out of n trials. Implement this as a function BinomPMF that accepts as arguments k, n and pi . Test your function with values = 0.5, n = 10 and k = {3, 4, 5} Answer Part a. Define your function(s) in the code chunk below, the call the function with appropriate arguments in the following sections R # function definition Python # function definition 4 Julia # function definition k = 3 R Python Julia k = 4 R Python Julia k = 5 R Python Julia You can check your work against the built in Binomial distribution functions, dbinom(k,n,pi) in R, scipy.stats.norm.pdf(k,n,pi) in Python and pdf(Binomial(n, pi),k) in Julia. R dbinom(3, 10, 0.5) dbinom(4, 10, 0.5) dbinom(5, 10, 0.5) Python import scipy scipy.stats.binom.pmf(3, 10, 0.5) scipy.stats.binom.pmf(4, 10, 0.5) scipy.stats.binom.pmf(5, 10, 0.5) Julia using Distributions pdf(Binomial(10, 0.5),3) pdf(Binomial(10, 0.5),4) pdf(Binomial(10, 0.5),5) Part b. In "An Argument for Divine Providence, Taken from the Constant Regularity Observ'd in the Births of Both Sexes." published in 1710 (https://royalsocietypublishing.org/doi/epdf/10.1098/rstl.1710.0011), 5 In London in 1629, there were 5218 males born and 4683 females. Assuming male and female births are equally likely, what is the probability of this occurrence happening by chance - that is, calculate the probability of the binomial distribution with = 0.5, k = 5218 and n = 5218 + 4683 using your BinomPMF function. What happens when you try to calculate this value? Hint: what happens when the factorial function is given large values as arguments? You might not be able to typeset code, if it results in an error. Simply report the resulting error messages. Are there differences among the three languages that you might use to calculate this value? An interesting side note. Arbuthnot's data is available as an R data set associated with the HistData library. According to the R help file for this data set (one version is available at https://www.stat.auckland.ac.nz/~w ild/data/Rdatasets/doc/HistData/Arbuthnot.html ), a publication (S. Zabell (1976). Arbuthnot, Heberden, and the Bills of Mortality. Technical Report No. 40, Department of Statistics, University of Chicago.) points out several inaccuracies or irregularities in these data. Erroneously published data has been around a long time.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
