Question: --- title: Assignment 4 [105 points] output: word_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` # Problem 1 [26 points] Here we will investigate

--- title: "Assignment 4 [105 points]" output: word_document ---

```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ```

# Problem 1 [26 points] Here we will investigate the "confidence level" of each of the confidence intervals: *percentile*, *normal*, *basic*, and *BCa*. The *confidence level* of a confidence interval is the long-run proportion that the interval contains the parameter. So, if we took 100 samples and computed a 95% CI that estimates the population mean mu, the intervals should contain it 95% (95 out of 100) of the time.

1(a). [10pts] Simulate samples X of size n=10 from an exponential distribution with rate = 1/50. Run a bootstrap procedure to estimate the median. Compute the 4 CI's mentioned above. Repeat this 1,000 times and estimate the confidence level of each type of confidence interval.

1(b). [3pts] Are the confidence intervals below or above the confidence level of .95? Why do you think this is? Explain.

1(c). [10pts] Repeat this for samples of size n=50.

1(d). [3pts] Did the confidence level of the intervals change with the sample size? Are the confidence intervals below or above the confidence level of .95? Why do you think this is? Explain.

remember: you can put 'warning=FALSE' in the R chunk to not print warnings

# Problem 2 [10 points] Use Monte carlo integration to find estimate the area under the curve of the function (x^1/2) * log(x-1) * (x+2)^1/3 from 10 to 15. Estimate this so that the standard error is less than 0.01

# Problem 3 [21] 3(a). [8pts] Conduct a Monte Carlo integral estimation of the function sin(e^x) from 0 to 3. Use a minimum sample size of 1,000. ```{r}

```

3(b). [5pts] Estimate the the standard error and use this to compute a 95% confidence interval for the true integral value. (hint: what distribution does our integral estimate have?...if you know the distribution, then the CI formula should be straight forward) Give a sentence interpreting what this means. ```{r}

```

3(c). [8pts] Is there a way we could estimate a 95% confidence interval using only simulation and quantiles(percentiles)? If so, estimate a 95% confidence interval based on quantiles. (hint1: the answer is yes; hint2: in the MC integration notes we simulated the 'sampling distribution' of the integral estimate. From here we can use quantiles. )

# Problem 4 [15 points] The relative risk (RR) is the ratio of the probability of an outcome in one group to the probability of the outcome in another group. For example, in our 'odds ratio example' the RR would be (104/1141)/(189/1123) = (prop. of HA's in aspirin gp.)/(prop. of HA's in placebo gp.) = 0.542. This could be interpreted as, the probability of having a HA while taking aspirin is about .54 that of getting a HA if taking no aspirin.

Suppose 50 basketball players use a new training program and 50 players use an old training program. At the end of the program we test each player to see if they pass a certain skills test. The following table shows the results. ```{r, echo=FALSE} data

library(knitr) kable(data) ```

(a). [3pts] Compute the relative risk of passing the skills test in the new program versus the old program. Write a statement interpreting the value.

(b). [12pts] Run a bootstrapping procedure to estimate the variability of the relative risk of passing the skills test in the new program versus the old program.

* Compute the standard error of the RR

* Compute the bias of the RR

* Compute the bias corrected RR value

* Compute the 4 confidence intervals percentile, normal, basic, BCa (at 95%). Would any be more appropriate to use than others, why or why not. Provide justification in your reasoning.

# Problem 5 [13 points] In the *car* package is a data set ***Prestige***. You can read about it by running *?Prestige* after loading the *car* package. We are interested in looking at the correlation between Income and Prestige.

(a). [3pts] Scatter plot of Income and Prestige. Is there a linear relationship between Income and Prestige? ```{r, message=FALSE} library(car) ```

(b). [10pts] A robust correlation measure is the **Minimum Covariance Determinant**. This can be computed below using the **covMcd()** function in the *robustbase* package. ```{r} # install.packages("robustbase") library(robustbase)

df

# robust MCD-based covariance covMcd(df, cor = TRUE)$cor ```

This is a "robust" correlation matrix between Income and Prestige. Obviously, the correlation between Income and itself is 1 (similar for Prestige), and the correlation between Income and Prestige is 0.824. It is a big challenge to estimate the standard errors of the MCD correlation using formulas. Run a bootstrapping procedure to estimate the standard error of the robust correlation between Income and Prestige.

# Problem 6 [17 pts] In the Assignment 4 information is a data set, *The Adv of Alice in Wonderland_Ch1.csv*. This contains the text from chapter 1 of the famous book "The Adventures if Alice in Wonderland", by Lewis Carol.

(a). [7pts] (1-gram text generation) Use the data set as a corpus. Fit a Markov chain to the text using 1-grams. Use the model to generate 5 random sentences of length 50 each. You may choose your own starting word(s) for the sentences.

(b). [7pts] (2-gram text prediction) In our 1-gram and 2-gram notes we saw a process for predicting the next word in a sentence with the highest probability. Using the "The Adventures if Alice in Wonderland" text, put down a function that predicts a set of possible next words in a given partial sentence.

The function will take three inputs:

