[SOLVED] 代写代考 STAT 340: Discussion 01: R review”

30 $

File Name: 代写代考_STAT_340:_Discussion_01:_R_review”.zip
File Size: 461.58 KB

SKU: 6281489386 Category: Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Or Upload Your Assignment Here:


title: “STAT 340: Discussion 01: R review”
documentclass: article
classoption: letterpaper
html_document:

Copyright By PowCoder代写加微信 assignmentchef

highlight: tango
fig_caption: false

“`{r setup, include=FALSE}
# check packages installed
if(!require(pacman)) install.packages(“pacman”)
pacman::p_load(knitr,tidyverse)

knitr::opts_chunk$set(tidy=FALSE,strip.white=FALSE,fig.align=”center”,comment=” #”)
options(width=120)

a:link {text-decoration: underline;}
h2{margin-top:40px;}
h3{margin-top:30px;}
h4{margin-top:30px;font-style: italic;}

[Link to source file](ds01.Rmd)

## XKCD comic

## Exercises

Today’s exercises are intended as a review of basic R features and operations. Remember that discussion attendance is completely optional but highly recommended. Also, if you finish the material early, don’t be afraid to leave early.

### 1) Vector operations

Remember that in R, if an operation works on a single number, it will usually also work across a vector. For example, if you multiply a number by a vector, each number in the vector will be multiplied. If you multiply two vectors of the same length, the first number of both vectors will be multiplied, and the second number of both vectors will be multiplied, etc. This will also work for functions like `exp()` or `pnorm()`.

a. Create a vector of the numbers 1 to 25 (try to do this without writing out each individual number). Multiply the vector by 2 to get a vector of all the even numbers less than or equal to 50. Then, square this vector.
c. Find the mean of this vector and subtract it from each number.
d. Using `>=`, compare this vector with 0 to show if each number is greater than or equal to 0. Use `sum()` on this resultant vector to count how many numbers satisfy this criterion (or alternatively, use `mean()` to get the proportion (think about why this works!)).
d. Divide the interval $(0,1)$ into 15 evenly spaced numbers (**not including** 0 and 1). (Hint: use the [ppoints](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/ppoints.html) function). Then, use `qnorm()` to get a vector of 15 points evenly spaced out along the quantiles of the normal distribution. Note: this is how you obtain the theoreticals for a QQ-plot.

vec <- 1:25;vec2 <- 2*vec;vec2sq <- vec2^2;# b missingcentered <- vec2sq – mean(vec2sq);spaced <- ppoints(15);qnorm(spaced)### 2) FunctionsFunctions are a useful way of creating a tool that can be used over and over again. Good functions ***usually*** (but not necessarily always have to) satisfy the following:1. The function has a good name that makes sense to the user.2. They have a single purpose (e.g. don’t write a function that can do two very different things).3. Extra features or special use cases can be accessed using arguments.4. Additional optional arguments should have sensible default values.5. At the end, it should return an object (in R, this is often a [list](https://www.r-bloggers.com/2010/11/programming-with-r-%E2%80%93-returning-information-as-a-list/) object, but you can return anything).Write a function for each of the following parts:1. Given an `n` and `k`, computes the binomial coefficient. You can use the factorial function for simplicity.binomcoef <- function( n, k ) { # Note that in practice you should use built-in functions for this stuff! # See ?factorial return( factorial(n)/(factorial(k)*factorial(n-k)) );2. Simulates rolling `n` 6-sided dice and gives the average of the outcomes. `n` should have a default value of 2.rolldice <- function( n) { rolls <- sample(1:6, size=n, replace=TRUE ); return( mean(rolls));3. Manually (i.e. without using `sd()`) compute the sample standard deviation of a vector.mysd <- function( data ) { return( sqrt( (data-mean(data))^2/length(data) ) );Note: functions in R have different scope than the global environment. Read [this](https://www.geeksforgeeks.org/scope-of-variable-in-r/) for a helpful guide about this. Also note that declaring/updating a global variable from inside a function is considered bad practice since it can easily introduce bugs that are very difficult to detect and fix. Avoid this if you can!### 3) Conditional executionsIt’s important to be able to write clear and effective conditionals (if, else, etc…) in R. It’s often very useful to check if a condition is satisfied and then do different things depending on the outcome.For this exercise, simply briefly review sections 7.3-7.5 of [this page](https://discdown.org/rprogramming/conditional-execution.html#conditional-execution-if-else-statement) here.### 4) For loopFor loops are a useful way of repeating a step a set number of times.1. Write a function that repeats the following experiment `n` times, with a default `n=1000`: – draw 5 cards from a standard deck of playing cards (hint: for this problem, you can represent a deck as the vector 1,2,…,13 repeated 4 times) – drop the lowest and highest card (if there are ties, just drop one). – take the mean of the 5 numbers and stores them in a vector – return the vector of meansdraw <- function(n=1000) { deck <- rep(1:13, each=4 ); reps <- rep( NA, n); for (i in 1:n ) {hand <- sample( deck, size=5, replace=FALSE );# Instructions unclear; dropping the largest and smallest and taking mean of the three that are left.maxidx <- which.max( hand );minidx <- which.min( hand );reps[i] <- mean( hand[-c(minidx, maxidx)]); return( reps );### 5) Random variables and LLN1. For each of the following, identify one or more random variables that can be used to model the outcome. – The number of cars that pass your house in an hour. __Solution:__ Poisson is the most reasonable, here.- The number of times you need to try before you make a 3-point shot. __Solution:__ Geometric models the number of trials until a success, assuming your shots are independent (of course in practice they aren’t…) – The number of people in a clinical trial who recover after going through an experimental treatment. __Solution:__ Binomial is the standard for this– we have a group, and we treat recoveries of the patients as indepenent events. Thus the total number of successes (i.e., recoveries) is binomial (a sum of Bernoullis). – The number you get when rolling a 20-sided die. __Solution:__ We typically model dice rolls as a uniform distribution on the sides of the die. So this will be probability mass $1/20$ on each of the integers $1,2,dots,20$. 2. Choose a type of random variable that has finite mean (e.g. normal, binomial, poisson, geometric, exponential, uniform, etc…) and choose some parameters. Write down what the theoretical mean of this particular distribution is (you can use Wikipedia to get the expected value for your random variable). __Solution:__ Wikipedia.Randomly generate at least 1000 observations of the variable you chose (if your computer can generate more, go ahead!). Then, use the `running.mean()` function defined below to compute a running mean (i.e. each number in the output is the mean of all the previous numbers in the input). Plot this running mean using the `plot()` function, and use `abline()` to add a horizontal red line at your previously computed theoretical mean.Explain what is happening here. (Hint: is this consistent with the Law of Large Numbers? Why or why not?).# define running average function# can be specified as cumulative sum / index of elementrunning.mean = function(vec) cumsum(vec)/seq(along=vec)data <- rnorm(n=1000);run <- running.mean(data);plot(run);abline(h=0)This behavior is exactly what the LLN predicts– as the sample size becomes large, the sample mean should get close to the population mean.Of course, the LLN doesn’t say anything about how this running mean should look (i.e., the “sample path”– a concept you’ll see in your later theory courses), only that it should asymptote to the mean.程序代写 CS代考加微信: assignmentchef QQ: 1823890830 Email: [email protected]

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] 代写代考 STAT 340: Discussion 01: R review”
30 $