我需要模拟一个随机变量,该变量没有共同的分布,并且与另一个变量相关。 我为此变量编写了代码。 变量具有这种分布类型
这是代码:
#variable simulation
rm(list=ls())
v1=rep(NA,380)
n=length(v1)
muv1=55
sv1=2
d=rbinom(n,1,0.90)
trend=seq(0.9, 1, length.out=n/6) #dividing the in period in 6
ntrend=rep(trend,6)
plot(ntrend,main="Non Stoc Trend")
v1=round(rnorm(n=380,muv1,sv1))*d*ntrend
x11()
plot(v1,type="lines",main="first try Simulated Var Non Stocastic Trend ")
#Add a Stochastic Trend
#rm(list=ls())
#sf#stochastic factor
n=length(v1)
d=rbinom(n,1,0.92)
cicle=6 #dividing period in 6 parts
i=1
ntrend1=rep(0,n)
store_sf=NULL
store_trend=NULL
store_lt=NULL
trend1=NULL
#START OF THE SIMULATION OF THE RANDOM VARIABLE
lt=1 #first lt for initializing the process
for(i in i:cicle) {
i=i
repeat{ #DO WHILE cos i need sf!=0 be positive
sf=abs(rnorm(1,2,4)) #non metto round() perché limita troppo il sf
if(sf!=0) {break
}
}
store_sf=c(store_sf,sf)
lt.out=round((n+sf)/(cicle*sf))
#creating indexes for substituting in ntrend1
La=which(ntrend1==0)
La=La[1] #find idx for the first 0 element
Lb=La+lt.out #find idx of the last filled element
Last=n
empty=Last-Lb #last empty value-last filled value
if(i==cicle | empty<0) {
Lb=n
}
trend1=seq(0.9, 1, length.out=lt.out)
lt=length(trend1)
if(i==cicle & lt!=empty) {rm(trend1)
trend1=seq(0.9, 1, length.out=empty)
lt=length(trend1)
store_lt=c(store_lt,lt)
}
else
{
store_trend=c(store_trend,trend1)
lt=length(trend1) #trend length
store_lt=c(store_lt,lt)
}
ntrend1[La:Lb]=replace(ntrend1,La:Lb,trend1)
#Sys.sleep(4)
print(ntrend1)
print(i)
print("lt")
print(lt)
print("empty")
print(empty)
flush.console()
end}
store_sf
store_lt
x11()
plot(ntrend1,main="Stochastic Trend")
length(ntrend1)
v1.1=round(rnorm(n=n,muv1,sv1))*d*ntrend1
x11()
plot(v1.1,type="lines",main="Simulated Series 2 Stochastic Trend")
我的问题是:如何在考虑到与另一个或多个随机变量的相关性的同时,模拟遵循此特定分布的随机变量?