generation of simulated data in R with loop
source link: https://www.codesd.com/item/generation-of-simulated-data-in-r-with-loop.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
generation of simulated data in R with loop
I'm trying to simulate a data set that I will go on to using later. I need to create a variable with 1000 observations (n=1000) where each observation is a function of the previous observation with the following equation: yt is 80% yt-1 + 20% random noise with mean 0 and sd 1. Also, the value of y1 = 1.
This is the code I have so far (it runs, but only provides me with 1000 1's and no other values. Thus, it's not generating the data correctly and I cannot figure out why...):
n <- 1000
i <- rnorm(n=1000, mean=0, sd=1)
data <- rep(1,1000);
for(i in 2:1000){
y[i]=0.80*y[i-1] + 0.20*rnorm(1)
}
data
Any advice or help with this code is appreciated! Thanks!
You have not instantiated the y
vector simply change data
to y
and you should be fine. Here is it all together:
y <- rep(1,1000);
for(i in 2:1000){
y[i]=0.80*y[i-1] + 0.20*rnorm(1)
}
y
Related Articles
Date Array with Loop
problem with the data in a while loop in Java
Disable data on the second loop
Generate an Ajax data string in the loop
Matlab - Recording data from a nested loop
Python: reading timestaps from csv to tables: model data after processing with numpy;
Using promises with loops and nested functions
Date in table with implode and stropos
Collecting data in a for loop (using a different structure than I am used to)
Divide the data into sql with multiple delimiters (, and -) that have a rule
WCF Client How can I deserialize a date format incompatible with the JSON response?
Calling Multiple Objects / Variables & ldquo; Name & rdquo; With Looping
JQuery UI Release date of the date format problem with the initial value
Stored procedure Choosing the operand type: the date is incompatible with int
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK