2023-02-07_coding_practice-sol (1)
Rmd
keyboard_arrow_up
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
---
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
Related Documents
Related Questions
Complete solution. By hand? determine if S={(1, 1, 1); (1, 1, 0); (1, 0, 0)} is linearly independent and spans R^3?
-If possible, give the matlab syntaxes to verify the answer.
arrow_forward
6. Find the largest possible number of independent vectors among
V3 =
V4 =
Justify your answer.
arrow_forward
I need answers all parts with typing clear .
2. AssumeU=Z, and let \[ \begin{array}{l} A=\{\ldots,-6,-4,-2,0,2,4,6, \ldots\}=2 \mathbb{Z}, \\ B=\{\ldots,-9,-6,-3,0,3,6,9, \ldots\}=3 \mathbb{Z}, \\ C=\{\ldots,-12,-8,-4,0,4,8,12, \ldots\}=4 \mathbb{Z} . \end{array} \] Describe the following sets by listing their elements explicitly. (a)A∩B(b)C−A(c)A−B(d)A∩Bˉ(e)B−A(f)B∪C(g)(A∪B)∩C(h)(A∪B)−C
arrow_forward
A set of 3 vectors in IR^4 spans IR^4. Is this true or false?
arrow_forward
obtain the least squares fit for a line
arrow_forward
6. Find the largest possible number of independent vectors among
v1 =
V3 =
V =
Justify your answer.
arrow_forward
When you execute the above Matlab code, you will get an error message.Please choose the reason why the presented Matlab code does not work.
a) Anonymous functions may contain only one variable, not three.
b) The expression in the power might contain a vector. Thus, element-wise operations are needed.
c) When calling an anonymous function, one has to use exactly the same names of the variables as in the definitionof the function.
d) It is not allowed to use the exp()-function in order to compute an value for the variable e beforehand.
arrow_forward
b. Please provide a well explained answer for the follwoing. And explain how to add/subtract vectors? What is the geometric meaning? And what is R^n? what is an ordered n-tuple?
arrow_forward
Graw-Hill Campus
s
Mirra Healthcare
cgi/x/Isl.exe/1o_u-IgNslkr7j8P3jH-IBSmSHQlfbH2xfgdyVelz24_x5rbgiCikTAt5eBX4pb4ZCz8imXugW5nNqFYuEXBgkv5NqH-nAJQV6NnpwLRsfhLW9K57to? 10Bw7QYjlbavbSPXtx-YCjsh_7mMmrq#item
CLAIMS ADJUDICA... Medicare Medicaid Ultimate Health Pla... NPPES NPI Registry New Time Clock
|||
ALEKS Deminika Mesiana - Lea
=
User Portal - Call C...
Ultimate Health Plans
O RATIONAL EXPRESSIONS
Solving a distance, rate, time problem using a rational equation
Rate of the team in still water: miles/hour
X +
A rowing team rowed 50 miles while going with the current in the same amount of time as it took to row 25 miles going against the current. The rate of the
current was 2 miles per hour. Find the rate of the rowing team in still water.
☐
X
0/5
Ś
Demini
arrow_forward
By using python
todo: fill the bipartite adjacency matrix
arrow_forward
Find the inverse of the following permutation matrix
arrow_forward
There is a function saved in the current folder named Q_17. It takes in three arguments, the last one of which is a time vector. The function returns
two vectors which are the same size as the time vector argument.
• Call the function when the input arguments are the variables A, B, and T (in that order).
• Calculate the smallest value returned from the function (from both return vectors).
On the same figure, plot both of the returns versus time.
arrow_forward
make an example and prove it about five vectors spanning R⁴
arrow_forward
Hello. Please answer the attached Linear Algebra question correctly and completely. Please show all of your work.
*If you answer the question correctly and show all of your work, I will give you a thumbs up. Thanks.
arrow_forward
Consider a basis for a space given by vectors
Note that you can type in square roots as "a^0.5"..
-10
-10
-10
-2
6
. Answer the following questions below:
arrow_forward
3) S contains only the vectors (-2+x-x² - 2x³), (4 + x - x² + 2x³), (8 + 5x -5x²+2x³), (10 + 7x - 7x² + 2x³).
The new linearly independent set with the same span would be
arrow_forward
onsider the geometric vectors shown below:
C
aw vector AB plus vector EF
B
D
E
arrow_forward
9.
{(1,5), (-2,3)} is a spanning set for R². Express the vector (4, -3) as a linear
combination of the vectors in the spanning set.
arrow_forward
4.
Express PQ-RQ+SR-PS + RS as a single vector.
arrow_forward
6) This is linear algebra ! write neatly, answer only. Show your work!
arrow_forward
Help me
arrow_forward
If AB = 0 then the column space of B is contained in the _ of A. Why?
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage
Related Questions
- Complete solution. By hand? determine if S={(1, 1, 1); (1, 1, 0); (1, 0, 0)} is linearly independent and spans R^3? -If possible, give the matlab syntaxes to verify the answer.arrow_forward6. Find the largest possible number of independent vectors among V3 = V4 = Justify your answer.arrow_forwardI need answers all parts with typing clear . 2. AssumeU=Z, and let \[ \begin{array}{l} A=\{\ldots,-6,-4,-2,0,2,4,6, \ldots\}=2 \mathbb{Z}, \\ B=\{\ldots,-9,-6,-3,0,3,6,9, \ldots\}=3 \mathbb{Z}, \\ C=\{\ldots,-12,-8,-4,0,4,8,12, \ldots\}=4 \mathbb{Z} . \end{array} \] Describe the following sets by listing their elements explicitly. (a)A∩B(b)C−A(c)A−B(d)A∩Bˉ(e)B−A(f)B∪C(g)(A∪B)∩C(h)(A∪B)−Carrow_forward
- When you execute the above Matlab code, you will get an error message.Please choose the reason why the presented Matlab code does not work. a) Anonymous functions may contain only one variable, not three. b) The expression in the power might contain a vector. Thus, element-wise operations are needed. c) When calling an anonymous function, one has to use exactly the same names of the variables as in the definitionof the function. d) It is not allowed to use the exp()-function in order to compute an value for the variable e beforehand.arrow_forwardb. Please provide a well explained answer for the follwoing. And explain how to add/subtract vectors? What is the geometric meaning? And what is R^n? what is an ordered n-tuple?arrow_forwardGraw-Hill Campus s Mirra Healthcare cgi/x/Isl.exe/1o_u-IgNslkr7j8P3jH-IBSmSHQlfbH2xfgdyVelz24_x5rbgiCikTAt5eBX4pb4ZCz8imXugW5nNqFYuEXBgkv5NqH-nAJQV6NnpwLRsfhLW9K57to? 10Bw7QYjlbavbSPXtx-YCjsh_7mMmrq#item CLAIMS ADJUDICA... Medicare Medicaid Ultimate Health Pla... NPPES NPI Registry New Time Clock ||| ALEKS Deminika Mesiana - Lea = User Portal - Call C... Ultimate Health Plans O RATIONAL EXPRESSIONS Solving a distance, rate, time problem using a rational equation Rate of the team in still water: miles/hour X + A rowing team rowed 50 miles while going with the current in the same amount of time as it took to row 25 miles going against the current. The rate of the current was 2 miles per hour. Find the rate of the rowing team in still water. ☐ X 0/5 Ś Deminiarrow_forward
- By using python todo: fill the bipartite adjacency matrixarrow_forwardFind the inverse of the following permutation matrixarrow_forwardThere is a function saved in the current folder named Q_17. It takes in three arguments, the last one of which is a time vector. The function returns two vectors which are the same size as the time vector argument. • Call the function when the input arguments are the variables A, B, and T (in that order). • Calculate the smallest value returned from the function (from both return vectors). On the same figure, plot both of the returns versus time.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:Cengage
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage