Hi to all the experts in R,
I am trying to solve some tasks for my course at the university and would like to get a little help. Thank you in advance.
The first task was to find 6 errors in the codes, I found 5 and I am not sure about the 6th one. First of all here is the task:
Y = −0.2X^2 * sin(3X) + 2X + ε with X ∈ [0, 1] and ε ∼ N (0, 1)
The code returns three matrices Sim2_20, Sim2_50 and Sim2_150, the first one containing 20 X and
Y values, the second 50 values and the third 150 values.
- Task:The code contains 6 errors. Find the errors and correct them, then describe the errors.
Here is the code with the mistakes:
MyMatrikel = 123
n_sim = c(20, 50, 150)
y_simulate = function {
-0.2x^2 * sin(3x) + 2x
}
sim_func = function(n, seed) {
X = seq(from = 0, to = 1, length_out = n)
Y0 = y_simulate(X)
set.seed(seed)
Y = Y0 + rnorm(n)*0.5
output = list(X, YO, Y)
names(output) = ("X", "Y0", "Y")
return(output)
}
Sim20 = sim_func(n_sim[1], MyMatrikel)
Sim50 = sim_func(n_sim[2], MyMatrikel)
Sim150 = sim_func(n_sim[3] MyMatrikel)
And here is the code corrected by me:
MyMatrikel = 6004429
n_sim = c(20, 50, 150)
y_simulate = function(x) {
-0.2x^2 * sin(3x) + 2*x
}
sim_func = function(n, seed) {
X = seq(from = 0, to = 1, length.out = n)
1
Y0 = y_simulate(X)
set.seed(seed)
Y = Y0 + rnorm(n)*0.5
output = list(X, Y0, Y)
names(output) = c("X", "Y0", "Y")
return(output)
}
Sim20 = sim_func(n_sim[1], MyMatrikel)
Sim50 = sim_func(n_sim[2], MyMatrikel)
Sim150 = sim_func(n_sim[3], MyMatrikel)
ERRORS:
Line 3: Missing (x) after function
Line 4: Missing * between sin(3 and x)
Line 7: length_out replaced by length.out
Line 12: Missing c before ("X", "Y0", "Y")
Line 17: Missing , after n_sim[3]
6th error: Line 4 missing "ε" ????
Thank you in advance for helping me with this first task