{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# u2 - #Code is broken into 2 parts Part 1 and Part 2#Part 1...

This preview shows pages 1–3. Sign up to view the full content.

Sheet1 Page 1 #Code is broken into 2 parts, Part 1 and Part 2 #Part 1 rm(list=ls(all=TRUE)) ls() n<- c(25,30,36,43,52,62,75,90,107,129,155,186,223,267,321,385,462,555,666,799,958,1150,1380,1656,1987,2385) l <- length(n) k = 1000 r = 500 #compute avg relative error are <- function(b,v) { l <- length(b) c <- 1/(l*v) sum <- 0 for(i in 1:l) { sum = sum + abs( v-b[i] ) } return(c*sum) } #bootstrap function for first question #returns variance of median bootstrapping and medians boo <- function(data,r) { median <- matrix(nrow=r,ncol=1) m <- length(data) for(i in 1:r) { s<-sample(1:m,size=m,replace=T) median[i,1] <- median(data[s]) } return( list(v=var(median),m=median) ) } #first question genkmed <- function(n) { median.u <- matrix(nrow=k,ncol=1) median.chi <- matrix(nrow=k,ncol=1) var.b.u <- matrix(nrow=1000,ncol=1) var.b.chi <- matrix(nrow=1000,ncol=1) for(i in 1:k) { data.u<- runif(n) median.u[i,1] <- median(data.u) data.chi<-rchisq(n,df=3)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Sheet1 Page 2 median.chi[i,1] <- median(data.chi) #bootstrap var.b.u[i,1] <- boo(data.u,r)\$v var.b.chi[i,1] <- boo(data.chi,r)\$v } var.u <- var(median.u) var.chi <- var(median.chi) return(list(vu=var.u,vchi=var.chi,vbu=var.b.u,vbchi=var.b.chi)) } areu <- matrix(nrow = l,ncol=1) arechi <- matrix(nrow =1, ncol=1) for(i in 1:l) {
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 10

u2 - #Code is broken into 2 parts Part 1 and Part 2#Part 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online