Question

# 3. The following code draws a sample of size $n=30$ from a chi-square distribution with 15 degrees of freedom, and then put(3c) The theoretical variance of a chi-square random variable with 15 degrees of freedom is 2(15)=30, so the theoretical variModify the function slightly so that it returns the value: $\sum_{i=1}^n e^{x_i}$ In other words, we want to write a function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

3a) R code

***{r} #add you own code below this line var (bootstrapmedians)

get this

[1] 1.276356

ans: the variance of the bootstrapped estimate medians is 1.2764

3b) R code

***{r} #add you own code below this line set. seed (117888) data=rchisq(30,15) M=matrix(rep (9,30*200), byrow=T, ncol=30) for

get this

[1] 1.258912

ans: The variance of bootstrapped mean is 1.2589

3c) The theoretical variance of the sample mean is 1. The variance of bootstrapped mean is 1.2589.

ans: Yes, the bootstrap has done a reasonable job of estimating the variance of the mean as the estimated variance is 1.2589 and it is close to the theoretical variance, which is 1.

4) As given below,

- **{r} myfun=function(){ if(length(x)=-1){return(x) return x, when there is only one element in x } else return(x[1]+myfun(x

The line x[1]+myfun(x[-1]), adds the first element of x to the output from myfun(x[-1]), where x[-1] indicates the vector x without the first element. That means for each recursive call of myfun, we keep adding the first element of the remaining x to the sum of first elements. We do this till there is only one element left in x. Hence myfun(x) gives the sum of the elements of the vector x.

For example, if we run the code, we should get the sum of 1+2+...+1000=(100+1)*1000/2=500500

R code

myfun=function() { if(length(x)==1){return(x) }else return(x[1]+myfun (x[-1])) myfun(C(1:1000)) sum(c(1:1000)) }

get this

[1] 500500 [1] 500 500

indicating both myfun(x) and sum(x) give the sum of the elements of vector x.

ans: myfun(x) gives the sum of the elements of the vector x. In fact it simulates the function sum(x)

R code to get

\sum_{i=1}^ne^{x_i}

1596246058983_image.png

get this

[1] 31.6847

All the code in text format

code given
```{r}
set.seed(117888)
data=rchisq(30,15)
M=matrix(rep(9,30*200),byrow=T,ncol=30)
for (I in 1:200) M[I,]=sample(data,30,replace=T)
bootstrapmedians=apply(M,1,median)
```

part 3a)

```{r}
#add you own code below this line
var(bootstrapmedians)
```

part 3b)

```{r}
#add you own code below this line
set.seed(117888)
data=rchisq(30,15)
M=matrix(rep(9,30*200),byrow=T,ncol=30)
for (I in 1:200) M[I,]=sample(data,30,replace=T)
bootstrapmeans=apply(M,1,mean)
var(bootstrapmeans)
```

part 4)


```{r}
myfun=function(x){
if(length(x)==1){return(exp(x))
}else
return(exp(x[1])+myfun(x[-1]))
}
myfun(c(2,1,3,0.4))
```

