Hi Scott,
 
Thanks for this.
 
Got some questions below: 
 
Thanks
 
Hugh
 


Date: Mon, 23 May 2011 17:32:52 -0500
From: scttchamberla...@gmail.com
To: h_a_patie...@hotmail.com
CC: r-help@r-project.org
Subject: Re: [R] Reading Data from mle into excel?



I would read the datasets into a list first, something like this which will 
make a list of dataframes:
filenames <- dir() # where only filenames you want to read in are in this 
directory 
dataframelist <- lapply(filenames, read.csv, header = TRUE, sep = ",")


OK I tried the code you suggest and I get:
 
 filenames<-dir("C:/Documents and Settings/Hugh/My Documents/PhD/Swaption 
vols.csv")
> dataframelist<-lapply(filenames, read.csv, header = TRUE, sep = ",")
> 
> dataframelist
list()
> list
function (...)  .Primitive("list")

Is this correct?
 
I only actually need one file, all the time series are stored in seperate 
columns.
 
vols=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Swaption 
vols.csv" 
, header=TRUE, sep=",")
X<-ts(vols[,2])

Can I still use this format?





You should be able to put the whole procedure, after reading in dataframes, 
into one lapply perhaps, e.g., 


lapply(dataframelist, yourfunction)
 
 
 
 
as for my function the function mle involves calls to other functions?
 
dcOU<-function(x,t,x0,theta,log=FALSE){
Ex<-theta[1]/theta[2]+(x0-theta[1]/theta[2])*exp(-theta[2]*t)
Vx<-theta[3]^2*(1-exp(-2*theta[2]*t))/(2*theta[2])
dnorm(x,mean=Ex,sd=sqrt(Vx),log=log)
}
OU.lik<-function(theta1,theta2,theta3){
n<-length(X)
dt<-deltat(X)
-sum(dcOU(X[2:n],dt,X[1:(n-1)],c(theta1,theta2,theta3),log=TRUE))
}
require(stats4)
require(sde)
set.seed(1)
#X<-sde.sim(model="OU",theta=c(3,1,2),N=10000,delta=1)
mle(OU.lik,start=list(theta1=1,theta2=1,theta3=1),
method="L-BFGS-B",lower=c(-Inf,-Inf,-Inf),upper=c(Inf,Inf,Inf))->fit
summary(fit)
 
 
 
Should I store each function is a seperate script?
If so how do I then make sure that they are available in the "workspace"?
 
Assuming that vols contains the dataframelist who would I call the mle function 
using lapply like you showed?
 
 
 
 
 
 


where dataframelist is a list of dataframes, and yourfunction is a function 
that does all the procedures for one dataset. The function 'yourfunction' will 
be applied to each dataset in the list separately, then the results output into 
a list. 


Then, if the results from each dataset will have the same dimensions, you can 
do something like ldply using package plyr 
ldply(output, 'identity') # where 'output' is the output list of results from 
the lapply call above
This will give you a  data frame of all the results. 







Scott Chamberlain
Rice University, EEB Dept.
On Monday, May 23, 2011 at 4:32 PM, Bazman76 wrote:


Hi there,

I ran the following code:

vols=read.csv(file="C:/Documents and Settings/Hugh/My Documents/PhD/Swaption
vols.csv" 
, header=TRUE, sep=",")
X<-ts(vols[,2])
#X


dcOU<-function(x,t,x0,theta,log=FALSE){
Ex<-theta[1]/theta[2]+(x0-theta[1]/theta[2])*exp(-theta[2]*t)
Vx<-theta[3]^2*(1-exp(-2*theta[2]*t))/(2*theta[2])
dnorm(x,mean=Ex,sd=sqrt(Vx),log=log)
}
OU.lik<-function(theta1,theta2,theta3){
n<-length(X)
dt<-deltat(X)
-sum(dcOU(X[2:n],dt,X[1:(n-1)],c(theta1,theta2,theta3),log=TRUE))
}

require(stats4)
require(sde)
set.seed(1)
#X<-sde.sim(model="OU",theta=c(3,1,2),N=10000,delta=1)
mle(OU.lik,start=list(theta1=1,theta2=1,theta3=1),
method="L-BFGS-B",lower=c(-Inf,-Inf,-Inf),upper=c(Inf,Inf,Inf))->fit
summary(fit)

#ex3.01 R
prof<-profile(fit)
par(mfrow=c(1,3))
plot(prof)
par(mfrow=c(1,1))
vcov(fit)

I run the code above and I get:



summary(fit)
Maximum likelihood estimation

Call:
mle(minuslogl = OU.lik, start = list(theta1 = 1, theta2 = 1, 
theta3 = 1), method = "L-BFGS-B", lower = c(-Inf, -Inf, -Inf), 
upper = c(Inf, Inf, Inf))

Coefficients:
Estimate Std. Error
theta1 0.03595581 0.013929892
theta2 4.30910365 1.663781710
theta3 0.02120220 0.004067477

-2 log L: -5136.327 

I need to run the same analysis for 40 different time series.

I want to be able to collate all the estimates of theta and the associated
stadard errors and then transfer them into excel?

Can someone please point me to some R code that will allow me to do this?

Thanks



--
View this message in context: 
http://r.789695.n4.nabble.com/Reading-Data-from-mle-into-excel-tp3545569p3545569.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


                                          
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to