2023-02-07_coding_practice-sol (1)

Rmd

School

University of Houston *

*We aren’t endorsed by this school

Course

404

Subject

Statistics

Date

Apr 3, 2024

Type

Rmd

Pages

6

Uploaded by student4781

Report
--- title: "STAT 404 Midterm1-Coding" author: "Your name" output: word_document: default --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE,error=TRUE) ``` ## IMPORTANT!!!! include your libraries here ```{r} ``` ## Coding questions Q1. (a) Create a vector with elements 1,3,2,10,11,12. Call it x.seq ```{r} # 1(a) x.seq=c(1,3,2,10,11,12) ``` (b) Create a matrix as follows. Call it x.mat ```{r} #[1,] 1 3 #[2,] 2 10 #[3,] 11 12 #1(b) x.mat=matrix(c(1,3,2,10,11,12),3,2) ``` (c) Change the element in the second row and second column of x.mat to be NA. ```{r} #1(c) x.mat[2,2]=NA ``` (d) remove the first, the second and the last elements in x.seq ```{r} #1(d) x.seq[-c(1,2,length(x.seq))] ``` (e) find the maximum value for each row of x.mat from question 1.(c). ```{r} # 1(e) apply(x.mat,1,max,na.rm=T) ```
(f) write a one-line code to generate a sequence of strings as below. ```{r} # [1] "2 is an even number" # [2] "4 is an even number" # [3] "6 is an even number" # [4] "8 is an even number" # [5] "10 is an even number" # [6] "12 is an even number" # [7] "14 is an even number" # [8] "16 is an even number" # [9] "18 is an even number" # [10] "20 is an even number" paste(seq(2,20,by=2), 'is an even number') ## there was a typo. I wrote 'odd' number there .... ## for this question, they will only get credit if they use paste function. if they use c('',''), they won't get credit. ``` Q2. Write a R program to (a) create a blank matrix with 5 rows and 3 columns. ```{r} # 2.(a) matrix(NA,5,3) ``` (b) create a matrix with 8 rows and 3 columns, where the first column is 1,2,...,8, the second column is $1^2 ,2^2,...,8^2$, and the third column is $1^3,2^3,\ cdots,8^3$. ```{r} # 2.(b) cbind(1:8, (1:8)^2,(1:8)^3) ``` (c) Use your UIN as the seed number to create a vector of 5 random numbers from a standard normal distribution (call it x), and create another vector saving the corresponding CDFs (call it x.cdf). ```{r} # 2.(c) set.seed(1234) x=rnorm(5) x.cdf=pnorm(x) ``` Q3. Load A from the following code. ```{r} data(ability.cov) A=ability.cov$cov ```
Write a R program to (a) check whether A is a matrix. ```{r} #3(a) is.matrix(A) ``` (b) find the dimension (how many rows and columns) of this matrix ```{r} # 3(b) dim(A) ``` (c) access the element of (1) 3rd column and 2nd row; (2) only the 3rd row; (3) only the 4th column of A. ```{r} #3.(c) A[2,3] A[3,] A[,4] ``` (d) Let B=log(A), i.e., B[i,j]=log(A[i,j]). Calculate the matrix multiplication of A and B (ie, AB). ```{r} # 3(d) B=log(A) A%*%B ``` (e) Define a new matrix D having the same dimension as A, where D[i,j]=A[i,j] when A[i,j]>=50, and D[i,j]=0 when A[i,j] <50. ```{r} #3(e) D=A D[A>50]=A[A>50] D[A<50]=0 ``` (f) write a code to find the row and column indices of the maximum value in A. ```{r} #3(f) which(A == max(A), arr.ind = TRUE) ``` Q4. (a) Create a dataframe called 'df' with two columns as follows: ```{r} # species glengths # ecoli 4.6 # human 3000 # corn 50000 # 4(a) df=data.frame(species=c('ecoli','human','corn'),glengths=c(4.6,3000,50000)) ```
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
(b) Use three different ways to extract the second column ```{r} # 4(b) df$glengths df[,2] df[,'glengths'] ``` Q5. (a) Create a list called `list_hw` with three components, the `glengths` vector, the dataframe `df`, and `number` value. `list_hw` has the following structure $glengths [1] 4.6 3000.0 50000.0 $df species glengths 1 ecoli 4.6 2 human 3000.0 3 corn 50000.0 $number [1] 8 ```{r} list_hw = list(glengths = c(4.6,3000.0,50000.0),df = data.frame(species = c("ecoli","human", "corn"),glength = c(4.6,3000.0,50000.0)), number = 8) ``` (b) Use 'list_hw' to return the second component of the list: ```{r} list_hw[2] ``` (c) return `50000.0` from the first component of the list: ```{r} list_hw[[1]][3] ``` (d) return the value `human` from the second component: ```{r} list_hw[[2]]$species[2] ``` (e) give the components of the list the following names: "genome_lengths", "genomes", "record" ```{r} names(list_hw) = c("genome_lengths","genomes","record") ``` (f) Use one of the apply functions to show the class for each component of list_hw. ```{r} sapply(list_hw,class) # or lapply(list_hw,class) ``` Q6. (a) Generate 10 random numbers from a Poisson distribution with lambda= 3.
```{r} x = rpois(10,lambda = 3) ``` (b) calculate x_min+x_max, where x_min is the smallest value and x_max is the largest value among the 10 numbers generated from (a) ```{r} min(x) +max(x) ``` (c) Repeat your experiment for 100 times. Use a matrix to save x_min and x_max. ```{r} x.mat = c() for (i in 1:100){ x = rpois(10,lambda = 3) total = min(x) +max(x) x.mat = rbind(x.mat,c(min(x),max(x))) } ``` (d) Write a function called myfun with two inputs: lambda: a required input argument, N.rep: an optional argument with a default value 100, And the output is a vector containing the sample mean and sample standard deviation of x_min+x_max. ```{r} myfun = function(lambda,N.rep= 100){ x.seq=c() for (i in 1:N.rep){ x = rpois(10,lambda=lambda) x.seq = c(x.seq,min(x) +max(x)) } return(c(mean(x.seq),sd(x.seq))) } ``` Q7, (a) Write a function with three inputs a1, a2 and N to generate a sequence X from the following autoregressive time series model: $X(t+2)=a1*X(t+1)+a2*X(t)+eps(t)$, for t=0, 1,..., N, where X(0)=0, X(1)=1, eps(t) are iid normal with mean 0 and variance 0.1. The output is a vector X(0), ..., X(N). Set the default values for a1 and a2 to be 0.1 and 0.5, respectively. ```{r} arfun=function(N,a1=0.1,a2=0.5){ x.seq=c(0,1) for (t in 3:(N+1)){ x.seq[t]=a1*x.seq[t-1]+a2*x.seq[t-2]+rnorm(1,0,sd=sqrt(0.1)) } return(x.seq) } ``` (b) Use the above function to generate a sequence with N=100, a1=0.1,a2=0.5 and plot it against t.
```{r} N=100 x.seq=arfun(N) plot(0:N,x.seq,type='l') ``` Q8. Write a function using if or if...else statement to convert a given input score to a S/U grade. - 70 or above is equivalent to the S grade - 69 or below is equivalent to the U grade ```{r} converScore = function(grade){ if(grade >= 70) { return ("S") }else{ return ("U") } } converScore(75) ```
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help