Add a comment
Know the answer?
Add Answer to:
# 3. The following code draws a sample of size $n=30$ from a chi-square distribution with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with the R code! Thanks! Assume that your data consists of x1, . ....

    Please help with the R code! Thanks! Assume that your data consists of x1, . . , strap sample, we sample with replacement of these n points to obtain a set of IID new points Xi,... , X" such tha , Tn, n values. When we generate the boot- for each l. This new dataset, X* , X*, is called a bootstrap sample (a) (1 pt) Show that the bootstrap sample is an IID random sample from Fn, where TI...

  • (4 points) Let Xi, , Xn denote a randon sample from a Normal N(μ, 1) distribution, with 11 as the...

    please answer with full soultion. with explantion. (4 points) Let Xi, , Xn denote a randon sample from a Normal N(μ, 1) distribution, with 11 as the unknown parameter. Let X denote the sample mean. (Note that the mean and the variance of a normal N(μ, σ2) distribution is μ and σ2, respectively.) Is X2 an unbiased estimator for 112? Explain your answer. (Hint: Recall the fornula E(X2) (E(X)Var(X) and apply this formula for X - be careful on the...

  • 1. Let X be an iid sample of size n from a continuous distribution with mean...

    1. Let X be an iid sample of size n from a continuous distribution with mean /i, variance a2 and such that Xi e [0, 1] for all i e {1,...,n}. Let X = average. For a E (0,1), we wish to obtain a number q > 0 such that: (1/n) Xi be the sample Р(X € |и — 9. и + q) predict with probability approximately In other words, we wish to sample of size n, the average X...

  • a) Use the following R code to empirically check the Central Limit Theorem via simulation .n...

    a) Use the following R code to empirically check the Central Limit Theorem via simulation .n <- 40 # sample size m <- c(1:200) #create a vector of length 200 for (i in 1:200) { #simulate 200 samples x <- rnorm(n) m[i] <- mean(x) } hist(m) b) Repeat part (a) with n=200 and compare the histograms. Describe what you observe and what you expect when n increases. c) Repeat parts (a) and (b) with runif() and rexp() respectively instead of...

  • LU 22 2. We know that the sample variance follows a chi-square distribution: Sanx?(n-1). (a) (5...

    LU 22 2. We know that the sample variance follows a chi-square distribution: Sanx?(n-1). (a) (5 points) Use this fact to show that E(S) = 02. (Hint: Find the mean of the x as then mean of a Gamma distribution.) (b) (5 points) Use Markov's inequality to find an upper bound on the probability that the sample variance is twice the true variance, i.e. P(S? > 20%).

  • 1. A pumpkin farmer weighed a simple random sample of size n = 20 pumpkins, with these results: 9...

    1. A pumpkin farmer weighed a simple random sample of size n = 20 pumpkins, with these results: 9.6, 8.8, 5.1, 9.7, 9.1, 8.9, 8, 9.2, 2.7, 9.1, 8.5, 7.3, 9.3, 9.6, 4.1, 9.9, 7.6, 9, 7.2, 8.5 (a) Create a QQ plot of the weights. Do you think it is reasonable to assume that the population distribution is normal? Explain your answer. (b) Regardless of your answer to (a), use R to perform the bootstrap with 2000 resamplings to...

  • Show that the mean X bar of a random sample of size n from a distribution...

    Show that the mean X bar of a random sample of size n from a distribution having probability density function f(x;θ)=(1/θ)e-(x/θ) , ,0 < x < ∞ , 0 < θ < ∞ , zero elsewhere, is an unbiased estimator of θ and has variance θ2/n.

  • Code in R Part 1. a) Run the following lines: n<-30 x<-matrix(rnorm(n * 1000), 1000,n) xL,1:3]<-xl,1:3]*10...

    Code in R Part 1. a) Run the following lines: n<-30 x<-matrix(rnorm(n * 1000), 1000,n) xL,1:3]<-xl,1:3]*10 y<-1+matrix(rnorm (n * 1000), 1000, n) Note: the samples in "x" are contaminated. b) Conduct 1000 two-sample two-sided t-tests for the associated rows of "xand "". (e.g., for the first test, the two samples are "x[1," and "y[1,]", for the second test, the two samples are "x[2," and "y[2,1",etc.). Calculate the total number of rejections of the 1000 tests. (Use significant level a =...

  • Please Provide Code for above question , be sure that the C code compiles using GCC...

    Please Provide Code for above question , be sure that the C code compiles using GCC Task 6: Arrays: Statistics Calculations Write a C program which accepts a "large" mumber of real numbers and calculates their sample mean and sample variance. Revision: From statistics, the sample variance, ,of N samples, X1, X2,..XN, can be calculated as: N mcan(X) ) 2. (Xt (1) N-1 There are many algorithms for calculating a data set's variance. For this task you should implement the...

  • Problem 3. If Xi,... . Xio are a random sample from a Normal distribution N(2,32) and...

    Problem 3. If Xi,... . Xio are a random sample from a Normal distribution N(2,32) and 10 - X is the sample mean. (a) What is E(X)? (0.25 point) (b) What is Var(X)? (0.25 point) (c) What is the distribution of X? Please specify the name of the distribution, mean, and variance of the distribution. (0.25 point) d) What is the probability that the random sample mean falls in the interval [1.5,2.5] (0.25 point)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT