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