Distributions in R: Exercises Distributions in R: Exercises [Your Name Here] XXXXXXXXXX Below are some exercises for you to complete on your own. Make sure to add your name in the above YAML where it...

Basic "R" homework. Ran out of time. Help!


Distributions in R: Exercises Distributions in R: Exercises [Your Name Here] 2020-07-28 Below are some exercises for you to complete on your own. Make sure to add your name in the above YAML where it says “[Your Name Here].” When you’re done with this problem set. Just hit “Knit” and email your pdf output to [email protected] Exercise 1: A colleague needs help making a p.d.f.! You have a colleague that says they don’t trust the dnorm function. “How can I know it’s actually returning the correct probabilities?!” they ask. Fortunately, R is a functional programming language. That means you can create your own custom functions! Knowing this, you tell your colleague that they can make their own version of dnorm. They can then compare the output from their custom function to that provided by dnorm to learn whether the base R function is trustworthy. As it turns out, they’ve already started working on such a function. However, the function is returning results that don’t equal what dnorm is returning. That shouldn’t be! Can you help them figure out where they’ve gone wrong? Here’s what they have: my.dnorm = function(x,mean=0,sd=1) { # start bracket pdf = # equation for p.d.f. 1/(sd*sqrt(2*pi)) * exp(-0.5 * ((x - mean)/sd)) return(pdf) # tell the function to return the pdf } # close brackets The following code will compare the results to determine if both functions return the same thing. Right now, it’s coming back FALSE. 1 mailto:[email protected] x = seq(-5,5,0.01) # vector from -5 to 5 in 0.01 increments base.dnorm = dnorm(x=x) my.results = my.dnorm(x=x) all(base.dnorm==my.results) # if output is TRUE, then you can trust dnorm! ## [1] FALSE Hint: Did they use the right equation for the normal distribution p.d.f.? Exercise 2: Binomial distributions The Binomial distribution analogues for the dnorm, pnorm, etc. functions in R are: • dbinom, • pbinom, • qbinom, • rbinom. You can learn more about these functions by typing ?dbinom into your console. Using these functions, can you plot the p.m.f., c.d.f., and empirical distribution for a binomial distribution with 5 trials where the probability of success is 0.25? This would be like a scenario where you have a “trick” coin where the likelihood of Heads, rather than being 50%, is 25%, and you want to know the probability of getting a certain number of heads after 5 tosses. As a quick example, this is how to get the p.m.f. for a Binomial distribution with 2 trials and probability of success equal to 0.6: n.trials = 2 dbinom(x = 0:n.trials, size = n.trials, prob = 0.6) ## [1] 0.16 0.48 0.36 Exercise 3: Conditional Probability Distributions You will almost always work with conditional probabilities or conditional means—or a conditional something—in your research. The function for the normal distribution is an example of a conditional probability function: f (x|µ, σ) = 1 σ √ 2π exp ( − (x− µ) 2 2σ2 ) . 2 The “|” in f (·) signifies that the function is evaluated at x given µ and σ. That is, the mean and standard deviation of a random variable condition the probability of observing a given value of x. Say you have a friend who wants to learn some intuition behind conditional probabilities. You decide to use the example of the conditional probability of pulling out up to 10 red jelly beans in 10 draws without replacement from a bowl of 100 total blue and red jelly beans. You provide the following code to demonstrate the idea more intuitively: # jelly beans beans = c('red','blue') # number of red jelly beans N1 = 20 N2 = 80 # total number of beans tot.beans = 100 # make a bowl of jelly beans bowl1 = c( rep(beans[1],N1), rep(beans[2],tot.beans-N1) ) bowl2 = c( rep(beans[1],N2), rep(beans[2],100-N2) ) # use loop to iterate times you pull from the bowl: # use the foreach package library(foreach) foreach(i = 1:1000, .combine = 'c') %do% { sum(sample(bowl1,size=10,replace=F)=='red') } -> out1 foreach(i = 1:1000, .combine = 'c') %do% { sum(sample(bowl2,size=10,replace=F)=='red') 3 } -> out2 # print out frequencies per number of times table(out1) ## out1 ## 0 1 2 3 4 5 6 ## 95 259 313 214 90 26 3 table(out2) ## out2 ## 3 4 5 6 7 8 9 10 ## 1 3 15 79 244 293 253 112 You tell your friend that table(out1) returns a summary of the number of times you get x many reds in 10 draws from the bowl when there are 20 reds total in the bowl. Meanwhile, table(out2) returns a summary of the number of times you get x many reds in 10 draws from the bowl when there are 80 total red jelly beans. “As you can see,” you tell them, “a bigger N of reds leads to a greater likelihood that you’ll get up to 10 red jelly beans in 10 draws from the bowl.” You continue, "this is what we mean by ‘conditional’ probability—the probability of pulling out x reds from the bowl is conditioned by the total number of reds in the bowl. Part (A) But then your friend asks, “What’s the minimum N such that I could get at least 10 reds in 10 pulls at least once?” How would you modify your code to answer this question? Note: You can either summarize in words how you would modify the code, or write out the code. You don’t have to do both! Part (B) You friend also asks, “Does the effect of the total number of reds in the bowl change depending on the total number of beans in the bowl?” Play arround with values for tot.beans in the above code to see if it makes a difference. Part (C) 4 Finally, your friend says, “One more Question. I promise!” “What happens if we put each bean we draw from the bowl back in before we draw again?” Change the code to answer their question. 5 Integration Integration Problem Set Miles D. Williams Below are problems related to integrals. You may either work out your responses in Latex or R Markdown and submit a rendered pdf, or you may write out your answers in a word doc that you save as a pdf, or you can write out your scanned answers. Send your answers to [email protected]. Problem 1. The area under and over the curve? Recall the example of the mid-sized paper supply company in northeast Pennsylvania. Say you’ve invested $50 in this company and want to know how much money you’ve netted by week 15. The function describing the rate of ROI at week t, remember, is given as k(t) = −2t. The indefinite integral is ∫ k(t)dt = C− t2. Part (A) What will be your net ROI between when you invested and week 15? Part (B) Hint for Part (A): your answer should have been negative. What does a negative value returned by the definite integral of k(t) imply? Are we calculating the area below the curve of k(t), or above? 1 mailto:[email protected] Problem 2. A unique fact about circles. If you’ve taken a class on geometry, you’re probably familiar with the equation for the circumference of a circle: C = πd = 2πr, where π is, well, the value of pi (3.14. . . .), and where d is the diameter of the circle and r the circumference. Use integration to find the area of a circle with r = 5. Hint: imagine that r varies from 0 at the origin of the circle to r = 5. Problem 3. Practice with Integrals In today’s age of Google where information is readily at our fingertips, rote memorization is pretty pointless. But, the ability to leverage tools to solve complex problems is incredibly valuable. With that in mind, below are some functions that I want you to integrate—but not by hand. . . Instead, use Wolfram Alpha to get the indefinite integrals for these functions. Here’s an example of how it works. Say you have the function q(x) = 1 x . When you follow the link to Wolfram Alpha above, go to the search bar at the top of the page that says Enter what you want to calculate or know about. To get the integral of q(x), all we need to do is write in the search bar integrate 1/x, then hit enter. Some software will do all the work for you behind the scenes and return the integral of the function along with some plots. For the above function, it should return the following indefinite integral: ∫ 1 x dx = ln(x) + C. Try it yourself to see. 2 https://www.wolframalpha.com/ Using this tool, find the integrals for the following functions:1 1. x4, 2. ln(w), 3. ey, 4. 10v, 5. 1/(x2 + 2x), 6. √ 3 + 2x. 1Note: To indicate that you want to multiply a variable by a constant, either write ’x*6’, for example, or ’x 6’ with a space. Otherwise, Wolfram’s language will think you’re specifying a function (e.g., ’x4’ = x(4)). 3 Problem 1. The area under and over the curve? Problem 2. A unique fact about circles. Problem 3. Practice with Integrals
Aug 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here