[R] Caculate first difference from a dataframe; write a simulation
Meenu Sahi
meenusahi at gmail.com
Tue Aug 4 20:12:11 CEST 2009
Dear R Users
I'm writing my first simulation in R.
I've put across my problems with a smaller example in the attachment along
with the questions.
Please help.
Best regards
Meenu
-------------- next part --------------
mydat<-read.table(textConnection("Level spread change State
4.57 1.6 BlF NA
4.45 2.04 BrS NA
3.07 2.49 BlS NA
3.26 -0.26 BlF NA
2.80 0.22 BrF NA
3.22 2.5 BrS NA
4.2 -0.34 BlF NA
3.80 0.35 BrS NA
4.28 1.78 BrF NA
5.4 -0.34 BrF NA
4.89 0.24 BlF NA"), header=TRUE,as.is=TRUE)
mydat3<-data.frame(mydat)
q<-quantile(mydat3[[1]],c(0,0.25,0.75,1))
z.level<-cut(mydat3[[1]],q,include.lowest=T)
summary(z.level)
q<-quantile(mydat3[[2]],c(0,0.25,0.75,1))
z.shape<-cut(mydat3[[2]],q,include.lowest=T)
summary(z.shape)
#to identify States in mydat3
attach(mydat3)
state1<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>=-Inf & Level<=3.24,])
state2<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>=-Inf & Level<=3.24,])
state3<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>=-Inf & Level<=3.24,])
state4<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>3.24 & Level<=4.51,])
state5<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>3.24 & Level<=4.51,])
state6<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>3.24 & Level<=4.51,])
state7<-data.frame(mydat3[spread>=-Inf & spread<= -0.02 & Level>4.51 & Level<=Inf,])
state8<-data.frame(mydat3[spread>-0.02 & spread<= 1.91 & Level>4.51 & Level<=Inf,])
state9<-data.frame(mydat3[spread>1.91 & spread<= Inf & Level>4.51 & Level<=Inf,])
detach(mydat3)
state2<-transform(state2,
State="State2"
)
state3<-transform(state3,
State="State3"
)
state4<-transform(state5,
State="State4"
)
state5<-transform(state5,
State="State5"
)
state6<-transform(state6,
State="State6"
)
state7<-transform(state7,
State="State7"
)
state8<-transform(state8,
State="State8"
)
mydat4<-data.frame(rbind(state2,state3,state4,state5,state6,state7,state8))
## To identify states - can it be done in an easier way?
#################################################################################
##Question1:I want to calculate the first difference of mydat4[,1:2]
t<-diff(mydat4[,1:2],1)
#The command fails, why? Because it fails I've written a code for calculating the first diff
c<-dim(mydat4)
e<-mydat4[-c[1],]
dim(e)
f<-mydat4[-1,]
dim(f)
#f
firstDiff<-data.frame(f[,-c(3:4)]-e[,-c(3:4)])
# Draw a random sample for the first differences
d<-dim(mydat3)
z<-mydat3[d[1],1:2]
z.t<-t(z)
x<-data.frame(t(firstDiff[,1:2]))
y<-sample(x[,1:2],1,replace=T)
ad<-z.t+y
##Output of ad is given below
ad
X6
Level 5.04
spread 0.25
###################################################################################
##Question2: How can I easily identify which out of the 9 states does ad fall into with Level =5.04 and spread=0.25?
##OUTPUT
ad
X6
Level 5.04
spread 0.25
state state8
##################################################################################
##Question3: I want to write a simulation of the order given below: HOw can I in R store each
simulation result in deltay[s,t] where s keeps track of scenarios and t keeps track of time within a scenario
Do I need to declare
See below in 'program-language'.
For (s in 1:10000){
yield[s,1]=z.t
#How can I write yield[s,]<-z.t ?
For (t in 1:60){
deltay[s,t] = sample(x[,1:2],1,replace=T)
yield[s,t+1]=y[s,t]+ deltay[s,t]
}
write scenario s
# how can I write scenario s into a dataframe that can be stored for future calculations
}
More information about the R-help
mailing list