Completely new to R_second try to upload a topic

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.

  1. 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

Please take a look at our homework policy

Also, check this guide about how to make R related questions

In accordance with the homework policy, I'll confine my comment to something that often causes confusion.

-0.2x^2 is a perfectly good algebraic expression, but it is not a valid R statement. Mere adjacency, -0.2x does not imply multiplication. You need the * operator

-0.2 * x

Go back over your code to identify this type of error, and I think you'll find you'll make better progress.

Consider the possibility that the error is not syntactical but that it produces a result that does not satisfy the conditions given. I have no idea if it does or not --- this is just a general heuristic

Thank you very much for your help. I am also considering it might be MyMatrikel = 123 where we have to put our student id number.

I really don't want to abuse your kindness, but could you also help me with the plotting and the lines please. I don't have a clue which variables I need to plot in this case. I know the R commands for plots and lines, which aren't that difficult but having difficulties finding the right variables.

The default base R plot command is a scatterplot on the Cartesian plane of x along the ordinate and y along the abscissa. More complex graphs, are, of course, possible, and you've found the lines option.

By convention, x is the independent variable, and we want to show the values of y, the dependent variable at different values of x.

So, begin by asking yourself a "which came first" question. What values are given (independent) and what values are calculated (dependent).

Without embarrassing myself more, I am giving up. In my opintion X is the independent variable and Y0 and Y are the dependent ones. I tried to plot them but that didn't work, it says X couldn't be found.
On the other hand MyMatrikel and n_sim are given variables. I tried every possible plot combination I could think of. After hours of trying it is time to give up. Thank you very much for your help

There is a classic book How to Solve It by Polya. He says that if you are stuck on a problem, try a similar problem that you have solved before, like plotting simple (x,y).

Use the str command to see the structure of the object that you are trying to plot x may not be called X. Also, only a single dependent variable in linear regression. Only one of Y or Y0.

Hey, everything is fine it was just too late yesterday today everything was super easy

1 Like

Sleep is the only "smart pill" known to work.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.