(1) **sentence**: the starting of a sentence as a character string, ex. "Hello, how are" and

(2) **n**: the number of words to return that might come next in the sentence. The first returned word should have the highest probability of coming next, the second word should have the second highest probability, and so on.

(3) **trans_mat**: a transition matrix from a previously fitted markov model.

The function should base its predictions on the last bigram of the partial sentence.

Note: if there are less than **n** words that can be generated based on the text, then have your function only return these.

(c). [5pts] Use your function from 1(b) to generate a list of 4 words (or less) which could be the next word in the sentence "I looked around and saw the rabbit". Note: this will require fitting a markov chain to all bigrams in the text first to obtain the transition matrix.

The Adventures if Alice in Wonderland CVS Table

gutenberg_idtext
11chapter i .
11down the rabbithole
11alice was beginning to get very tired of sitting by her sister on the
11bank , and of having nothing to do once or twice she had peeped into
11the book her sister was reading , but it had no pictures or
11conversations in it , and what is the use of a book , thought alice
11without pictures or conversations
11so she was considering in her own mind as well as she could , for the
11hot day made her feel very sleepy and stupid , whether the pleasure of
11making a daisychain would be worth the trouble of getting up and
11picking the daisies , when suddenly a white rabbit with pink eyes ran
11close by her .
11there was nothing so very remarkable in that nor did alice think it
11so very much out of the way to hear the rabbit say to itself , oh
11dear ! oh dear ! i shall be late ! when she thought it over afterwards ,
11it occurred to her that she ought to have wondered at this , but at the
11time it all seemed quite natural but when the rabbit actually took a
11watch out of its waistcoatpocket , and looked at it , and then hurried
11on , alice started to her feet , for it flashed across her mind that she
11had never before seen a rabbit with either a waistcoatpocket , or a
11watch to take out of it , and burning with curiosity , she ran across the
11field after it , and fortunately was just in time to see it pop down a
11large rabbithole under the hedge .
11in another moment down went alice after it , never once considering how
11in the world she was to get out again .
11the rabbithole went straight on like a tunnel for some way , and then
11dipped suddenly down , so suddenly that alice had not a moment to think
11about stopping herself before she found herself falling down a very
11deep well .
11either the well was very deep , or she fell very slowly , for she had
11plenty of time as she went down to look about her and to wonder what
11was going to happen next . first , she tried to look down and make out
11what she was coming to , but it was too dark to see anything then she
11looked at the sides of the well , and noticed that they were filled with
11cupboards and bookshelves here and there she saw maps and pictures
11hung upon pegs . she took down a jar from one of the shelves as she
11passed it was labelled orange marmalade , but to her great
11disappointment it was empty she did not like to drop the jar for fear
11of killing somebody underneath , so managed to put it into one of the
11cupboards as she fell past it .
11well ! thought alice to herself , after such a fall as this , i shall
11think nothing of tumbling down stairs ! how brave theyll all think me
11at home ! why , i wouldnt say anything about it , even if i fell off the
11top of the house ! which was very likely true .
11down , down , down . would the fall never come to an end i wonder how
11many miles ive fallen by this time she said aloud . i must be
11getting somewhere near the centre of the earth . let me see that would
11be four thousand miles down , i think for , you see , alice had learnt
11several things of this sort in her lessons in the schoolroom , and
11though this was not a very good opportunity for showing off her
11knowledge , as there was no one to listen to her , still it was good
11practice to say it over yes , thats about the right distancebut
11then i wonder what latitude or longitude ive got to alice had no
11idea what latitude was , or longitude either , but thought they were nice
11grand words to say .
11presently she began again . i wonder if i shall fall right through
11the earth ! how funny itll seem to come out among the people that walk
11with their heads downward ! the antipathies , i think she was rather
11glad there was no one listening , this time , as it didnt sound at all
11the right word but i shall have to ask them what the name of the
11country is , you know . please , maam , is this new zealand or australia
11and she tried to curtsey as she spokefancy curtseying as youre
11falling through the air ! do you think you could manage it and what
11an ignorant little girl shell think me for asking ! no , itll never do
11to ask perhaps i shall see it written up somewhere .
11down , down , down . there was nothing else to do , so alice soon began
11talking again . dinahll miss me very much tonight , i should think !
11dinah was the cat . i hope theyll remember her saucer of milk at
11teatime . dinah my dear ! i wish you were down here with me ! there are
11no mice in the air , im afraid , but you might catch a bat , and thats
11very like a mouse , you know . but do cats eat bats , i wonder and here
11alice began to get rather sleepy , and went on saying to herself , in a
11dreamy sort of way , do cats eat bats do cats eat bats and
11sometimes , do bats eat cats for , you see , as she couldnt answer
11either question , it didnt much matter which way she put it . she felt
11that she was dozing off , and had just begun to dream that she was
11walking hand in hand with dinah , and saying to her very earnestly ,
11now , dinah , tell me the truth did you ever eat a bat when suddenly ,
11thump ! thump ! down she came upon a heap of sticks and dry leaves , and
11the fall was over .
11alice was not a bit hurt , and she jumped up on to her feet in a moment
11she looked up , but it was all dark overhead before her was another
11long passage , and the white rabbit was still in sight , hurrying down
11it . there was not a moment to be lost away went alice like the wind ,
11and was just in time to hear it say , as it turned a corner , oh my ears
11and whiskers , how late its getting ! she was close behind it when she
11turned the corner , but the rabbit was no longer to be seen she found
11herself in a long , low hall , which was lit up by a row of lamps hanging
11from the roof .
11there were doors all round the hall , but they were all locked and when
11alice had been all the way down one side and up the other , trying every
11door , she walked sadly down the middle , wondering how she was ever to
11get out again .
11suddenly she came upon a little threelegged table , all made of solid
11glass there was nothing on it except a tiny golden key , and alices
11first thought was that it might belong to one of the doors of the hall
11but , alas ! either the locks were too large , or the key was too small ,
11but at any rate it would not open any of them . however , on the second
11time round , she came upon a low curtain she had not noticed before , and
11behind it was a little door about fifteen inches high she tried the
11little golden key in the lock , and to her great delight it fitted !
11alice opened the door and found that it led into a small passage , not
11much larger than a rathole she knelt down and looked along the
11passage into the loveliest garden you ever saw . how she longed to get
11out of that dark hall , and wander about among those beds of bright
11flowers and those cool fountains , but she could not even get her head
11through the doorway and even if my head would go through , thought
11poor alice , it would be of very little use without my shoulders . oh ,
11how i wish i could shut up like a telescope ! i think i could , if i only
11knew how to begin . for , you see , so many outoftheway things had
11happened lately , that alice had begun to think that very few things
11indeed were really impossible .
11there seemed to be no use in waiting by the little door , so she went
11back to the table , half hoping she might find another key on it , or at
11any rate a book of rules for shutting people up like telescopes this
11time she found a little bottle on it , which certainly was not here
11before , said alice , and round the neck of the bottle was a paper
11label , with the words drink me , beautifully printed on it in large
11letters .
11it was all very well to say drink me , but the wise little alice was
11not going to do that in a hurry . no , ill look first , she said ,
11and see whether its marked poison or not for she had read
11several nice little histories about children who had got burnt , and
11eaten up by wild beasts and other unpleasant things , all because they
11would not remember the simple rules their friends had taught them
11such as , that a redhot poker will burn you if you hold it too long
11and that if you cut your finger very deeply with a knife , it usually
11bleeds and she had never forgotten that , if you drink much from a
11bottle marked poison , it is almost certain to disagree with you ,
11sooner or later .
11however , this bottle was not marked poison , so alice ventured to
11taste it , and finding it very nice , it had , in fact , a sort of mixed
11flavour of cherrytart , custard , pineapple , roast turkey , toffee , and
11hot buttered toast , she very soon finished it off .
11
11
11
11what a curious feeling ! said alice i must be shutting up like a
11telescope .
11and so it was indeed she was now only ten inches high , and her face
11brightened up at the thought that she was now the right size for going
11through the little door into that lovely garden . first , however , she
11waited for a few minutes to see if she was going to shrink any further
11she felt a little nervous about this for it might end , you know ,
11said alice to herself , in my going out altogether , like a candle . i
11wonder what i should be like then and she tried to fancy what the
11flame of a candle is like after the candle is blown out , for she could
11not remember ever having seen such a thing .
11after a while , finding that nothing more happened , she decided on going
11into the garden at once but , alas for poor alice ! when she got to the
11door , she found she had forgotten the little golden key , and when she
11went back to the table for it , she found she could not possibly reach
11it she could see it quite plainly through the glass , and she tried her
11best to climb up one of the legs of the table , but it was too slippery
11and when she had tired herself out with trying , the poor little thing
11sat down and cried .
11come , theres no use in crying like that ! said alice to herself ,
11rather sharply i advise you to leave off this minute ! she generally
11gave herself very good advice , though she very seldom followed it ,
11and sometimes she scolded herself so severely as to bring tears into
11her eyes and once she remembered trying to box her own ears for having
11cheated herself in a game of croquet she was playing against herself ,
11for this curious child was very fond of pretending to be two people .
11but its no use now , thought poor alice , to pretend to be two
11people ! why , theres hardly enough of me left to make one respectable
11person !
11soon her eye fell on a little glass box that was lying under the table
11she opened it , and found in it a very small cake , on which the words
11eat me were beautifully marked in currants . well , ill eat it , said
11alice , and if it makes me grow larger , i can reach the key and if it
11makes me grow smaller , i can creep under the door so either way ill
11get into the garden , and i dont care which happens !
11she ate a little bit , and said anxiously to herself , which way which
11way , holding her hand on the top of her head to feel which way it was
11growing , and she was quite surprised to find that she remained the same
11size to be sure , this generally happens when one eats cake , but alice
11had got so much into the way of expecting nothing but outoftheway
11things to happen , that it seemed quite dull and stupid for life to go
11on in the common way .
11so she set to work , and very soon finished off the cake .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!