[R] Using the digest and t distribution.

2012-01-28 Thread Rohit Pandey
Hello R community,

I have two questions:
The first might be one of the silliest ever posted here and I
apologize if I've missed some thing very obvious. It relates to using
this digest. When I subscribed to the forum, I had chosen the digest
option that bundles all mails every day into a single digest.

Now, I posted a question a while ago on the logistic regression
function and got no reply. After a while, I realized that it was
probably bundled up in one of the digests. So, looked through them and
found my question in Vol107, issue 16. My question was number 34 in it
and some one had posted a solution to it which appeared in item number
42. However, when I scrolled down to the bottom of the digest, it went
only up to 24. In any case, I've tried searching a a variety of other
things and just can't find the solution posted to my question in the
body (I couldn't even thank the solution provider). Can some one
please tell me where  I can find it and if there is an easy way to
access particular mails sent to you.

My second question is related to the fitting the t distribution with
the fitdistr function. I am actually trying to fit it to the standard
data set in the Ecdat package. Here is my code:
library(ecdat)
data(SP500)
#Fitting a normal distribution runs just fine.
 fitdistr(SP500[[1]],normal)
   meansd
  0.0004180994   0.0108610082
 (0.0002058797) (0.0001455789)
#But when I try the t:
 fitdistr(SP500[[1]],t)
m  s  df
  0.0005236326   0.0083937340   9.984297
 (0.0001721041) ( NaN) ( NaN)
There were 17 warnings (use warnings() to see them)

#I get these NaN and warnings.
Am I doing some thing wrong here?

Also, if some one relies, could you please mark any reply to my
personal ID as well (rohitpandey...@gmail.com)?

-- 
Thanks in advance,
Rohit

__
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.


[R] Need help interpreting the logit regression function

2012-01-15 Thread Rohit Pandey
Hello R community,

I have a question about the logistic regression function.
Specifically, when the predictor variable has not just 0's and 1's,
but also fractional values (between zero and one). I get a warning
when I use the glm(formula = ... , family = binomial(link =
logit)) which says:

In eval(expr, envir, enclos) : non-integer #successes in a binomial glm!

I looked this up on a few forums and got the impression that I could
go ahead and ignore this warning. If that is not so, please suggest
another function that can be used (and no need to read further).

But if this is usable, then the numbers are not making sense for me.

What I am seeing is best explained through an example:

#Setting up the data:
c(1,1,0,0,1,1,0,1,1,0,0,0,0,1,1,0,0,0,0,1)-resp
indep-rep(2:3,10)
#Running the logistic regression:
glm(formula = resp~as.factor(indep) , family = binomial(link = 
logit),na.action = na.pass)
#Gives me:
Coefficients:
  (Intercept)  as.factor(indep)3
   -1.692e-16 -4.055e-01
table(indep,resp)
 resp
indep 0 1
2   5 5
3   6 4

Meaning that the odds for '2' to have a favorable outcome are 5/5 =1;
and the odds for 3 are 4/6 = 0.

And indeed, exp( -1.692e-16) = 1 and exp( -1.692e-16+-4.055e-01) = 0.66.

But when I change all the 1's of the independent variable to 0.5, the
odds should both be half of what they were before (right?).

However, when I run the logistic function after doing this,

resp[resp==1]=0.5
glm(formula = resp~as.factor(indep) , family = binomial(link = 
logit),na.action = na.pass)
(Intercept)  as.factor(indep)3
  -1.0986-0.2877

Now, exp(-1.0986) = 0. and exp(-1.0986-0.2877) = 0.25.

These are nothing like the previous odds halved. So, either I am using
the function wrong or interpreting it wrong. Can some one please point
me in the right direction..

-- 

Thanks in advance,

Rohit

__
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.


Re: [R] help with the maxBHHH routine

2011-05-04 Thread Rohit Pandey
Hi Andrew, Ravi and Arne,

Thank you so much for your prompt replies. I see that all of you mention the
need for simple, reproducible code. I had thought of doing this, but the
functions I was using for the observation level gradient and likelihood
function were very long. I will paste them below here.

Also, sorry for the ambiguity with the 1000's of observations and 821
parameters on the one hand and the 10 * 2 matrix on the other. The latter
is a toy data set and the former is the real data set I ultimately hope to
apply this routine to once it works. Also, sorry for not mentioning the fact
that the maxBHHH function I am using is from the maxLik package (thanks,
Ravi for pointing out).

So, the code that is giving me the errors is:

maxBHHH(logLikALS4,grad=nuGradientC4,finalHessian=BHHH,start=prm,iterlim=2)

and

maxBHHH(logLikALS4,grad=nuGradientC4,finalHessian=BHHH,start=prm,iterlim=2)
Where nuGradientC4 returns a 2*10 matrix and nuGradientC5 a 10*2 matrix
(there are 10 parameters and 2 observations).

I have attached the required functions in the .R file.

These make for some pretty long code, but all you have to do is either load
the file or paste the contents into your R console (and maybe see that
they're returning what they're supposed to). I'm sorry I couldn't think of a
way to come up with a shorter version of this code (I tried my best).

Once you load the file, you should see the following:


#The observation level likelihood function
 logLikALS4(prm)
 1  2
-0.6931472 -0.6931472

#The observation level gradients
 nuGradientC4(prm)
   1  2 3 4  5  6
7  8 910
2 -0.3518519  0.3518519 0.000 0 -0.1481481 -0.167  0.1481481
0.167 0.000 0.000
4  0.000 -0.3518519 0.3518519 0  0.000  0.000 -0.167
-0.1481481 0.167 0.1481481
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'


 nuGradientC5(prm)
2  4
1  -0.3518519  0.000
2   0.3518519 -0.3518519
3   0.000  0.3518519
4   0.000  0.000
5  -0.1481481  0.000
6  -0.167  0.000
7   0.1481481 -0.167
8   0.167 -0.1481481
9   0.000  0.167
10  0.000  0.1481481
Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

Ignore the warning messages.

The errors are:


maxBHHH(logLikALS4,grad=nuGradientC4,finalHessian=BHHH,start=prm,iterlim=2)
Error in checkBhhhGrad(g = gr, theta = theta, analytic = (!is.null(attr(f,
:
  the matrix returned by the gradient function (argument 'grad') must have
at least as many rows as the number of parameters (10), where each row must
correspond to the gradients of the log-likelihood function of an individual
(independent) observation:
 currently, there are (is) 10 parameter(s) but the gradient matrix has only
2 row(s)
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

 and:


maxBHHH(logLikALS4,grad=nuGradientC5,finalHessian=BHHH,start=prm,iterlim=2)
Error in gr[, fixed] - NA : (subscript) logical subscript too long
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'

Again, thanks for your patience and help.

Rohit


On Wed, May 4, 2011 at 4:44 AM, Andrew Robinson 
a.robin...@ms.unimelb.edu.au wrote:

 I suggest that you provide some commented, minimal, self-contained,
 reproducible code.

 Cheers

 Andrew

 On Wed, May 04, 2011 at 02:23:29AM +0530, Rohit Pandey wrote:
  Hello R community,
 
  I have been using R's inbuilt maximum likelihood functions, for the
  different methods (NR, BFGS, etc).
 
  I have figured out how to use all of them except the maxBHHH function.
 This
  one is different from the others as it requires an observation level
  gradient.
 
  I am using the following syntax:
 
  maxBHHH(logLik,grad=nuGradient,finalHessian=BHHH,start=prm,iterlim=2)
 
  where logLik is the likelihood function and returns a vector of
 observation
  level likelihoods and nuGradient is a function that returns a matrix with
  each row corresponding to a single observation and the columns
 corresponding
  to the gradient values for each parameter (as is mentioned in the online
  help).
 
  however, this gives me the following error:
 
  *Error in checkBhhhGrad(g = gr, theta = theta, analytic =
 (!is.null(attr(f,
  :
the matrix returned by the gradient function (argument 'grad') must
 have
  at least as many rows as the number of parameters (10), where each row
 must
  correspond to the gradients of the log-likelihood function of an
 individual
  (independent) observation:
   currently

[R] help with the maxBHHH routine

2011-05-03 Thread Rohit Pandey
Hello R community,

I have been using R's inbuilt maximum likelihood functions, for the
different methods (NR, BFGS, etc).

I have figured out how to use all of them except the maxBHHH function. This
one is different from the others as it requires an observation level
gradient.

I am using the following syntax:

maxBHHH(logLik,grad=nuGradient,finalHessian=BHHH,start=prm,iterlim=2)

where logLik is the likelihood function and returns a vector of observation
level likelihoods and nuGradient is a function that returns a matrix with
each row corresponding to a single observation and the columns corresponding
to the gradient values for each parameter (as is mentioned in the online
help).

however, this gives me the following error:

*Error in checkBhhhGrad(g = gr, theta = theta, analytic = (!is.null(attr(f,
:
  the matrix returned by the gradient function (argument 'grad') must have
at least as many rows as the number of parameters (10), where each row must
correspond to the gradients of the log-likelihood function of an individual
(independent) observation:
 currently, there are (is) 10 parameter(s) but the gradient matrix has only
2 row(s)
*
It seems it is expecting as many rows as there are parameters. So, I changed
my likelihood function so that it would return the transpose of the earlier
matrix (hence returning a matrix with rows equaling parameters and columns,
observations).

However, when I run the function again, I still get an error:
*Error in gr[, fixed] - NA : (subscript) logical subscript too long*

I have verified that my gradient function, when summed across observations
gives the same results as the in built numerical gradient (to the 11th
decimal place - after that, they differ since R's function is numerical).

I am trying to run a very large estimation (1000's of observations and 821
parameters) and all of the other methods are taking way too much time
(days). This method is our last hope and so, any help will be greatly
appreciated.

-- 
Thanks in advance,
Rohit
Mob: 91 9819926213

[[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.


[R] Fwd: The results of your email commands

2011-04-08 Thread Rohit Pandey
Hi R community,

I posted a question on using the R maximum likelihood functions a short
while ago and got an email saying that some of the content was
unprocessed. Hence, I am reposting the question just to be sure (sorry for
the multiple emails if both reached you).

My question is reagarding the way we use constraints in programs like
maxBFGS, etc.

We need to put two matrices into a list, containing the mutipliers on the
LHS of the constraint equations and the constants on the RHS.
I want to constrain the last parameter of my routine to a negative value.

Hence, I specify:
lhs=matrix(0,nrow=noOfParameters,ncol=noOfParameters)
lhs[noOfParameters,noOfParameters]=-1

rhs=matrix(0,nrow=(noOfParameters))

Then I use:
constraints = list(ineqA=lhs,ineqB=rhs)
in the maxBFGS function.

However, the result is that no matter what parameters I enter (positive,
negative or 0), I get an error saying that the initial value is out of
bounds.

Can you please tell me what I am doing wrong?

Thanks in advance,

Rohit

[[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.


Re: [R] Questions on dividing lists and tapply

2011-03-16 Thread Rohit Pandey
Awesome,that worked!  Thanks.


On Wed, Mar 16, 2011 at 6:46 AM, Henrique Dallazuanna www...@gmail.comwrote:

 Try this:

 mapply('/', l1, l2, SIMPLIFY = FALSE)

 and

 tapply(1:5, lapply(indxLi, as.numeric), sum)


 On Tue, Mar 15, 2011 at 6:06 PM, Rohit Pandey rohitpandey...@gmail.com
 wrote:
 
  Hello R community,
 
  I have two questions about using R.
 
  The first is about dividing each element of a list with another similar
  sized list. So, if the first list has two elements and so does the
 second,
  then the result should also be a list with two elements.
 
  For example, the inputs are:
 
  list(matrix(1:6,ncol=2),matrix(1:6,ncol=2))-l1
  l2-list(1:3,2)
 
  I want to get a list, l3 with the first element being l1[[1]]/l2[[1]] and
  the second one, l1[[2]]/l2[[2]].
 
  I had asked a similar question before and the solution using mapply works
  well for a list and a vector, but for two lists, it will return an array
  (and not a list).
 
  My second question is about the tapply function.
 
  tapply(1:5,list(c(1,1,2,2,3),c(1,2,1,1,3)),sum)
  will give me:
 
123
  1  1   2  NA
  2  7  NA  NA
  3 NA  NA   5
  The index here is a list: list(c(1,1,2,2,3),c(1,2,1,1,3)).
 
  However, if I get the same index list through one of the other apply
  functions (like by) for example,
  indxLi-by(rbind(c(1,1,2,2,3),c(1,2,1,1,3)),1:2,function(x){return(x)})
  then the tapply no longer works with this list.
  tapply(1:5,indxLi,sum) gives me the error:
 
  Error in sort.list(y) : 'x' must be atomic for 'sort.list'
  Have you called 'sort' on a list?
 
  Is there a way to use the above list in the tapply function?
 
  Many thanks in advance,
 
  --
  Thanks,
  Rohit
  Mob: 91 9819926213
 
 [[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.htmlhttp://www.r-project.org/posting-guide.html
  and provide commented, minimal, self-contained, reproducible code.



 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O




-- 
Thanks,
Rohit
Mob: 91 9819926213

[[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.


[R] Questions on dividing lists and tapply

2011-03-15 Thread Rohit Pandey
Hello R community,

I have two questions about using R.

The first is about dividing each element of a list with another similar
sized list. So, if the first list has two elements and so does the second,
then the result should also be a list with two elements.

For example, the inputs are:

list(matrix(1:6,ncol=2),matrix(1:6,ncol=2))-l1
l2-list(1:3,2)

I want to get a list, l3 with the first element being l1[[1]]/l2[[1]] and
the second one, l1[[2]]/l2[[2]].

I had asked a similar question before and the solution using mapply works
well for a list and a vector, but for two lists, it will return an array
(and not a list).

My second question is about the tapply function.

tapply(1:5,list(c(1,1,2,2,3),c(1,2,1,1,3)),sum)
will give me:

   123
1  1   2  NA
2  7  NA  NA
3 NA  NA   5
The index here is a list: list(c(1,1,2,2,3),c(1,2,1,1,3)).

However, if I get the same index list through one of the other apply
functions (like by) for example,
indxLi-by(rbind(c(1,1,2,2,3),c(1,2,1,1,3)),1:2,function(x){return(x)})
then the tapply no longer works with this list.
tapply(1:5,indxLi,sum) gives me the error:

Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

Is there a way to use the above list in the tapply function?

Many thanks in advance,

-- 
Thanks,
Rohit
Mob: 91 9819926213

[[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.


[R] Mutiplying a data frame to a list

2011-02-21 Thread Rohit Pandey
Hi R community,

I have a question I'm sure is very simple for most of you.

I have a list, with each element being a matrix and the names of the
elements are numbers (like 1,3,...). I can extract the matrices and the
names individually. Now, I want to multiply each of the names to the
individual list matrices (after converting to numbers of course).

I could use a for loop, but the very reason I have this list is that I was
trying to avoid loops (using lapply). So, turning to one now could defeat
the whole purpose.

Thanks in advance,

-- 
Thanks,
Rohit
Mob: 91 9819926213

[[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.


Re: [R] [Rcpp-devel] Help with integrating R and c/c++

2011-02-19 Thread Rohit Pandey
 of Dirk Eddelbuettel [
 e...@debian.org]
 Sent: 06 February 2011 18:18
 To: Dirk Eddelbuettel
 Cc: Rohit Pandey; rcpp-devel
 Subject: Re: [Rcpp-devel] [R] Help with integrating R and c/c++

 [ Now resending to rcpp-devel as I had said below I would --Dirk ]

 On 6 February 2011 at 20:58, Rohit Pandey wrote:
 | Hi,
 |
 | I have been using R for close to two years now and have grown quite
 | comfortable with the language. I am presently trying to implement an
 | optimization routine in R (Newton Rhapson). I have some R functions that
 | calculate the gradient and hessian (pre requisite matrices) fairly
 | efficiently. Now, I have to call this function iteratively until some
 | convergance criterion is reached. I think the standard method of doing
 this
 | in most programming languages is a while loop. However, I know R can get
 | pretty slow when you use loops. In order to make this efficient, I want
 to
 | transfer this part of my code to a more efficient programming language
 like
 | c++ or c. However, I have been trying to learn this all day without any
 | luck. I found a package called Rcpp that makes this easier. However, it
 | seems some functional knowledge of writing R packages is a pre requisite.
 I

 What gave you that impression?

 Here is a counter-example, using the packages inline (for cxxfunction) and
 Rcpp:

  R library(inline)
  R src - 'std::cout  Hello C++_From_R World  std::endl;
 return(Rcpp::wrap(42));'
  R rohit - cxxfunction(signature(), src, plugin=Rcpp)
  R rohit()
  Hello C++_From_R World
  [1] 42
  R

 This compiled, linked and loaded a C++ routine built from the two-statement
 program submitted as character variable.

 The Rcpp documentation, including its eight vignettes, is full of other
 examples. Start with Rcpp-introduction and maybe the Rcpp-FAQ.

 | tried to follow the standard manual for doing this, but could not find a
 | simple example to get me started. I know I am supposed to make a cpp file
 | and put it some where before it can be called from R, but I'm confused as
 to
 | how this can be done.
 |
 | My requirement is to start with a parameter vector, update it according
 to
 | the gradient and hessian, check if the parameter satisfies some
 convergance
 | criterion and continue doing this until it does. Is there a way to
 | efficiently do this through an R function (replicate?). The problem is
 that
 | the number of iterations is not fixed. If there is no function in R, is
 | there a way I can quickly use Rcpp or some thing to have this last part
 of
 | my code in a C or C++ program which repeatedly calls my R functions for
 | updating the parameters?

 Give the example above a first try, and then read some more. The archives
 of
 the rcpp-devel (CC'ed; post there for follow-ups after subscribing) list
 are
 full of examples, and the CRAN page for Rcpp lists almost two dozen other
 packages using Rcpp giving you plenty of examples should you want to write
 a
 package using Rcpp. Several of these packages do optimization giving you
 examples of you to pass parameters etc pp.

 Hope this helps, Dirk

 |
 | --
 | Thanks in advance,
 | Rohit
 | Mob: 91 9819926213
 |
 |   [[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.htmlhttp://www.r-project.org/posting-guide.html
 | and provide commented, minimal, self-contained, reproducible code.

 --
 Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
 ___
 Rcpp-devel mailing list
 rcpp-de...@lists.r-forge.r-project.org
 https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel




-- 
Thanks,
Rohit
Mob: 91 9819926213

[[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.


Re: [R] [Rcpp-devel] Help with integrating R and c/c++

2011-02-19 Thread Rohit Pandey
Hi Douglas,

Sorry for leaving that information out earlier.

I am running windows XP. I'm not sure about the tools you mention. I thought
installing the packages in R was enough. Are these tools like a program you
install?

The results of running sessionInfo() are:

R version 2.12.1 (2010-12-16)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252LC_MONETARY=English_United States.1252
LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  tcltk methods
base



On Sat, Feb 19, 2011 at 9:21 PM, Douglas Bates ba...@stat.wisc.edu wrote:

 On Sat, Feb 19, 2011 at 12:56 AM, Rohit Pandey rohitpandey...@gmail.com
 wrote:
  Hi Christopher/ Dirk,

  Thank you very much for your replys. I think the idea of using inline as
 you
  suggest is the best way to start off with using c++ with R. I went
 through
  your examples and also plenty I found on the net. I have been trying to
 run
  some of these over the past few days, but have consistently been getting
  this error message as soon as I run the 'cfunction' or 'cxxfunction'. For
  example, in the syntax that Dirk sent,
  R library(inline) #Runs perfectly
   R src - 'std::cout  Hello C++_From_R World  std::endl;
  return(Rcpp::wrap(42));' #Runs perfectly
   R rohit - cxxfunction(signature(), src, plugin=Rcpp)
  Now, as soon as I run this line, R spills a whole lot of text out and
 gives
  an error and a warning:
  ERROR(s) during compilation: source code errors or compiler configuration
  errors!
  Program source:
   1:
   2: // includes from the plugin
   3:
   4: #include Rcpp.h
   5:
   6:
   7: #ifndef BEGIN_RCPP
   8: #define BEGIN_RCPP
   9: #endif
   10:
   11: #ifndef END_RCPP
   12: #define END_RCPP
   13: #endif
   14:
   15: using namespace Rcpp;
   16:
   17:
   18: // user includes
   19:
   20:
   21: // declarations
   22: extern C {
   23: SEXP file59046688( ) ;
   24: }
   25:
   26: // definition
   27:
   28: SEXP file59046688(  ){
   29: BEGIN_RCPP
   30: std::cout  Hello C++_From_R World  std::endl;
  return(Rcpp::wrap(42));
   31: END_RCPP
   32: }
   33:
   34:
  Error in compileCode(f, code, language = language, verbose = verbose) :
   Compilation ERROR, function(s)/method(s) not created!
  In addition: Warning message:
  running command 'C:\PROGRA~1\R\R-212~1.1/bin/i386/R CMD SHLIB
  file59046688.cpp 2 file59046688.cpp.err.txt' had status 1
 
  The file59046688.cpp 2  changes every time I run a different function,
 but
  the problem seems to be the same.
 
  I installed and loaded the inline package (0.3.8) and then the Rcpp
 package
  (0.9.0). I also tried reversing the order in which I load these, but
 still
  no luck. I think if I can get just one of these programs to work, I will
 be
  on my way. Can any of you tell me what I might be doing wrong?
 
  For your question on what exacly I require, Christopher - I just need to
 use
  a while loop. I have always been able to substitute for loops with some
 of
  the apply functions in R, but can't seem to be able to replace the while
  with a more efficient function. But the things that are required inside
 the
  while loop, I have already implemented in R efficiently. So, I thought of
  transfering just the while loop to a language that is faster with loops.

 What operating system and version of R are you using?  It would help
 if you included the results of executing

 sessionInfo()

 in R.  More importantly, do you have the compiler tools installed and
 configured?  You need to have certain tools installed on Windows or
 Mac OS X before you can compile packages (as opposed to installing
 pre-compiled binary packages).  Most Linux distributions assume that
 their users are adults and provide them with the tools to compile
 programs.

 
  Thanks in advance.
  On Mon, Feb 7, 2011 at 6:21 AM, Wray, Christopher 
  christopher.wray...@ucl.ac.uk wrote:
 
  As Dirk says, using inline makes it real simple to start and to
 prototype
  code.
 
  You mention you have R functions you wish to call via Rcpp. Im not
  certain I fully understand what you require here, but it is pretty
 simple to
  pass R-side functions to C++ via Rcpp, and similarly its simple to send
  compiled functions back to R-side as external pointers (and reuse them
  elsewhere). Here is a toy example using a simple user function defined
 on
  the R-side (user_F).
 
  This is evaluated in R, passed as a function parameter to compiled C++
  function and evaluated there, and then a compiled version of the
 function is
  passed back to R as an external pointer, which you can send back to the
 C
  side and evaluate:
 
  R user_F=function(v){sum((1-v)*exp(-0.5*v))}
 
  cpp - '
  NumericVector numvec(xvec);
  NumericVector RetVec;
  Function userR_f(fun);
  List ATR;
  typedef SEXP (*h_ptr)(SEXP);
  RetVec = userR_f(numvec);
  ATR[Fn_ptr]=XPtrh_ptr (new h_ptr(fme));
  ATR[Fn_VAL

[R] Help with integrating R and c/c++

2011-02-06 Thread Rohit Pandey
Hi,

I have been using R for close to two years now and have grown quite
comfortable with the language. I am presently trying to implement an
optimization routine in R (Newton Rhapson). I have some R functions that
calculate the gradient and hessian (pre requisite matrices) fairly
efficiently. Now, I have to call this function iteratively until some
convergance criterion is reached. I think the standard method of doing this
in most programming languages is a while loop. However, I know R can get
pretty slow when you use loops. In order to make this efficient, I want to
transfer this part of my code to a more efficient programming language like
c++ or c. However, I have been trying to learn this all day without any
luck. I found a package called Rcpp that makes this easier. However, it
seems some functional knowledge of writing R packages is a pre requisite. I
tried to follow the standard manual for doing this, but could not find a
simple example to get me started. I know I am supposed to make a cpp file
and put it some where before it can be called from R, but I'm confused as to
how this can be done.

My requirement is to start with a parameter vector, update it according to
the gradient and hessian, check if the parameter satisfies some convergance
criterion and continue doing this until it does. Is there a way to
efficiently do this through an R function (replicate?). The problem is that
the number of iterations is not fixed. If there is no function in R, is
there a way I can quickly use Rcpp or some thing to have this last part of
my code in a C or C++ program which repeatedly calls my R functions for
updating the parameters?

-- 
Thanks in advance,
Rohit
Mob: 91 9819926213

[[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.