Test whether the function works for x = numeric(0) and for any other numeric
vector x.
3. The Body Mass Index (BMI) is used to determine a person’s corpulence. It is calculated
using the formula:
BMI = Weight (kg)
Height2
(m)
(a) [4 marks] Write an R function that calculates your BMI and round it to 3 decimal
places.
(b) [6 marks] Consider the following BMI and weight categories:
Underweight if BMI < 18.5
Normal weight if BMI ∈ [18.5, 25)
Overweight if BMI ∈ [25, 33)
Obesity if BMI ≥ 33
Write an R function which takes the result of (3a) as a single argument BMI, a
and returns the corresponding category. For example:
> weight.category(21.133)
This corresponds to category: normal weight
4. Consider an asymmetric random walk when the probability of going one step up is 0.55
and the probability of going one step down is 0.45. Let Xn denotes the n-th step in this
random walk. Then, X1, X2, · · · are independent and identically random variables with
Xn =
(
+1 w.p. 0.55
−1 w.p. 0.45
Assume that {Dn}n≥0 is a sequence showing the distance from the origin (starting
point). So,
D0 = 0
Dn = Dn−1 + Xn for n = 1, 2, · · ·
(a) [6 marks] Write an R function which takes the number of steps as its single argument
and returns a vector of distances (for instance shows a sequence of realization
of distances D0, D1, · · · , D10 for the argument n = 10).
Hint: sample() function is helpful.
(b) [6 marks] By using the function in (4a) write a new R function (with single
argument) which generates one realization of the walk (starting from the origin)
and counts the total number of steps taken in the right hand side of the origin
2
(for instance, one example could be the case of starting from the origin with the
sequence of steps
X1 = +1, X2 = −1, X3 = −1, X4 = +1, X5 = −1, X6 = +1.
Then, the sequence of distances is
D0 = 0, D1 = 1, D2 = 0, D3 = −1, D4 = 0, D5 = −1, D6 = 0.
Here, your function should returns 2 as the number of steps taken in the right hand
side of the origin).
3
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。