R to Fortran

Hi,
I am trying to learn how to call fortran subroutines from a program written in the statistical language R. I read up on this on the web, and got a very simple example to work, but with a more complex example I ran into trouble and I need some help. Attached is a zip file with the files pertaining to the more complex example (a Cox regression analysis), as follows:
Attached is a zip file, with the following relevant files:
1. fnewton.f = the fortran subroutine I am trying to run
2. fnewton.dll = the dll file I created from the fortran file
3. test.r = the R program that calls the fortran
4. coxdat.txt = the data set
(Note: with the first 3 files, I artificially added the extension.txt so that they would be uploadable on Experts Exchange.)
FYI, I have checked the subroutine fnewton.f and verified that it works (ie gives the correct answer for the Cox regression coefficient on the attached dataset).
I'd be grateful for help in working this out.
Thanks,
David R-to-Fortran.zip
LVL 1
David_ZuckerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JesterTooCommented:
David,

What platform (UNIX/Linux, Windows, or OS/X) are you using and which Fortran compiler did you use to build the dll?  Knowing this would be helpful for anyone wanting to duplicate/solve your issue.

I suspect the problem is related to data types and/or calling conventions between R and your Fortran code.

Thanks,
Lynn
David_ZuckerAuthor Commented:
Hi,

Many thanks for your responses. Meantime, someone else helped me solve the problem. In the R file, it should be
beta=as.double(1),
instead of
beta=double(1)
Also, I forgot to include igood in the arguments being returned.
The full R code is appended below.
By the way, to answer Lynn's question, I was using a Windows platform with the gfortran compiler in the MinGW package, and I built the dll using the following command
gfortran -shared -ofnewton.dll fnewton.f

Thanks again and regards,
David

THE R CODE

library(survival)
dyn.load("fnewton.dll")

fnewt = function(t,d,z,beta0) {
  n = 5000
  p = 1
  dim(z) = c(n,1)
  res = .Fortran("fnewton",
    as.integer(n),
    as.integer(p),
    as.double(t),
    as.double(d),
    as.double(z),
    as.double(beta0),
    beta=as.double(1),
    igood=as.integer(1))
    result = c(res$beta,res$igood)
  return(result)
 }

indat = read.table("coxdat.txt")
names(indat) = c("t","d","z")
coxr = coxph(Surv(t,d) ~ z, data=indat)
print(coxr)
t = indat[,1]
d = indat[,2]
z = indat[,3]
beta0 = 0
rslt = fnewt(t,d,z,beta0)
print(rslt)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JesterTooCommented:
Thanks for the update and the points David.  I'm glad you found the solution.

Regards,
Lynn
David_ZuckerAuthor Commented:
I got the answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.