2

generation of simulated data in R with loop

 2 years ago
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

advertisements

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

I have problem with this code. If I print item of daysArray in for loop (console.log(daysArray[i]);) it returns right date, but after for loop returns last date at all daysArray items. Please some could help me. public getFullMonth(date: Date): Array

problem with the data in a while loop in Java

running this code, I am unable to have the number add properly. it only adds the data from the last loop public class P4_3 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter your 8 digit C

Disable data on the second loop

I want to create simple poll system. I have two question and two answers. First Question has two answer but second question hasn't any answer. In a sense I want to get: First question? Answer1 Answer2 Second question? But I'm getting First question?

Generate an Ajax data string in the loop

I have 2 arrays. One for that contains keys and the other that contains input values, like so: var keys = ['id', 'name', 'category']; var values = ['1', 'nameVal', 'catVal']; Now what I'm trying to do is create a data string using a loop to use for a

Matlab - Recording data from a nested loop

I would like to store the data from a double loop in a matrix (not a cell). I tried the following code (the function used in the inner loop here is just an example). valuesforOPratio = zeros(41,1); valuesforM = zeros(41,61); NPVtotal=1; for M = 40:10

Python: reading timestaps from csv to tables: model data after processing with numpy;

I am still trying to come around with python, but this problem exceeds my knowledge: Topic: hydrodynamic postprocessing: csv output of hydraulic software to array, split timesteps Here is the data and how far i came with a working code: Input-file (s

Using promises with loops and nested functions

I am trying to use promises with loops and nested functions that are within some of those loops. I have a series of functions that are supposed to bring back SharePoint list items from REST calls - once those are done executing, then another function

Date in table with implode and stropos

i have a date input javascript that lets users add as many dates onto the database at once, this is using implode and stores as one big string like 2015-01-01 2015-01-02 etc... I also want this to find out if one of those days contains a weekend day.

Collecting data in a for loop (using a different structure than I am used to)

I know this question gets asked a lot. I'm comfortable with collecting data in a for loop by creating an object outside of the loop to iteratively store observations. However, I'm making a k-fold cross validation function, and came across this nice f

Divide the data into sql with multiple delimiters (, and -) that have a rule

I have data like this 1,2,3,4-8,10,11 I want split the data into rows with these 2 rules : The , will only split the data into rows. Ex 1,2,3 become : 1 2 3 The - will split into series number. Ex 4-8 become : 4 5 6 7 8 How can a SQL query do that? P

WCF Client How can I deserialize a date format incompatible with the JSON response?

I have scoured the Net for info on this, but most of the results are about creating WCF services or situations where the service is under your control. I am building a WCF client proxy for a RESTful JSON service which is out of my control. I am using

Calling Multiple Objects / Variables & ldquo; Name & rdquo; With Looping

I got stucked on making looping for adding these Variables to ComboBox, i want to call it with a simpler way with Looping for, but i failed so many times, i've been googling around but i still failed, so any help will be appreciated Public MyPass1 As

JQuery UI Release date of the date format problem with the initial value

I need to set the initial date for the date picker to 03/20/2010 in mm-dd-yyyyy format. I have done this <input id='datepicker' type='text' value='20/03/2010' /> But my problem is on clicking the field date picker populates with today's date as high

Stored procedure Choosing the operand type: the date is incompatible with int

This is my stored procedure ALTER PROCEDURE [dbo].[sp_ListFirmJobFilter] (@Status as nvarchar(10), @CityID as nvarchar(10), @DataStart as date, @DataEnd as date, @FirmID as nvarchar(10) ) AS BEGIN DECLARE @Sql NVARCHAR(MAX) SET @Sql = N'SELECT * FROM

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK