2 years ago
#15476
Xiang Yi
Question on loop when integrating vectors into dataframe
Happy newyear! Simulating a model pertaining to circulation, I encounter some questions here! I generate Y correctly as it makes no sense and adds NAs on dataframe into which I am planning to simulate Y 5000 times to integrate. I don't have a idea on that,and really appreciate for any help or suggestions, Guys! Thanks!
[#Investment simulation
library(tidyverse)
library(ggplot2)
library(statmod)
library(invGauss)
library(dplyr)
library(estimatr)
Res<-as.data.frame(matrix(nrow = 27))
k=1
while(k <=5000){#this circulation probably collapesed
Y=c(1610,1550)
DeviaY=c(0)
Pos_value=c(0)
Years<-c(1986,1987)
DiffY=c(0)
prob_poi=(6/13)*(exp(1)^(-(6/13)))
expec<-c()
q <- rinvgauss(27, mean=0.5, disp=0.5) # generate vector of 10 random numbers
p <- pinvgauss(q, mean=0.5, disp=0.5) # p should be uniformly distributed
for(i in 3:27){
regree<-lm_robust(Y~Years)
expec<- predict(regree,newdata=data.frame(Years=1985+i))
DiffY\[i-1\]=0.7*(Y\[i-1\]-tail(expec,1))
DeviaY\[i-1\]=p\[i\]*sd(Y)
Pos_value\[i-1\]=sample(x=c(-1,1,0),replace = T,size = 1,prob = c(prob_poi/2,prob_poi/2,1-prob_poi))
Y\[i\]<- Y\[i-1\]-DiffY\[i-1\]+DeviaY\[i-1\]+Pos_value\[i-1\]*rinvgauss(1,mean = 60,dispersion = 25)
Years\[i\]<-1985+i
if(i == 27) return(Y)
}
Res\[,k\] <- Y#and corresponding to the first'while' on line 10
k<- k+1 #and corresponding to the first'while' on line 10
} #and corresponding to the first'while' on line 10
#My purpose is to repeat the correct calculating processes on line11 to line30 for framing dataframe,but it returns NAs and it make no sense.][1]
r
loops
statistics
economics
0 Answers
Your Answer