Due Monday, 15 October, 12:00 PM
50 points total.
Submit a single knitr (either .rnw
or .rmd
) file, along with a valid PDF output file. Inside the file, clearly indicate which parts of your responses go with which problems (you may use the original homework document as a template). Raw R code/output or word processor files are not acceptable.
20 points
A problem with the Newton-Raphson algorithm is that it needs the derivative
Like the Newton-Raphson method, the secant method is based on a linear approximation to the function
To find a good new guess x2 we first draw the straight line from
The general form of the recurrence equation for the secant method is:
\[x_{i+1} = x_i - f(x_i)\frac{x_i - x_{i-1}}{f(x_i) - f(x_{i-1})}\]
Notice that we no longer need to know
Write a function that implements the secant algorithm. Validate your program by finding the root of the function
15 points
Import the HAART dataset (haart.csv
) from the GitHub repository into R, and perform the following manipulations:
- Convert date columns into a usable (for analysis) format.
- Create an indicator variable (one which takes the values 0 or 1 only) to represent death within 1 year of the initial visit.
- Use the
init.date
,last visit
anddeath.date
to calculate a followup time, which is the difference between the first and either the last visit or a death event (whichever comes first). If these times are longer than 1 year, censor them. - Create another indicator variable representing loss to followup; that is, if their status 1 year after the first visit was unknown.
- Recall our work in class, which separated the
init.reg
field into a set of indicator variables, one for each unique drug. Create these fields and append them to the database as new columns. - The dataset
haart2.csv
contains a few additional observations for the same study. Import these and append them to your master dataset (if you were smart about how you coded the previous steps, cleaning the additional observations should be easy!).
15 points
The game of craps is played as follows. First, you roll two six-sided dice; let x be the sum of the dice on the first roll. If x = 7 or 11 you win, otherwise you keep rolling until either you get x again, in which case you also win, or until you get a 7 or 11, in which case you lose.
Write a program to simulate a game of craps. You can use the following snippet of code to simulate the roll of two (fair) dice:
x <- sum(ceiling(6*runif(2)))
The instructor should be able to easily import and run your program (function), and obtain output that clearly shows how the game progressed.
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { displayMath: [ ['$$','$$'], ["\\[","\\]"] ], inlineMath: [ ['$','$'], ["\\(","\\)"] ], processEscapes: true } }); </script> <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script>