首页 > > 详细

讲解data留学生、R程序语言调试、R设计辅导、讲解program辅导Python编程|讲解SPSS

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
(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).

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!