[R] formatting a result table (number of digits)

2010-04-08 Thread Esmail

Hello,

Is there an easy way to format the output of a result table that R
generates from a regression?  I like the table, but would like to
limit the number of decimal points in the entries if possible.

For instance I would like only 3 digits of precision for the Value,
Std.Error. (And if it would be easy to get rid of scientific notation,
that would be good to know too). So ideally keep the table/headers etc,
but chance the entries.

I've looked at format and print without much success.
I'm using  R 2.10.1 in a Linux environment.

Thanks,
Esmail


Below's an example of generating the table I am trying to format:

 load('data.rda')
 library(nlme)
 ic - lme(Ind_Cntrbn ~ 
D_Clb_Pbl+D_Club+Round+Ind_cntr_1+GR_cntr_1+D_Success_1+IndEarngs_1+D_Unfair_cntr_1+D_1st10rnds+D_Female+D_econ_gov+D_mjr_social+D_frshmn+D_junior+D_senior+UrbanArea+ParentEducation+Empathy+Lcs_Cntrl_Intn+Trust, 
random=~1|Group, data=Dataset)

 s=summary(ic)
 t=s$tTable
 t
   Value  Std.Error   DF t-value  p-value
(Intercept)  5.683020304 0.78867419 2237  7.20578963 7.853134e-13
D_Clb_Pbl0.193187682 0.25494452   22  0.75776363 4.566332e-01
D_Club   0.112745107 0.09636280 2237  1.17000650 2.421230e-01
Round   -0.096400805 0.0181 2237 -5.31948107 1.144272e-07
Ind_cntr_1   0.340706093 0.04002901 2237  8.51147920 3.097981e-17
GR_cntr_10.033215293 0.01271829 2237  2.61161545 9.071799e-03
D_Success_1  2.210964800 0.38904872 2237  5.68300241 1.496038e-08
IndEarngs_1 -0.277758214 0.03903289 2237 -7.11600506 1.489839e-12
D_Unfair_cntr_1 -0.535979858 0.18280685 2237 -2.93194624 3.402644e-03
D_1st10rnds -0.479775417 0.19375663 2237 -2.47617547 1.335331e-02
D_Female 0.07269 0.12354518 2237  0.62055250 5.349573e-01
D_econ_gov  -0.282679230 0.12023002 2237 -2.35115350 1.880165e-02
D_mjr_social 0.149134543 0.12572505 2237  1.18619595 2.356709e-01
D_frshmn-0.149157208 0.20609463 2237 -0.72373166 4.693062e-01
D_junior-0.112917886 0.17455488 2237 -0.64689045 5.177692e-01
D_senior-0.107570461 0.15088326 2237 -0.71293835 4.759583e-01
UrbanArea   -0.364410559 0.11988827 2237 -3.03958489 2.396486e-03
ParentEducation  0.098237171 0.05350248 2237  1.83612365 6.647201e-02
Empathy  0.042232476 0.08248236 2237  0.51201825 6.086888e-01
Lcs_Cntrl_Intn   0.132131306 0.08841098 2237  1.49451240 1.351828e-01
Trust   -0.003872427 0.08468412 2237 -0.04572789 9.635312e-01

__
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] Comparing matrices

2010-03-11 Thread Esmail

Hello all,

I have two matrices, pop and pop2, each the same number of rows and
columns that I want to compare for equality. I am concerned about
efficiency in this operation.

I've tried a few things without success so far. Doing something simple like:

if (pop==pop2) { cat('equal') } else { cat('NOT equal') }

results in the warning:
1: In if (pop == pop2) { :
  the condition has length  1 and only the first element will be used

so it seems to look only at the first element.

print(pop==pop2) gives me a listing of TRUE/FALSE values for each
element.

I am really only looking for a single TRUE or FALSE value to tell me
if the two populations (ie pop and pop2) are the same or different. In
my application there will be about 200 columns and 50 rows and this
comparison will happen very frequently (possibly a few thousand
times), so I am concerned about efficiency.

I am appending the code I used to generate my matrices and some things
I tried (mentioned above) and also the output get so far.

FWIW, Linux environment, R 2.10.1.

Thanks,
Esmail

ps: Am I correct that if I do the assignment

pop2 = pop

I create a totally separate instance/(deep)copy of the
data? I tried a few tests that seem to confirm this, but I'd
rather be sure.


--- code 

# create a binary vector of size len
create_bin_Chromosome - function(len)
{
  sample(0:1, len, replace=T)
}

# create popsize members, each of length len
create_pop_2 - function(popsize, len)
{
  datasize=len*popsize

  npop - matrix(0, popsize, len, byrow=T)

  for(i in 1:popsize)
npop[i,] = create_bin_Chromosome(len)

  npop
}


POP_SIZE = 3
LEN = 8

pop = create_pop_2(POP_SIZE, LEN)
pop2 = pop

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

pop[2,5] = 99
pop2[3,3] = 77

print(pop==pop2)
if (pop==pop2) { cat('equal\n') } else { cat('NOT equal\n') }

print(pop)
print(pop2)

-- output ---

 source('popc.R')
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
equal
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01001011
 [,1] [,2]  [,3] [,4]  [,5] [,6] [,7] [,8]
[1,] TRUE TRUE  TRUE TRUE  TRUE TRUE TRUE TRUE
[2,] TRUE TRUE  TRUE TRUE FALSE TRUE TRUE TRUE
[3,] TRUE TRUE FALSE TRUE  TRUE TRUE TRUE TRUE
equal
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]0110   99000
[3,]01001011
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01010110
[2,]01100000
[3,]01   7701011
Warning messages:
1: In if (pop == pop2) { :
  the condition has length  1 and only the first element will be used
2: In if (pop == pop2) { :
  the condition has length  1 and only the first element will be used


__
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] sprintf + integer(0) problem

2010-02-24 Thread Esmail

Hello all,

I am stuck with R v2.8.0 under Linux for the time being and I am
running into a small problem that doesn't exist under 2.9.x and 2.10.x
with sprintf.

If I have the following code segment to help me determine the column
number for a specific column header/label:

 nn = names(Dataset)
 s = Group
 c = which(nn==s)

 cat(sprintf('found %s in col %d\n', s, c))


If the string s is found as a column header, sprintf works fine.

However if the string isn't found, c contains integer(0) which then
causes a program abort with the following message under 2.8.0

  Error in sprintf(found %s in col %d\n, s, c) : zero-length argument

Is there an easy work around? I tried using %s (in place of %d) hoping
it would just print integer(0) - which is better than a crash - but
that didn't work.

I am developing and testing my code under 2.9 and 2.10 but then
transferring it to a faster system that unfortunately still uses 2.8.0
.. I'd rather not have to keep modifying the source each time I upload it.

I'm hoping someone has an easy fix.

Thanks
Esmail

PS: in v 2.10.1 no output at all is generated when the string isn't found

__
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] sprintf + integer(0) problem

2010-02-24 Thread esmail


Henrique Dallazuanna wrote:
 
 You can try this:
 
 cat(sprintf(ifelse(any(grepl(s, nn)), 'found %s in col %d\n', 'Column
 %s not found'), s, match(s, nn)))
 
 

Wow .. one can always learn something new .. that looks pretty complex, I
have a bunch of print statements. I was hoping for a simpler solution so I
guess for now I'll just comment out the sections that might cause problems
under 2.8.0 (too bad, because it's useful to have some of this info
displayed - which it does during development/testing, so it's ok).

If anyone has a alternate solution, please let me know. In the meantime I'm
going to try to use this code to determine if I should display the info or
not:

min = version[[minor]]
maj = version[[major]]
ver = paste(maj, '.',min, sep='')

if (ver == 2.8.0) ...


Cheers,
Esmail
-- 
View this message in context: 
http://n4.nabble.com/sprintf-integer-0-problem-tp1567829p1568357.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.


Re: [R] sprintf + integer(0) problem

2010-02-24 Thread Esmail

On 24-Feb-10 20:03, Henrique Dallazuanna wrote:

You can use tryCatch also:

cat(tryCatch(sprintf('found %s in col %d\n', s, c), error =
function(x)cat('Not Found\n')))


Ah .. one more way .. thanks, I've saved it away in my set of
R tricks!

Best,
Esmail

__
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] lint for R? and debugging

2010-02-16 Thread Esmail

Good morning .. sorry if this is a basic question, but is there
a lint-like utility for R to check for suspicious language
constructs?

And along the same lines, any type of interactive debugging
utility for R?

My main use of R is under Linux (though I run code sometimes
under Window XP). R versions 2.9-2.10

Thanks,
Esmail

__
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] lint for R? and debugging

2010-02-16 Thread Esmail

On 16-Feb-10 09:03, Karl Ove Hufthammer wrote:

On Tue, 16 Feb 2010 08:00:09 -0500 Esmailesmail...@gmail.com  wrote:

And along the same lines, any type of interactive debugging
utility for R?


See this article in R News:

'Debugging Without (Too Many) Tears'
http://cran.r-project.org/doc/Rnews/Rnews_2003-3.pdf#page=29



Thanks for the pointer, that looks very interesting.

Any lint-like utilities out there? I miss a lot of the development
tools I have available for Python or Java with R, esp once the code
starts to grow beyond a few hundred lines.

Esmail

__
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] argh .. if/else .. why?

2010-02-15 Thread Esmail

Hello, would someone please help explain the following inconsistency
in the  if/else statement to me?

The format of the if/else #3 below is ok, but if/else #1 is not? (I get
an unexpected else type error.) In order for it to work I have to use
if/else #2

Thanks .. maybe there is some reason for this, but this looks very
inconsistent to me.

R version 2.10.1 (2009-12-14)
Ubuntu 9.04

Esmail


---

#if (ZELIG)   if/else #1
#  cat(sprintf(Zelig for %d runs - timeR\n, RUNS))
#else
#  cat(sprintf(lme for %d runs - timeR\n, RUNS))


if (ZELIG){  ### if/else #2
  cat(sprintf(Zelig for %d runs - timeR\n, RUNS))
} else
{
  cat(sprintf(lme for %d runs - timeR\n, RUNS))
}


cat(date(), start - timeR\n)

for(i in 1:RUNS)
{

  if (ZELIG)  # if/else #3
do_Zelig()
  else
do_lme()


  cat(i, ' - ', date(),\n)
}

cat(date(), end - timeR\n)
sink()

__
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] argh .. if/else .. why?

2010-02-15 Thread Esmail

On 15-Feb-10 09:13, jim holtman wrote:

 This is a common question.  The problem
is basically when input commands at the command line, when you hit the
carriage return it is assuming there is a complete command.
Within a function, or a block like the 'for' loop, the statement is
not complete until the closing bracket, so internally it will be
parsed correctly, but just get into the habit of making sure the
if/else are on the same line or bracket.


Ah .. so the fact that this is inside a block (in this case a function)
makes a difference .. I see .. well this is certainly different from other
languages I have worked with, but the explanation is clear.

Thanks you!

Esmail

__
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] argh .. if/else .. why?

2010-02-15 Thread Esmail

On 15-Feb-10 09:27, lee.ad...@luminant.com wrote:

I, personally, utilize the ifelse(test,statement,statement) function when 
possible over the methodology outlined.



Haven't used that construct before, I'll check it out.

Thanks. And mystery solved.

Esmail

__
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] a fast way to get AIC, BIC and logLik?

2010-02-15 Thread Esmail

Hello all,

Disclaimer, I know very little about statistics :-|

I am running a series of regressions for friends, the goal being to
determine the AIC, BIC and logLik values from the result/summary of
each regression. We are doing this as part of an evolutionary
algorithm, so it is important that the regression step be as quick as
possible.

We initially used the Zelig library, but this was running rather slow
(we may have up to 210 variables in our equation).

The call looked like this:

IndContr_1 - zelig(Y ~ var1 + var2 + .. + varN +tag(1|Group), data=Dataset, 
model=ls.mixed)


My friends then suggested using the nlme library instead, so our call
looks like this now:

IndContr_2 - lme(Y ~ var1 + var2 + .. varN, random=~1|Group, data=Dataset)

This runs much faster, in about 25-30% of the time of the zelig (at least my
preliminary timing seems to indicate this)

Is there a faster way yet to get the AIC/BIC/logLik values? (I hope
this question makes sense)

Thanks,
Esmail

__
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] lme4 and function 'cholmod_start' not provided by package 'Matrix' / Ubuntu

2010-01-10 Thread Esmail

Hello all,

Using Ubuntu 9.04 and R 2.8.1.

For a project I need to use the Zelig package, which in turn wants to
use the lme4 package. When trying to use Zelig and it tries to its required
packages I get the following error message.

Error in dyn.load(file, DLLpath = DLLpath, ...) :
  function 'cholmod_start' not provided by package 'Matrix'
Error in loadModelDeps(model) :
  This model depends on package lme4. Please install this package and try 
again

However, library() lists this package:

Packages in library '/usr/lib/R/site-library':

codaOutput analysis and diagnostics for MCMC
lme4Linear mixed-effects models using S4 classes
Matrix  Sparse and Dense Matrix Classes and Methods
MCMCpackMarkov chain Monte Carlo (MCMC) Package
mvtnorm Multivariate Normal and t Distributions
sandwichRobust Covariance Matrix Estimators
VGAMVector Generalized Linear and Additive Models
Zelig   Everyone's Statistical Software
zoo  ...

Trying to load lme4 directly with library(lme4) yields:


Error in dyn.load(file, DLLpath = DLLpath, ...) :
  function 'cholmod_start' not provided by package 'Matrix'
Error: package/namespace load failed for 'lme4'


I have looked on the web for quite a bit, but not found a solution,
what am I missing?

lme4 wants matrix version = 0.999375-11, I have Matrix_0.999375-17,
and R = 2.7 (I have 2.8.1). I notice that in the functions listings
for Matrix cholmod_start is not found.

(also for some odd reason, my system installs version Zelig 3.3-1 instead of 
3.4-5, don't think this is a factor though)



Here are some specifics about my setup, I tried to be complete but I
am happy to provide more information if it would help.

Thanks!

Esmail


Ubuntu 9.04
Linux t61 2.6.28-17-generic #58-Ubuntu SMP Tue Dec 1 18:57:07 UTC 2009 i686 
GNU/Linux



 library(lme4)
Loading required package: Matrix
Loading required package: lattice

Attaching package: 'Matrix'


The following object(s) are masked from package:stats :

 xtabs


The following object(s) are masked from package:base :

 colMeans,
 colSums,
 rcond,
 rowMeans,
 rowSums

Error in dyn.load(file, DLLpath = DLLpath, ...) :
  function 'cholmod_start' not provided by package 'Matrix'
Error: package/namespace load failed for 'lme4'


 sessionInfo()
R version 2.8.1 (2008-12-22)
i486-pc-linux-gnu

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

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

other attached packages:
[1] Matrix_0.999375-17 lattice_0.17-26

loaded via a namespace (and not attached):
[1] grid_2.8.1


Information on package 'lme4'

Description:

Package:   lme4
Version:   0.999375-28
Date:  2008-12-13
Title: Linear mixed-effects models using S4 classes
Author:Douglas Bates ba...@stat.wisc.edu, Martin Maechler
   maech...@r-project.org and Bin Dai d...@stat.wisc.edu
Maintainer:Douglas Bates ba...@stat.wisc.edu
Description:   Fit linear and generalized linear mixed-effects models.
Depends:   methods, R(= 2.7.0), Matrix(= 0.999375-11), lattice
LinkingTo: Matrix, stats
Imports:   graphics, stats
Suggests:  mlmRev, MEMSS
LazyLoad:  yes
LazyData:  yes
License:   GPL (=2)
URL:   http://lme4.r-forge.r-project.org/
Packaged:  Sat Dec 13 20:07:38 2008; maechler
Built: R 2.8.0; i486-pc-linux-gnu; 2008-12-15 13:16:45; unix




Information on package 'Matrix'

Description:

Package:  Matrix
Version:  0.999375-17
Date: 2008-12-10
Title:Sparse and Dense Matrix Classes and Methods
Author:   Douglas Bates ba...@stat.wisc.edu and Martin
  Maechler maech...@stat.math.ethz.ch
Maintainer:   Doug and Martin matrix-auth...@r-project.org
Description:  Classes and methods for dense and sparse matrices
  and operations on them using Lapack and
  SuiteSparse.
Depends:  R (= 2.6.0), stats, methods, utils, lattice
Imports:  graphics, lattice, grid, stats
Enhances: graph, SparseM
SystemRequirements:   GNU make
LazyLoad: yes
LazyData: no
LazyDataNote: no longer available, since we use data/*.R *and*
  our classes

all.equal-methods   Matrix Methods for Function all.equal()
atomicVector-class  Virtual Class atomicVector of Atomic Vectors
bandExtract bands of a matrix
bdiag   Construct a Block Diagonal Matrix
cBind   Versions

Re: [R] lme4 and function 'cholmod_start' not provided by package 'Matrix' / Ubuntu

2010-01-10 Thread Esmail

On 10-Jan-10 12:12, Dieter Menne wrote:




Esmail Bonakdarian-4 wrote:



Using Ubuntu 9.04 and R 2.8.1.

For a project I need to use the Zelig package, which in turn wants to
use the lme4 package. When trying to use Zelig and it tries to its
required
packages I get the following error message.

Error in dyn.load(file, DLLpath = DLLpath, ...) :
function 'cholmod_start' not provided by package 'Matrix'
Error in loadModelDeps(model) :
This model depends on package lme4. Please install this package and
try again




Looks like a version mismatch. It's probably easiest if you update to R
2.10.1 and try again; you will have to reinstall all your packages, because
of the change help format in 2.9.x

Dieter


Ok, I'll give this a try (probably not a bad idea to be more up-to-date.
Hopefully that will make the problem go away.

Thanks,
Esmail

__
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] lme4 and function 'cholmod_start' not provided by package 'Matrix' / Ubuntu

2010-01-10 Thread Esmail

On 10-Jan-10 12:40, Douglas Bates wrote:

As Dieter points out, this is likely a mismatch between the versions
of the lme4 and the Matrix packages, which are very closely linked
together.  It appears that the version of the lme4 package is too old
for the version of the Matrix package, which is hard to catch in the
dependencies (lme4 depends on Matrix but not the other way around).
As Dieter suggests, it would be best to install the most recent
version of R which can then pull in the most recent versions of the
lme4 and Matrix packages.


Hi,

Yes, you were both right on the money. I took your suggestion and
upgraded R to 2.10.1 and I had to install lme4 and zelig from source
to get the current packages.

Thanks again!

Esmail

__
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] Learning R

2009-11-30 Thread Esmail

Julia Cains wrote:

Dear R helpers,

Almost 15 days back I have become member of this very active and wonderful 
group. So far I have been only raising  queries and in turn got them solved too 
and I really thank for the spirit this group member show when it comes to the 
guidance.

I wish to learn R language and I have given 2 months time for this. Can anyone 
please guide me as how do I begin i.e. from basics to advance.

R is such a vast thing to learn, so I wish to learn it step by step without 
getting lost at any stage.

Please guide me where do I start and upgrade myself to higher level step by 
step.

Regards

Julia


I heard some time back that O'Reilly is coming out with a book on R .. not
sure if that's out yet, or how good it might be. Worth looking into ...

Esmail

ps: Just checked, 'R in a Nutshell':
http://oreilly.com/catalog/9780596801717
release date dec 2009/jan 2010

__
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] Google's R Style Guide

2009-08-30 Thread Esmail

hadley wickham wrote:

Perhaps most of you have already seen this?

 http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html

Comments/Critiques?


I made my own version that reflects my personal biases:
http://had.co.nz/stat405/resources/r-style-guide.html



thanks for sharing, I'll check it out.

Cheers,
Esmail

__
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] Google's R Style Guide

2009-08-29 Thread Esmail

Duncan Murdoch wrote:

On 8/28/2009 8:59 AM, Esmail wrote:

Perhaps most of you have already seen this?

   http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html

Comments/Critiques?


The rules are mostly reasonable, though they aren't the ones followed in 
the R source.  One bad rule is the one on curly braces:


An opening curly brace should never go on its own line; a closing curly 
brace should always go on its own line.


The problem is the second part.  If the closing brace is followed by an 
else clause, the else should go on the same line as the brace, or things 
will parse differently when pasted than they do in a function.  F


Excellent point, thanks, this has caused me some problems in the
past too.

Esmail

__
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] Google's R Style Guide

2009-08-29 Thread Esmail

(Ted Harding) wrote:

On 28-Aug-09 12:59:24, Esmail wrote:

Perhaps most of you have already seen this?
  
http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html


Comments/Critiques?



I think it is grossly over-prescriptive. For example:
  function names have initial capital letters and no dots
is violated throughout R itself.


Thanks Ted, the way I look at these is as recommendations that
I can choose to follow .. if something strikes me as silly I will
happily ignore it ;-)

Best,
Esmail

__
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] Google's R Style Guide

2009-08-29 Thread Esmail

Kingsford Jones wrote:

A few thoughts:




...



-- It's nice that people have made these guides available


Agreed .. it helps those relatively new to the language (and possible
other language biases) get their orientation.

Cheers,
Esmail

__
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] Google's R Style Guide

2009-08-29 Thread Esmail

Barry Rowlingson wrote:

On Fri, Aug 28, 2009 at 5:11 PM, hadley wickhamh.wick...@gmail.com wrote:


In my view, that's the purpose of indenting - you see scope from
indenting.


 *cough* python *cough*


:-)

(my favorite language at the moment)

__
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] Best R text editors?

2009-08-28 Thread Esmail

Jonathan Greenberg wrote:
Quick informal poll: what is everyone's favorite text editor for working 
with R? 


Emacs+ESS

__
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] Google's R Style Guide

2009-08-28 Thread Esmail

Perhaps most of you have already seen this?

  http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html

Comments/Critiques?

Thanks,
Esmail

ps: Reminds me of PEP 8 for Python

http://www.python.org/dev/peps/pep-0008/

Maybe not that surprising since Python is also one of the main languages
used by Google.

__
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] using optimize() correctly ...

2009-05-25 Thread Esmail

Berend Hasselman wrote:


If you do

 resopt - optim(-5,f, method=SANN,control=list(fnscale=-1))

you will get the global maximum. SANN: simulated annealing. But starting 
in -4 takes you to the local maximum.


So if I understand correctly, this method would also yield the same
sort of result (ie non-guaranteed global max or min over the range
-10 to 10).

But the help for optim recommends optimize for one-dimensional 
maximization.


Yes, I recall reading this.

As far as I know there is no general foolproof method for finding a 
global optimum except trying different initial points.

No method can really replace one's own knowledge about a function.


Agreed, however, I am testing a genetic algorithm's ability to find the
global maximum/minimum and was looking for another independent tool to
verify the results I am getting for f(x) and f(x, y), ie my various
test functions which will be more complex than the one I posted (and
many will be multi-modal since those present a challenge for any
algorithm). Since R has proven to be quite capable in the past I
thought I would check and see if it could easily do this. I was hoping
to avoid having to plot and re-run various functions repeatedly over
various ranges or starting points.

Thanks again for the help/information, much appreciated.

Esmail

__
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] using optimize() correctly ...

2009-05-25 Thread Esmail

Hi Ravi!

Ravi Varadhan wrote:

 Yes.  Most classical optimization methods (e.g. gradient-type,
 Newton-type) are local, i.e. they do not attempt to locate the
 global optimum.

Ah .. I see.

 The primary difficulty with global optimization is that there are no
 mathematical conditions that characterize global optimum in
 multi-modal problems.
..
  A simplistic strategy to find global optimum is to use local
 methods with multiple starting values.

:-) .. well, that is somewhat similar to the approach the genetic
algorithm uses, well, at least with respect of having many starting
points.

 Again the problem is that you don't have any guarantee that you have
 found the global optimum.

Well, then it looks like I am out of luck if I wanted to plug in a
function and provide end ranges and get the global max/min. I was
hoping this would provide a way for me to verify the workings of
a genetic algorithm I am testing.

I appreciate you taking the time to explain this so clearly, thanks
again,

Esmail

__
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] using optimize() correctly ...

2009-05-24 Thread Esmail

Hello Berend,

Berend Hasselman wrote:



Your function is not unimodal.
The help for optimize states: 


If f is not unimodal, then optimize() may approximate a local, but perhaps
non-global, minimum to the same accuracy.


Ah ok, I didn't read the manual page carefully enough.

Do you know if R has a function to find the global maximum/minimum of a
function of x over a given interval?

nlminb(), optim(), in particular the option `method = L-BFGS-B' or the 
function spg() in BB package were recommended to use if I wanted to

optimize a function over x and y given their respective intervals. Will
they also potentially only give me the local maxima/minima? I am not a
regular R user, so my knowledge is clearly not where is could/should be.

Thanks,
Esmail

__
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] optimizing function over x,y

2009-05-23 Thread Esmail

Hello all,

I would like to maximize or minimize a given math function over a
specific set of values.

I was checking out Wolfram Alpha (http://www70.wolframalpha.com/)
and it can do simple optimization problems in math, such as

maximize 15*x - x**2 over 0 to 15

(http://www70.wolframalpha.com/input/?i=maximize+15*x+-+x**2+over+0+to+15)

which finds the maximum value and input for x in the range 0 to 15.
Very cool. (This is of course a very simple example).

Reading the R documentation I found out that with R I can do this:


 f-function(x) (15*x)-x**2
 optimize(f, c(0,15), lower=0,upper=15, maximum=TRUE)

which works very nicely too!


What I would like to do, but what Wolfram Alpha apparently can't do
(according to a post in their community forum) is to maximize (or
minimize) functions that contain two variables, x and y, or more. So
for example a very simple example would be

   minimize x**2 + y**2 over x:0 to 15, y:0-12  -- this does not work
-- though I'm unclear about
-- the correct syntax too.

Is there a way to do this in R in just a neat and easy way as R
optimizes a 1-D function?

For this particular instance I am interested in the minimum of

  x * sin(4*x) + 1.1 * sin(2*y), where x,y in range 0-10

so an example of this in R would be great, though in other problems
the range may not be identical for x and y.

Thanks,

Esmail

ps: Has anyone written any programs in Python that they then have used
to call R functions? I am thinking of writing some of my scripts in
Python, but then accessing some of R's functions.

__
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] using optimize() correctly ...

2009-05-23 Thread Esmail

Hi,

I am trying to use the optimize function to optimize a function. The
results I am getting don't agree with what I compute on my own and
when I look at the graph of

f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 = x = 10

in gnuplot.

I suspect I am making a mistake in the usage of the R optimize
function, perhaps someone could point out where?


 f-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10)

to MAXIMIZE:

 result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE)
 result
$maximum
[1] -2.728743

$objective
[1] 247.3284

to MINIMIZE

 result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE)
 result
$minimum
[1] 6.290112

$objective
[1] 91.52681


However, I believe the correct values should be

minimize:
x = -5.838   val= -133.020

maximize:
x = -8.957   val= 438.448

Thanks,
Esmail


This is w/ R version 2.8.1 (2008-12-22) under Ubuntu 9.04

__
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] optimizing function over x,y

2009-05-23 Thread Esmail

Ravi Varadhan wrote:

Look at nlminb() or optim(), in particular the option `method = L-BFGS-B' or the 
function spg() in BB package.

With these you can optimize over any number of variables.

Ravi. 


Hi Ravi,

Thanks for the leads, I'll take a look, though right now I have
run into problems with even the simple (?) optimize function, so
I'll first have to figure out what I'm doing wrong with it.

Best,
Esmail

__
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] running R on netbooks/minis?

2009-05-03 Thread Esmail

Erin Hodgess wrote:

Dear R People:

Is it possible to run R on a netbook/mini, please?


I have run this in my MSI Wind w/ 2GB RAM under XP and also
Linux (in VirtualBox) w/o problems - though of course it's
much slower than on a 'real' desktop or laptop :-) The Atom
processor's forte is not speed, but frugal power consumption.

HTH,
Esmail

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-26 Thread Esmail

Hello David,

Let me try again, I don't think this was the best post ever I've made :-)
Hopefully this is clearer, or otherwise I may break this up into
three separate simple queries as this may be too long.


 == is not an assignment operator in R, so the answer is that it
 would do neither.  - and = can do assignment. In neither case
 would it be a deep copy.

It was late when I posted the code, I made a mistake with regard to
the assignment operator and used the boolean compare instead -- thanks
for catching that.

It should have been:

keep_pop[1:POP_SIZE] = pop[1:POP_SIZE]


 Here's an edited and clearer version I hope:


The basic idea is that I am trying to keep track of a number of bitrings.

Therefore I am creating a matrix (named 'pop') whose rows are made up
of bit vectors (ie my bitstrings).  I only initialize half of the rows
with my bitstrings of random 1s and 0s, the rest of the rows are set
to all zeros).

So I use following function call to create a matrix and fill it with
bit strings:

   pop=create_pop_2(POP_SIZE, LEN)

where

   POP_SIZE refers to the number of rows
   LEN to the columns (length of my bitstrings)



This is the code I call:


# create a random binary vector of size len
#
create_bin_Chromosome - function(len)
{
  sample(0:1, len, replace=T)
}



## create_population ###
# create population of chromosomes of length len
# the matrix contains twice as much space as popsize
#
create_pop_2 - function(popsize, len)
{
  datasize=len*popsize
  print(datasize)
  npop - matrix(0, popsize*2, len, byrow=T)

  for(i in 1:popsize)
npop[i,] = create_bin_Chromosome(len)

  npop
}


My 3 questions:

(1) If I did

keep_pop[1:POP_SIZE] = pop[1:POP_SIZE]

to keep a copy of the original data structure before manipulating
'pop' potentially, would this make a deep copy or just shallow? Ie
if I change something in pop would keep_pop change too? I would
like two independent copies so that 'keep_pop' stays intact while
'pop' may change.

 - and = can do assignment. In neither case would it be a
 deep copy.

Is there a deepcopy operator, or would I have to have two nested
loops and iterate through them? Or is there a nice R-idiomatic way
to do this?


(2) If I wanted to change the order of rows in my matrix 'pop', is
there an easy way to shuffle these?  I.e., I don't want to change
any of the bitstrings vectors/rows, just the order of the rows in the
matrix 'pop'. (E.g., in Python I could just say something like
suffle(pop)) - is there an equivalent for R?

So if pop [ [0, 0, 0]
[1, 1, 1]
[1, 1, 0] ]

after the shuffle it may look like

  [ [1, 1, 0](originally at index 2)
[1, 1, 1](originally at index 1)
[0, 0, 0] ]  (originally at index 0)

the rows themselves remained intact, just their order changes.
This is a tiny example, in my case I may have 100 rows (POPS_SIZE)
and rows of LEN 200.


(3) I would like to compare the contents of 'keep_pop' (a copy of the
original 'pop') with the current 'pop'. Though the order of rows
may be different between the two, it should not matter as long as
the same rows are present.  So for the example given above, the
comparison should return True.

For instance, in Python this would be simply

if sorted(keep_pop) == sorted(pop):
   print 'they are equal'
else
   print 'they are not equal'

Is there an equivalent R code segment?


I hope this post is clearer than my original one. Thank you David for
pointing out some of the shortcomings of my earlier post.

Thanks,

Esmail

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-26 Thread Esmail

David Winsemius wrote:


Yes. As I said before I am going to refrain from posting speculation 
until you provide valid R code

that will create an object that can be the subject of operations.



The code I have provided works, here is a run that may prove helpful:

POP_SIZE = 6
LEN = 8

pop=create_pop_2(POP_SIZE, LEN)

print(pop)
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
 [1,]01011001
 [2,]00000000
 [3,]11001000
 [4,]00000001
 [5,]00110010
 [6,]10000010
 [7,]00000000
 [8,]00000000
 [9,]00000000
[10,]00000000
[11,]00000000
[12,]00000000

I want to (1) create a deep copy of pop, (2) be able to shuffle
the rows only, and (3) be able to compare two copies of these objects
for equality and have it return True if only the rows have been shuffled.

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-26 Thread Esmail

David,

Good news! It seems that R has deep copy by default. I ran this simplified
test and it seems I can change 'pop' without changing the saved version.

POP_SIZE = 4
LEN = 8
pop=create_pop_2(POP_SIZE, LEN)
cat('printing original pop\n')
print(pop)

keep_pop = pop
pop[1,1] = 99

cat('printing changed pop\n')
print(pop)
cat('printing keep_pop\n')
print(keep_pop)



---

 source('mat.R')
[1] 32
printing original pop
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01101001
[2,]10100011
[3,]01011101
[4,]00010100
[5,]00000000
[6,]00000000
[7,]00000000
[8,]00000000


printing changed pop
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]   991101001
[2,]10100011
[3,]01011101
[4,]00010100
[5,]00000000
[6,]00000000
[7,]00000000
[8,]00000000


printing keep_pop
 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]01101001
[2,]10100011
[3,]01011101
[4,]00010100
[5,]00000000
[6,]00000000
[7,]00000000
[8,]00000000


Re Shuffle

I tried using sample based on your earlier post, but your example
really helped, thanks!  That solves the shuffling issue.

dx - sample(1:POP_SIZE, POP_SIZE)
cat('shuffled index:')
print(dx)
print(pop[dx,])

cat('shuffled pop')
pop[1:POP_SIZE,] = pop[dx,]
print(pop)


re compare:

 I am unclear why you would not create an original and a copy,

Well .. that I wanted to do from the start (hence my question about
deep copy :-)

 work on the copy, and compare with the original that is also sorted
 by the external index.

That's a great idea, hadn't thought of keeping the index around for
this, I'll give this a try.

Final question, how do I compare these two structures so that I get
one result, true or false? Right now

keep == pop yields all these individual comparisons:

 pop==keep

  [,1] [,2]  [,3] [,4]  [,5]
[1,] FALSE TRUE FALSE TRUE FALSE
[2,] FALSE TRUE FALSE TRUE FALSE
[3,]  TRUE TRUE  TRUE TRUE  TRUE
[4,]  TRUE TRUE  TRUE TRUE  TRUE
[5,]  TRUE TRUE  TRUE TRUE  TRUE
[6,]  TRUE TRUE  TRUE TRUE  TRUE

Thanks for the help, much appreciated.

Esmail

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-26 Thread Esmail

hadley wickham wrote:

I want to (1) create a deep copy of pop,

I have already said *I* do not know how to create a deep copy in R.


Creating a deep copy is easy, because all copies are deep copies.
You need to try very hard to create a reference in R.


Hi Hadley

Right you are .. I discovered this now too. It's really confusing to
go back and forth between different languages. I have been programming
in Python for the last 2 months and everything there is a reference .. so
I have to worry about deep copy etc.

Thanks!
Esmail

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-26 Thread Esmail

 David Winsemius

dwinsem...@comcast.net wrote:

My understanding of the OP's request was for some sort of copy which did
change when entries in the original were changed; the sort of behavior that
might be seen  in a spreadsheet that had a copy by reference.


You misunderstood (my phrasing wasn't probably the best), but I was
clear about wanting two independent copies.

From my earlier post:

(1) If I did

keep_pop[1:POP_SIZE] = pop[1:POP_SIZE]

to keep a copy of the original data structure before manipulating
'pop' potentially, would this make a deep copy or just shallow? Ie
if I change something in 'pop' would it be reflected in 'keep_pop'
too? (I don't think so, but just wanted to check). I would like
two independent copies.

Regardless, the net outcome was new knowledge, so this is a good outcome.

Esmail

__
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] comparing matrices

2009-04-26 Thread Esmail

I'm trying to compare two matrices made up of bits.

doing a simple comparison of

matA == matB

yields this sort of output.

  [,1] [,2]  [,3]  [,4]  [,5]  [,6]
[1,] FALSE TRUE FALSE  TRUE  TRUE FALSE
[2,]  TRUE TRUE  TRUE  TRUE  TRUE  TRUE
[3,] FALSE TRUE FALSE FALSE FALSE  TRUE
[4,] FALSE TRUE  TRUE FALSE FALSE FALSE
[5,]  TRUE TRUE  TRUE  TRUE FALSE FALSE
[6,]  TRUE TRUE  TRUE  TRUE FALSE FALSE

I really would like just one comprehensive value to say TRUE or FALSE.

This is the hack (rather ugly I think) I put together that works,
but there has to be a nicer way, no?

res=pop[1:ROWS,] == keep[1:ROWS,]

if ((ROWS*COL) == sum(res))
 {
   cat('they are equal\n')
 }else
   cat('they are NOT equal\n')

Thanks!

Esmail

__
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] comparing matrices

2009-04-26 Thread Esmail

baptiste auguie wrote:

I'm not sure I'm following you but have you tried,

identical(matrix(c(1,1,1,1),ncol=2), matrix(c(1,1,1,1),ncol=2))

?all.equal
?isTRUE
?identical

and possibly the compare package,

compare(matrix(c(1,1,1,1),ncol=2),matrix(c(1,1,1,1),ncol=2))


HTH,

baptiste


Hi Babtiste,

Thanks for pointing out the various options that exist. R is
a very rich language indeed and it's good to know how to accomplish
tasks in various ways.

Cheers,
Esmail

__
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] comparing matrices

2009-04-26 Thread Esmail

ONKELINX, Thierry wrote:

Have a look at all.equal

matA - matrix(1:4, ncol = 2)
matB - matA
all.equal(matA, matB)
matB[1,1] - -10
all.equal(matA, matB)


Hi Thierry,

Thanks, all.equal does indicate if it's all equal so that
works great!

Much nicer than my hack - thanks,

Esmail

__
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] 3 questions regarding matrix copy/shuffle/compares

2009-04-25 Thread Esmail

Hello all,

I have the following function call to create a matrix of POP_SIZE rows
and fill it with bit strings of size LEN:

   pop=create_pop_2(POP_SIZE, LEN)

I have 3 questions:

(1) If I did

keep_pop[1:POP_SIZE] == pop[1:POP_SIZE]

to keep a copy of the original data structure before manipulating
'pop' potentially, would this make a deep copy or just shallow? Ie
if I change something in 'pop' would it be reflected in 'keep_pop'
too? (I don't think so, but just wanted to check). I would like
two independent copies.

(2) If I wanted to change the order of *rows* in my matrix 'pop', is there
an easy way to shuffle these? I don't want to change anything in the
columns, just the complete rowsn (E.g., in Python I could just say
something like suffle(pop) assuming pop is a list of list) - is there
an equivalent for R?

(3) I would like to compare the contents of 'keep_pop' with 'pop'. Though
the order of rows may be different it should not matter as long as
the same rows are present. Again, in Python this would be simply

if sorted(keep_pop) == sorted(pop):
   print 'they are equal'
else
   print 'they are not equal'

Is there an equivalent R code segment?

Thanks,

Esmail

--- the code called above -



# create a binary vector of size len
#
create_bin_Chromosome - function(len)
{
  sample(0:1, len, replace=T)
}



## create_population ###
# create population of chromosomes of length len
# the matrix contains twice as much space as popsize
#
create_pop_2 - function(popsize, len)
{
  datasize=len*popsize
  print(datasize)
  npop - matrix(0, popsize*2, len, byrow=T)

  for(i in 1:popsize)
npop[i,] = create_bin_Chromosome(len)

  npop
}

__
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] flip certain bits in vector

2009-04-19 Thread Esmail

I have a string of binary values, and I would like to flip certain
bits in a set of positions.

Let's say the

vector p contains position [1, 3, 5, 7]
vector b contains bits   [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
result r should be   [0, 1, 0, 0, 0, 0, 0, 0, 1, 0]

in pseudo code this would be something like

---

r = c()

for (i in 1:10)
  if (i in p)
 r = c(r, flip[i])

r


doesn't work :-)  R doesn't like the if statement.

Is there a nice, concise way to do this? The vector contents and
size will vary, but length of p = b.

Thanks,
Esmail

__
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] flip certain bits in vector

2009-04-19 Thread Esmail

David Winsemius wrote:
I do not think your wetware processed the inputs correctly. The second 
bit should not have been flipped:


Ooops .. yes you are right!



Try this loop free index based solution:

 b - c( 1, 0, 1, 0, 1, 0, 1, 0, 1, 0)
 r - b
 r[p] - 0 + !r[p]  # adding 0 converts logical TRUE/FALSE  to 0/1
 r
 [1] 0 0 0 0 0 0 0 0 1 0


Great! .. thanks ... I appreciate the hlep,


Esmail

__
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] flip certain bits in vector

2009-04-19 Thread Esmail

jim holtman wrote:

try this:


b - c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0)
p - c(1, 3, 5, 7)
b[p] - ifelse(b[p] == 0, 1, 0)


I'll have to look up the ifelse operator, looks like
the ternary operator used in C

b[p] = b[p]==0?1:0

Cool - thanks!

Esmail

__
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] difference between assignment syntax - vs =

2009-02-21 Thread Esmail Bonakdarian

Patrick Burns wrote:

'The R Inferno' page 78 is one source you can
look at.


Patrick Burns


wow .. nice! .. thanks for posting this reference.

Esmail

__
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] Sorting rows in a matrix based on vector of indecies

2009-02-20 Thread Esmail Bonakdarian

Hello

I have a matrix of size rows x cols.
I also have a vector  of size rows.

The vector contains index values that corresponds to rows in the matrix.

I would like to re-arrange/sort the contents of the matrix according
to the entries in the vector. Can this be done efficiently in R and
possibly in place? Or would it be better to allocate another whole matrix?

Simple example:

matrix:
ff
aa
zz
bb


vector:
2, 4, 1, 3

new matrix:
aa
bb
ff
zz


I suppose I could allocate another matrix and loop through the vector
of indecies placing entries into the new matrix. Do I have to worry
about deallocating the memory space of the first matrix or will it
automagically go away if I assign the new matrix to the old matrix
identifier (assuming nothing else is pointing at it)?

Thanks,
Esmail

__
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] Sorting rows in a matrix based on vector of indecies

2009-02-20 Thread Esmail Bonakdarian

Hi David,

This was useful, thanks.

The example was just that, there are no as, b,s etc I was
just trying to show what I was trying to do.


Using m[v,]

where m was the matrix in my example and v the vector of
index values works great as you suggested Thanks.

R is quite a powerful language as I am starting to discover
(and folks on the list here are very helpful).

Regards,

Esmail

David Winsemius wrote:


See if this helps:

  mtx-matrix( rep(letters[1:4], 6), nrow=4)
  mtx
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] a  a  a  a  a  a
[2,] b  b  b  b  b  b
[3,] c  c  c  c  c  c
[4,] d  d  d  d  d  d

  mtx[c(2, 4, 1, 3), ]

 [,1] [,2] [,3] [,4] [,5] [,6]
[1,] b  b  b  b  b  b
[2,] d  d  d  d  d  d
[3,] a  a  a  a  a  a
[4,] c  c  c  c  c  c

You can do with that result whatever you want:

assign it,   m2 - mtx[c(2, 4, 1, 3), ]


__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Doran, Harold wrote:

lm(y ~ x-1)
solve(crossprod(x), t(x))%*%y# probably this can be done more
efficiently



You could do

crossprod(x,y) instead of t(x))%*%y



that certainly looks more readable (and less error prone) to an R newbie
like myself :-)

__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Hi Kenn,

Thanks for the suggestions, I'll have to see if I can figure out how to
convert the relatively simple call to lm with an equation and the data file
to the functions you mention (or if that's even feasible).

Not an expert in statistics myself, I am mostly concentrating on the
programming aspects of R. Problem is that I suspect my colleagues who
are providing some guidance with the stats end are not quite experts
themselves, and certainly new to R.

Cheers,

Esmail

Kenn Konstabel wrote:
lm does lots of computations, some of which you may never need. If speed 
really matters, you might want to compute only those things you will 
really use. If you only need coefficients, then using %*%, solve and 
crossprod will be remarkably faster than lm


# repeating someone else's example
# lm(DAX~., EuStockMarkets)

 y - EuStockMarkets[,DAX]
 x - EuStockMarkets
 x[,1]-1
colnames(x)[1] - Intercept

lm(y ~ x-1)
solve(crossprod(x), t(x))%*%y# probably this can be done more 
efficiently


# and a naive timing

  system.time( for(i in 1:1000) lm(y ~ x-1))
   user  system elapsed
  14.640.33   32.69
  system.time(for(i in 1:1000) solve(crossprod(x), crossprod(x,y)) )
   user  system elapsed
   0.360.000.36


Also lsfit() is a bit quicker than lm or lm.fit.

Regards,
Kenn


__
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] Python and R

2009-02-19 Thread Esmail Bonakdarian

Gabor Grothendieck wrote:

On Wed, Feb 18, 2009 at 7:27 AM, Esmail Bonakdarian esmail...@gmail.com wrote:

Gabor Grothendieck wrote:


See ?Rprof for profiling your R code.

If lm is the culprit, rewriting your lm calls using lm.fit might help.

Yes, based on my informal benchmarking, lm is the main bottleneck, the
rest
of the code consists mostly of vector manipulations and control structures.

I am not familiar with lm.fit, I'll definitely look it up. I hope it's
similar
enough to make it easy to substitute one for the other.

Thanks for the suggestion, much appreciated. (My runs now take sometimes
several hours, it would be great to cut that time down by any amount :-)



Yes, the speedup can be significant.  e.g. here we cut the time down to
40% of the lm time by using lm.fit and we can get down to nearly 10% if
we go even lower level:


Wow those numbers look impressive, that would be a nice speedup to have.

I took a look at the manual and found the following at the top of
the description for lm.fit:

  These are the basic computing engines called by lm used to fit linear
   models. These should usually not be used directly unless by experienced
   users. 

I am certainly not an experienced user - so I wonder how different it
would be to use lm.fit instead of lm.

Right now I cobble together an equation and then call lm with it and the
datafile.

I.e.,

LM.1 = lm(as.formula(eqn), data=datafile)
s=summary(LM.1)

I then extract some information from the summary stats.

I'm not really quite sure what to make of the parameter list in lm.fit

I will look on-line and see if I can find an example showing the use of
this - thanks for pointing me in that direction.

Esmail


system.time(replicate(1000, lm(DAX ~.-1, EuStockMarkets)))

   user  system elapsed
  26.850.07   27.35

system.time(replicate(1000, lm.fit(EuStockMarkets[,-1], EuStockMarkets[,1])))

   user  system elapsed
  10.760.00   10.78

system.time(replicate(1000, qr.coef(qr(EuStockMarkets[,-1]), 
EuStockMarkets[,1])))

   user  system elapsed
   3.330.003.34

lm(DAX ~.-1, EuStockMarkets)


Call:
lm(formula = DAX ~ . - 1, data = EuStockMarkets)

Coefficients:
 SMI   CAC  FTSE
 0.55156   0.45062  -0.09392


# They call give the same coefficients:



lm.fit(EuStockMarkets[,-1], EuStockMarkets[,1])$coef

SMI CACFTSE
 0.55156141  0.45062183 -0.09391815

qr.coef(qr(EuStockMarkets[,-1]), EuStockMarkets[,1])

SMI CACFTSE
 0.55156141  0.45062183 -0.09391815



__
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] Python and R

2009-02-18 Thread Esmail Bonakdarian

Gabor Grothendieck wrote:



See ?Rprof for profiling your R code.

If lm is the culprit, rewriting your lm calls using lm.fit might help.


Yes, based on my informal benchmarking, lm is the main bottleneck, the rest
of the code consists mostly of vector manipulations and control structures.

I am not familiar with lm.fit, I'll definitely look it up. I hope it's similar
enough to make it easy to substitute one for the other.

Thanks for the suggestion, much appreciated. (My runs now take sometimes
several hours, it would be great to cut that time down by any amount :-)

Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian

Hello all,

I am just wondering if any of you are doing most of your scripting
with Python instead of R's programming language and then calling
the relevant R functions as needed?

And if so, what is your experience with this and what sort of
software/library  do you use in combination with Python to be able
to access R's functionality.

Is there much of a performance hit either way? (as both are interpreted
languages)

Thanks,
Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian
Hello!

On Tue, Feb 17, 2009 at 5:58 PM, Warren Young war...@etr-usa.com wrote:

 Esmail Bonakdarian wrote:

 I am just wondering if any of you are doing most of your scripting
 with Python instead of R's programming language and then calling
 the relevant R functions as needed?

 No, but if I wanted to do such a thing, I'd look at Sage: http://sagemath.org/

ah .. thanks for the pointer, I had not heard of Sage, I was just
starting to look at
SciPy.

 It'll give you access to a lot more than just R.

 Is there much of a performance hit either way? (as both are interpreted
 languages)

 Are you just asking, or do you have a particular execution time goal, which 
 if exceeded
 would prevent doing this?  I ask because I suspect it's the former, and fast 
 enough is fast
 enough.

I put together a large'ish R program last year, but I think I would be
happier if I could code
it in say Python - but I would rather not do that at the expense of
execution time.

Thanks again for telling me about Sage.

Esmail

__
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] Python and R

2009-02-17 Thread Esmail Bonakdarian
On Tue, Feb 17, 2009 at 6:05 PM, Barry Rowlingson
b.rowling...@lancaster.ac.uk wrote:
 2009/2/17 Esmail Bonakdarian esmail...@gmail.com:

  When I need to use the two together, it's easiest with 'rpy'. This
 lets you call R functions from python, so you can do:

  from rpy import r
  r.hist(z)

wow .. that is pretty straight forward, I'll have to check out rpy for sure.

 to get a histogram of the values in a python list 'z'. There are some
 complications converting structured data types between the two but
 they can be overcome, and apparently are handled better with the next
 generation Rpy2 (which I've not got into yet). Google for rpy for
 info.

Will do!

 Is there much of a performance hit either way? (as both are interpreted
 languages)

  Not sure what you mean here. Do you mean is:

  R sum(x)

 faster than

 Python sum(x)

 and how much worse is:

 Python from rpy import r
 Python r.sum(x)


Well, I have a program written in R which already takes quite a while
to run. I was
just wondering if I were to rewrite most of the logic in Python - the
main thing I use
in R are its regression facilities - if it would speed things up. I
suspect not since
both of them are interpreted, and the bulk of the time is taken up by
R's regression
calls.

Esmail

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian

Hello all,

I need some help with a nice R-idiomatic and efficient solution to a
small problem.

Essentially, I am trying to eliminate randomly half of the entries in
a vector that contains index values into some other vectors.


More details:

I am working with two strings/vectors of 0s and 1s. These will contain
about 200 elements (always the same number for both)

I want to:

1. determines the locations of where the two strings differ

   -- easy using xor(s1, s2)

2. *randomly* selects *half* of those positions

   -- not sure how to do this. I suppose the result would be
   a list of index positions of size sum(xor(s1, s2))/2


3. exchange (flip) the bits in those random positions for both strings

   -- I have something that seems to do that, but it doesn't look
   slick and I wonder how efficient it is.

Mostly I need help for #2, but will happily accept suggestions for #3,
or for that matter anything that looks odd.

Below my partial solution .. the HUX function is what I am trying
to finish if someone can point me in the right direction.

Thanks
Esmail
--



rm(list=ls())


# create a binary vector of size len
#
create_bin_Chromosome - function(len)
{
  sample(0:1, len, replace=T)
}




# HUX - half uniform crossover
#
# 1. determines the locations of where the two strings
#differ (easy xor)
#
# 2. randomly selects half of those positions
#
# 3. exchanges (flips) the bits in those positions for
#both
#
HUX - function(b1, b2)
{
  # 1. find differing bits
  r=xor(b1, b2)

  # positions where bits differ
  different = which(r==TRUE)

  cat(\nhrp: , different, \n)
  # 2. ??? how to do this best so that each time
  #a different half subset is selected? I.e.,
  #sum(r)/2 positions.


  # 3. this flips *all* positions, should really only flip
  #half of them (randomly selected half)
  new_b1 = b1
  new_b2 = b2

  for(i in different)  # should contain half the entries (randomly)
  {
new_b1[i] = b2[i]
new_b2[i] = b1[i]
  }

  result - matrix(c(new_b1, new_b2), 2, LEN, byrow=T)
  result
}



LEN = 5
b1=create_bin_Chromosome(LEN)
b2=create_bin_Chromosome(LEN)

cat(b1, \n)
cat(b2, \n)

idx=HUX(b1, b2)
cat(\n\n)
cat(idx[1,], \n)
cat(idx[2,], \n)

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian
(sorry if this is a duplicate-problems with posting at my end)

Hello all,

I need some help with a nice R-idiomatic and efficient solution to a
small problem.

Essentially, I am trying to eliminate randomly half of the entries in
a vector that contains index values into some other vectors.

More details:

I am working with two strings/vectors of 0s and 1s. These will contain
about 200 elements (always the same number for both)

I want to:

1. determines the locations of where the two strings differ

-- easy using xor(s1, s2)

2. *randomly* selects *half* of those positions

-- not sure how to do this. I suppose the result would be
a list of index positions of size sum(xor(s1, s2))/2

3. exchange (flip) the bits in those random positions for both strings

-- I have something that seems to do that, but it doesn't look
slick and I wonder how efficient it is.

Mostly I need help for #2, but will happily accept suggestions for #3,
or for that matter anything that looks odd.

Below my partial solution .. the HUX function is what I am trying
to finish if someone can point me in the right direction.

Thanks
Esmail
--

rm(list=ls())


# create a binary vector of size len
#
create_bin_Chromosome - function(len)
{
   sample(0:1, len, replace=T)
}


# HUX - half uniform crossover
#
# 1. determines the locations of where the two strings
#differ (easy xor)
#
# 2. randomly selects half of those positions
#
# 3. exchanges (flips) the bits in those positions for
#both
#
HUX - function(b1, b2)
{
   # 1. find differing bits
   r=xor(b1, b2)

   # positions where bits differ
   different = which(r==TRUE)

   cat(\nhrp: , different, \n)
   # 2. ??? how to do this best so that each time
   #a different half subset is selected? I.e.,
   #sum(r)/2 positions.

   # 3. this flips *all* positions, should really only flip
   #half of them (randomly selected half)
   new_b1 = b1
   new_b2 = b2

   for(i in different)  # should contain half the entries (randomly)
   {
 new_b1[i] = b2[i]
 new_b2[i] = b1[i]
   }

   result - matrix(c(new_b1, new_b2), 2, LEN, byrow=T)
   result
}

LEN = 5
b1=create_bin_Chromosome(LEN)
b2=create_bin_Chromosome(LEN)

cat(b1, \n)
cat(b2, \n)

idx=HUX(b1, b2)
cat(\n\n)
cat(idx[1,], \n)
cat(idx[2,], \n)

__
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] how to randomly eliminate half the entries in a vector?

2009-02-17 Thread Esmail Bonakdarian

Gene Leynes wrote:

This is my first help post, hope it works!

Just check out the sample function
At the command line type:
?sample

I think it will be pretty clear from the documentation.


Yes, most excellent suggestion and quite helpful!

Thanks,
Esmail

__
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] running R-code outside of R

2008-06-25 Thread Esmail Bonakdarian

Jim Porzak wrote:
The user of your R script sees only the outputs you create. The R source 
is hidden.


Ah .. that sounds great .. I wish I had known about this a month ago!
I'll have to check it out - thanks!

Esmail



HTH,
Jim Porzak

..

   Would the R script that is being run be hidden from the user, or
   would the
   user be able to view/download/save the R source code - or could it
   be hidden
   so they just run the code, but never see it?

   Esmail


__
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] running R-code outside of R

2008-06-25 Thread Esmail Bonakdarian

Spencer Graves wrote:
 If you want to hide the fact that you are using R -- especially if 
you charge people for your software that uses R clandestinely -- that's 
a violation of the license (GPL).  


No on both accounts .. but thanks for pointing this out none the less.

__
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] Programming Concepts and Philosophy

2008-06-20 Thread Esmail Bonakdarian

hadley wickham wrote:

2008/6/20 [EMAIL PROTECTED] [EMAIL PROTECTED]:

If you do nothing to your code, in 18 months time its performance will
have doubled because computers will have become faster.  Your code
will not get easier to understand by itself.


Very nicely put .. and true too!

__
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] replace column headers

2008-06-17 Thread Esmail Bonakdarian

Paul Adams wrote:



Hello everyone,=I have a question as to how to remove the column headers
in a data file and then replace those with titles from another file in this case
the file labeled ann  (
in which the titles are all in one column).


Maybe this will help partially.

I am not sure on how to read your new column names from a file into a vector
but if you can get them into that format this should let you accomplish your
goal.

Esmail



 load(Data/simpleTestData2.rda)

 df
   Y X1   X2   X3 X4 X5 X6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 colnames(df)
[1] Y  X1 X2 X3 X4 X5 X6

 altnames=c(A, B1, B2, B3, B4, B5, B6)

 colnames(df)=altnames

 df
   A B1   B2   B3 B4 B5 B6
1   74.9 10 49.8  0.2 99 50 57
2   79.8 11 49.6  0.4 69 91 57
3   84.4 12 48.8  1.2 30 38 58
4   88.8 13 47.6  2.4 77 87 58

 save(df, file=example.rda)

# new file will contain the new column headers.

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Hi,

I have matrix of bits and a target vector. Is there an
efficient way to search the rows of the matrix for the target?
I am interested in the first row index where target is found.

Example:

 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
 [1,]10110
 [2,]11010
 [3,]00100
 [4,]10011
 [5,]10111
 [6,]11001
 [7,]10011
 [8,]00111
 [9,]01101
[10,]00010

target:  1 1 0 1 1

Should return -1 (or some other indicator) since the
target was not found in any of the rows.



 source(lookup.R)
  [,1] [,2] [,3] [,4] [,5]
 [1,]00110
 [2,]10000
 [3,]10000
 [4,]11000
 [5,]11100
 [6,]00110
 [7,]01110
 [8,]00110
 [9,]11011
[10,]10100

target:  1 1 0 1 1

Should return 9 since the target was  found in row 9


If the target is found, it is no longer necessary to keep
searching the rest of the matrix (which may be quite large)

The data/size etc may change of course, but target will
always have the same number of columns as the matrix.

I tried variations of which, and a for loop
comparing pop[i,] to target without much success, nor
did google yield any results. I am hoping someone here
can provide a suggestion.

Thanks,

EB


-

# Here is the code that generates the above data

create_bin_string - function(len)
{
  sample(0:1, len, replace=T)
}

ROWS = 10
COLS =  5
pop = matrix(create_bin_string(ROWS*COLS), ROWS, COLS, byrow=T)



target=c(1, 1, 0, 1, 1)

# my population
print(pop)

# I am looking for the index of this in pop
# if present (else -1?)
cat(\ntarget: , target, \n)



##
## this is NOT working
## plus it would continue to search
## after it found the target
##
for(i in ROWS)
   if (pop[i,] == target)
  cat(\nfound in row: , i, \n\n)

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Henrique Dallazuanna wrote:

Try this:

which(apply(t(m) == target, 2, all))


Wow! .. talk about concise! Neat! Thanks.

This will return all matches correct? So if I only wanted
the first I'd simply subscript [1] into it.

Do you think the fact that it searches the whole matrix instead
of stopping when it finds a match may slow it down?

This is my own solution I came up with in the meantime,
looks rather pedestrian compared to your one line, but it will
drop out immediately once if finds the target. Yours looks (based
simply on appearance :-) faster. Having no feel for the language
I may just have to time them.


I would assume that your solution would be faster simple since it's
using built-in language constructs which are optimized (and implemented
in C?) instead of my own interpreted way.


# return index of target in pop, else -1
searchPop - function(pop, target)
{
rows = length(pop[1,])
for(i in 1:rows)
{
result = (pop[i,] == target)
if (sum(which(result==FALSE)) == 0)
return(i)
}

return (-1)
}


idx=searchPop(pop, target)

if (idx  0)
{
cat(NOT found\n)
} else
cat(Found at position , idx, \n)



Esmail

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


__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian

Dimitris Rizopoulos wrote:

try this:

match.pat - function (mat, target, nomatch = -1) {
   f1 - do.call(paste, c(as.data.frame(mat), sep = \r))
   f2 - paste(target, collapse = \r)
   ind - f1 %in% f2
   if (any(ind)) which(ind)[1] else nomatch
}


Thanks! More R for me to sink my teeth in :-)  My own solution
doesn't seem to work quite correctly as I found out from some
further testing .. so the solutions posted here are much appreciated!

Esmail

__
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] searching for specific row in matrix

2008-06-11 Thread Esmail Bonakdarian



# determine which data matches
matches - t(pop) == target  # 't' due to matching in column order

# colSums equal to COLS will indicate matches
which(colSums(matches) == COLS)




Neat! .. somewhat similar to the solution I came up with in the
meantime, only yours works :-)

Thanks Jim.

Esmail

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

Hello, I have the following task I'd like to accomplish:

A file contains 5 columns of data (several hundred rows), let's call
them a, b, c, d and e (ie these are their column headers)

I also have a set of definitions, e.g.,

f = a + b
g = a * 3
h = c + d
etc.

I would like to write out a new .rda file that contains columns

a b c d e f g h etc.

I.e. , the original data plus new columns (with headers and data).

It seems that there ought to be a simple way to do this, could
someone provide some guidance on the best way to accomplish
this task? (I tried a few things as this seemed rather trivial,
but did not succeed. I still hope/assume this is a trivial thing
to do if one knows R well)

Thanks,

Esmail

ps: I want to thank everyone again who posted their solutions
to my previous query. Seeing different solutions for the same
problem is a tremendously effective way to learn something
new.

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

Hi Erik,

Erik Iverson wrote:

Esmail -

Are these 5 vectors of data stored in a data.frame?  I assume so.


Yes, I do a simple load() call first to read the .rda file ...


test2 - transform(test, d = 2*a + b, e = 3*c)
save(test2, file = test2.Rdata)

Does this help?


Yes it does .. this is just what I needed. I'll probably do it
in this way:

df=transform(df, X5=X1+X2)
df=transform(df, X6=X1-X2)
etc ...


building it up the df line by line since the definitions have been
provided one per line (instead of queuing them up in one line like
you showed). I'll have to write a  little awk or ruby script to take
the original definitions and wrap them into the transform call, but then
I should be all set to go.

Thanks again!

Esmail

__
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] Adding new columns to (output) data - e.g., read 5 cols write 8

2008-06-11 Thread Esmail Bonakdarian

jim holtman wrote:

yourDF - cbind(yourDF, f=yourDF$a+yourDF$b, g=yourDF$a * 3,
h=yourDF$c + yourDF$d)


Thanks Jim, I also learned about the transform() method from Erik
which will also work beautifully.

Esmail

__
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] R + Linux

2008-06-06 Thread Esmail Bonakdarian

steven wilson wrote:


I'm planning to install Linux on my computer to run R (I'm bored of
W..XP). However, I haven't used Linux before and I would appreciate,
if possible, suggestions/comments about what could be the best option
install, 


Hi,

I have used Linux since the early 1990s starting with the original
slackware distribution, followed by various versions of Red Hat,
Gentoo (compiled from source), Fedora and now Ubuntu.

Ubuntu is my choice for having the least troublesome install and
maintenance. It has a very nice package manager, and if your goal
is to *use* a Linux system rather than tinker with it, you could
do much worse than Ubuntu.

I installed R via the package manger a month ago or so, very easy
and trouble free.

Hope that helps,

Esmail

__
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] R + Linux

2008-06-06 Thread Esmail Bonakdarian

FWIW, those who are curious about Linux but are not willing
or ready to abandon the Windows platform can now very easily
try out Ubuntu without having to repartition their hard drive.

Wubi is a project that installs Ubuntu under Windows so that it
can be uninstalled easily and requires no messing around with
hard drive partitions.

From the Wubi web site:

Wubi is an officially supported Ubuntu installer for Windows users that can 
bring you to the Linux world with a single click. Wubi allows you to install 
and uninstall Ubuntu as any other Windows application, in a simple and safe 
way. Are you curious about Linux and Ubuntu? Trying them out has never been 
easier!



For more information see:

  http://wubi-installer.org/

Esmail

__
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] Improving data processing efficiency

2008-06-06 Thread Esmail Bonakdarian

hadley wickham wrote:




Hi,

I tried this suggestion as I am curious about bottlenecks in my own
R code ...


Why not try profiling?  The profr package provides an alternative
display that I find more helpful than the default tools:

install.packages(profr)


 install.packages(profr)
Warning message:
package ‘profr’ is not available


any ideas?

Thanks,
Esmail



library(profr)
p - profr(fcn_create_nonissuing_match_by_quarterssinceissue(...))
plot(p)

That should at least help you see where the slow bits are.

Hadley



__
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] Improving data processing efficiency

2008-06-06 Thread Esmail Bonakdarian

Esmail Bonakdarian wrote:

hadley wickham wrote:




Hi,

I tried this suggestion as I am curious about bottlenecks in my own
R code ...


Why not try profiling?  The profr package provides an alternative
display that I find more helpful than the default tools:

install.packages(profr)


  install.packages(profr)
Warning message:
package ‘profr’ is not available


I selected a different mirror in place of the Iowa one and it
worked. Odd, I just assumed all the same packages are available
on all mirrors.

__
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] Thank you

2008-05-29 Thread Esmail Bonakdarian

Tubin wrote:

In the past few weeks I have had to give myself a crash course in R, in order
to accomplish some necessary tasks for my job.  During that time, I've found
this forum to be helpful time and time again - usually I find the answer to
my problem by searching the archives; once or twice I've posted questions
and been given near-immediate solutions.

I just wanted to thank the forum participants for all of their contributions
over the years!  



Hear hear! .. I've benefited greatly from reading the postings here
and some of the members have been very generous with their knowledge too!

Esmail

__
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] How do you exit a function in R?

2008-05-29 Thread Esmail Bonakdarian

Bill Cunliffe wrote:

For example, based on a certain condition, I may want to exit my code early:

 


# Are there the same number of assets in prices and
positions?

if (nAssetPositions != nAssetPrices) {

cat(Different number of assets! \n\n)

exit function



I've wondered about that myself

Have you tried  return() ? It would return NULL I think, as long as that
doesn't cause a problem it should exit the method.

__
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] hash or other quick lookup function?

2008-05-28 Thread Esmail Bonakdarian

Hi Duncan,

Duncan Murdoch wrote:

Duncan Murdoch wrote:

Esmail Bonakdarian wrote:
 

Hello all,

I have a matrix of bit values.

I compute certain values based on the bits in each row. There may be
*duplicate* entries in the matrix, ie several rows may be identical.
These rows change over time, and identical entries may not be next to
each other.

Computing these values is time consuming, so I would like a way to 
store a value once it's computed. That way I would be able to look up the value
based on a given bitstring (row in the matrix) and avoid having to 
re-compute the same value.


So, my thought is a hash function based on a bit string - does R have such
a thing? Or is there an alternative approach that would do the same in R?
The lookup should be quick, otherwise the gains from avoiding
recomputing identical values would be lost to some extent.


Environments can be hashed. To use this, you'd convert the bit string 
into a character string, and use that as the name of a variable in an 
environment.


For example,

cache - new.env(hash=TRUE, parent=emptyenv())
 ...
bits - '0101'
if (exists(bits, cache)) {
  return(get(bits, cache))
} else {
  value - long.computation()
  assign(bits, value)
  


Whoops, that assignment should have been in the cache:

   assign(bits, value, envir=cache)

  return(value)
}



Thank you for posting this, I'll have to give it a try.
If I can get this to work it should save some some time
spent on recomputing values.

(Sorry for the delayed reply, I was traveling for the
whole last day - suffering from some reverse jet lag at
the moment :-)

Can anyone else think of other alternatives? Always happy
to learn something new.

Thanks!

Esmail

ps: do you think the conversion to a character string
before comparison/search will cause a noticeable
performance hit? I still lack an intuitive feel for
R the programming language. I'd have more of an idea
with regular high-level programming languages.



Duncan Murdoch


__
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] How to make R running faster

2008-05-28 Thread Esmail Bonakdarian

Neil Shephard wrote:


Loops are not massively efficient within R.

Look into using the apply() family of functions
(eapply()/lapply()/mapply/rapply()/tapply()).


Didn't someone post not too long ago that apply is
internally represented as a for-loop? Or am I not
remembering this correctly?

The reason for apply was that it was more concise but not
necessarily more efficient.

Esmail

__
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] recompute values repeatedly, or new file for glm()?

2008-05-20 Thread Esmail Bonakdarian

Hello all,

I need to tap into the collective wisdom of the group re an issue of
efficiency.

A sketch of the situation:

Let's say 4000 observations in variables Y, X1, X2 , X3 and X4.

I would like to feed various combinations of this expression

Y ~ X1+X2+X3+X4 + I(X1^2)+I(X2^2)+I(X3^2)+I(X4^2) + X1*X2 + X1*X3 + X1*X4 + 
X2*X3 + X2*X4 + X3*X4


repeatedly to glm(). (I really have little knowledge about how R or
glm() works internally)

Let's say I call glm() 200 times with various combinations does it
make sense to compute these various factors based on X1 .. X4 and
store them in a file along with the original data, and then use
that file for the glm() calls or will the overhead of computing
these factors be so small that it's not worth computing these
values ahead of time and storing them in a file?

This is simplified example, I actually have 20 original variables
rather than the 4 I show above. I hope this made some sense.

Thanks,
Esmail

ps: If it makes sense to preprocess X1,X2,X3 and X4 to generate a new
file that contains the values for

X1, X2, X3, X4, I(X1^2), I(X2^2), I(X3^2), I(X4^2), X1*X2, X1*X3, X1*X4 
,X2*X3, X2*X4, X3*X4


is there an easy way to take the expression at the top of the message
and convert the values in the original dataframe and compute them so that
I can write them out to a new file?

__
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] Printing output in STDOUT

2008-05-20 Thread Esmail Bonakdarian

Edward Wijaya wrote:

Hi,

Currently the R script I have is executed with this command:

$ R CMD BATCH mycode.R

And the output is stored in mycode.Rout.

Is there a way I can issue command from shell (like above)
so that the output is printed to STDOUT?

It's  troublesome to open the Rout file every time to debug.


Under a Unix system you could try to pipe the command into tail -f

i.e.,

$ R CMD BATCH mycode.R | tail -f

That should display the file as it gets written.

I don't have access to a Unix system right now to give this a try
but it should be a work around until someone who knows more about
R can come up with an answer.

HTH

Esmail

__
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] Printing output in STDOUT

2008-05-20 Thread Esmail Bonakdarian

Prof Brian Ripley wrote:

On Tue, 20 May 2008, Esmail Bonakdarian wrote:


Edward Wijaya wrote:

Hi,

Currently the R script I have is executed with this command:

$ R CMD BATCH mycode.R

And the output is stored in mycode.Rout.

Is there a way I can issue command from shell (like above)
so that the output is printed to STDOUT?

It's  troublesome to open the Rout file every time to debug.


Under a Unix system you could try to pipe the command into tail -f

i.e.,

$ R CMD BATCH mycode.R | tail -f

That should display the file as it gets written.


Buffering may get in the way -- so 'gets written' may be much later than 
when it is output by R.


Yes, that's quite likely to be the case .. I like your suggestions
below much better - saved away for future reference too.

Esmail

__
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] Format integer

2008-05-13 Thread Esmail Bonakdarian

Anh Tran wrote:

Hi,
What's one way to convert an integer to a string with preceding 0's?
such that
'13' becomes '013'
to be put into a string

I've tried formatC, but they removes all the zeros and replace it with
blanks


Hi,

try sprintf:

i=13
 cat(sprintf(%05d\n, i))
00013


HTH,

Esmail

__
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] test

2008-05-13 Thread Esmail Bonakdarian

Tony Plate wrote:
You probably should check this section in your R-help subscription 
options (via https://stat.ethz.ch/mailman/options/r-help/, I think):



Receive your own posts to the list?




Tony,

Like jt I too have it set to receive my own messages, but I too
don't see them. I wonder if it has to do with the fact that both
of us use gmail to post to the list?

In any case, regardless if we see them, they are getting posted,
which is what matters :)

Cheers,
Esmail

__
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] test

2008-05-13 Thread Esmail Bonakdarian

Charilaos Skiadas wrote:

On May 13, 2008, at 5:52 AM, Esmail Bonakdarian wrote:


Tony Plate wrote:
You probably should check this section in your R-help subscription 
options (via https://stat.ethz.ch/mailman/options/r-help/, I think):

Receive your own posts to the list?




Tony,

Like jt I too have it set to receive my own messages, but I too
don't see them. I wonder if it has to do with the fact that both
of us use gmail to post to the list?


Bingo! Well, I don't know if this happens with the web interface to 
gmail, but if you use POP to access your gmail account, then any emails 
you send to any kind of list will not get back to you 


Yup, I use Thunderbird to read/process my gmail :-)

(Problem being, 
sort of, that gmail groups things in Conversations, and in this case 
will remember the email you sent out and use that copy in the 
conversation, instead of the one sent through the list. 


Except that part of the conversation sits in my sent folder rather
than being nicely threaded into the conversation.

Not a very good 
excuse in my opinion, and a particularly irritating feature.).


agreed!

In the rare occasions  where my waiting powers get exhausted before 
someone on this very helpful list replies to my email, I just use the 
online archives to check whether my email was sent or not.


Yes, I did that initially when I wasn't sure if my message was getting
out. Often thought I don't have to do this because the helpful folks on
this list have already replied to my posting.

Best,
Esmail

__
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] Random number generation

2008-05-13 Thread Esmail Bonakdarian

Greg Snow wrote:

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Esmail Bonakdarian
Sent: Sunday, May 11, 2008 7:25 AM
To: Prof Brian Ripley
Cc: [EMAIL PROTECTED]
Subject: Re: [R] Random number generation


[snip]


What I read doesn't seem to be incorrect however (it may even
have been an archived message here), the *language* itself
does not seem to support block *comments*. Using conditional
constructs, or an IDE/editor to achieve similar results is a
work around - but not the same. I don't mean to nitpick, but
as a computer scientist I see this as different :-)


I am not a computer scientist, so correct me if I am wrong, but from what I remember (and a quick glance at my copy of Kernighan Ritchie), the C *language* itself does not support block *comments*, rather the preproccessor 


Hi there,

Yes, while that may be true, many other languages that don't use an
explicit pre-processor mechanism (though internally will go
through a similar process) do support block comments. It's a
moot point anyway, since when you compile a C program the preprocessor
also runs (just like the linker etc) unless you specifically ask for
something differently via command line switches. As far as the
programmer is concerned the semantics are those of a language
that provides block comments with the simple use of /* */ , and
not dependent on if-statements or editor/IDE tricks.

Since R is optimized for interactive use rather than compilation, running everything through a preproccessor is not really an option.  However as an additional work around 


My use then may be not typical since I am trying to write scripts in R
and only use R directly to try out functions/language constructs.

you could always run your R scripts through the C preproccessor and 

 have it strip the block comments for you.

Too much work, call me old school, but I like the computer do work for me,
rather than the other way around :-)


Given the complexity of implementing block commenting (even deciding on the 
syntax) and the ease of current work arounds, the cost benefit ratio probably 
puts this very near the bottom of the priority list.


I couldn't possibly offer an opinion on that .. I'll happily defer to you
and the other experts here for this.

Cheers,
Esmail

__
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] [OT] xemacs on windows vista

2008-05-12 Thread Esmail Bonakdarian

Wensui Liu wrote:

Hi, dear all,
I just switch to vista (ultimate) and have heard there is some problem
for the installation of xemacs on vista. Is there any insight or
experience that you could share? I really appreciate any input.
thank you so much!


Hi,

I don't know about XEmacs, but I am using a specially bundled
version of GNU Emacs by Vincet Goulet which is great. I was
using a plain-vanilla version of GNU Emacs before, but this one
has ESS  and spell check support built in :-)

I am using it under XP .. I believe this also works under Vista

Check out:

   http://vgoulet.act.ulaval.ca/en/ressources/emacs/

HTH,

Esmail

__
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] Random number generation

2008-05-11 Thread Esmail Bonakdarian

Stephan Kolassa wrote:


Have you tried successively removing/commenting parts of the script 
before the sample() command until the problem goes away? That way you 
should be able to pinpoint the offending script command.


Hi,

This brings up a question I have .. is there a way to do *block* comments
with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment
more than just one line at a time.

From what I have read this is not possible in R (at least not easily), but
I am eager for someone to contradict me :-)

Thanks,
Esmail

__
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] Random number generation

2008-05-11 Thread Esmail Bonakdarian

Hello there,

Prof Brian Ripley wrote:

On Sun, 11 May 2008, Esmail Bonakdarian wrote:


Stephan Kolassa wrote:


Have you tried successively removing/commenting parts of the script 
before the sample() command until the problem goes away? That way you 
should be able to pinpoint the offending script command.


Hi,

This brings up a question I have .. is there a way to do *block* comments
with scripts? A la /* ... */ like it's done in Java or C/C++? Ie comment
more than just one line at a time.

From what I have read this is not possible in R (at least not easily), 
but

I am eager for someone to contradict me :-)


if(FALSE) {
...
}

Any good editor can do block commenting, e.g. ESS.

You didn't tell us what you read, but I have never seen this in a 
reputable source.


I don't remember the source as I was reading widely all over the place
trying to get up to speed with R in a hurry  (having found this group
was one of the best sources).

What I read doesn't seem to be incorrect however (it may even have been an
archived message here), the *language* itself does not seem to support block
*comments*. Using conditional constructs, or an IDE/editor to achieve similar
results is a work around - but not the same. I don't mean to nitpick, but
as a computer scientist I see this as different :-)

I'll have to look at ESS though.

Thanks,
Esmail

ps: Ah, I seem Meta-; works as a toggle in emacs/ESS .. thanks for encouraging
me to look at that some more.

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Hello,

which.max() only returns one index value, the one for the
maximum value. If I want the two index values for the two
largest values, is this a decent solution, or is there a
nicer/better R'ish way?

max2 -function(v)
{
m=which.max(v)
v[m] = -v[m]
m2=which.max(v)
result=c(m, m2)
result
}

Seems to work ok.

Thanks,
Esmail

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Dimitris Rizopoulos wrote:

try this:

v - rnorm(10)
v
order(v, decreasing = TRUE)[1:2]


Wow .. that is slick! First I thought, wait .. I don't want to
reorder the elements, but this doesn't - it just returns the index
values in order. I don't really get that from reading the documentation,
it's probably there, but not that clear to me.

Thanks for showing me something more R'ish.

Esmail

__
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] which.max2()

2008-05-09 Thread Esmail Bonakdarian

Marc Schwartz wrote:


I might be tempted to take a more generic approach, where one can
provide an argument to the function to indicate that I want the 'top x'
maximum values and to give the user the option of returning the indices 
or the values themselves.


Perhaps:

which.max2 - function(x, top = 1, values = FALSE)
{
  if (values)
rev(sort(x))[1:top]
  else
order(x, decreasing = TRUE)[1:top]
}


Very cool too! .. Thanks Marc. Again, I did not get this from the
order documentation (ie that it manipulate index values rather than
the values themselves). Great to see examples.

Best,
Esmail

__
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] building a formula string bit by bit ..

2008-05-06 Thread Esmail Bonakdarian

Hello,

Still a newbie with R, though I have learned a lot from reading
this list. I'm hoping someone can help with this question:

I have two vectors, one for variables, and one for bits.

I want to build a string (really a formula) based on the values in my
vector of 1s and 0s in bits. If I have a one, I want to include the
corresponding entry in the vars vector, otherwise ignore it. Of course
the bits vector will not stay the same.

So for example:

  vars=c('X.1', 'X.2', 'X.3', 'X.4', 'X.5')

  bits=c(1, 0, 1, 1, 0)
  ones=which(bits==1)

should yield:

  X.1 + X.3 + X.4

where as

  bits=c(1, 1, 0, 0, 0)

would yield

  X.1 + X.2

the which operator gives me the index values, is there an easy and
*efficient* way to build this string so that I can pass it on to glm?
I can probably hack some ugly code together to do this, but it won't
be efficient, and it won't be elegant :-)

Can anyone help?

Thanks!

__
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] building a formula string bit by bit ..

2008-05-06 Thread Esmail Bonakdarian

Jorge Ivan Velez wrote:

Hi Esmail,

Try this:

vars=c('X.1', 'X.2', 'X.3', 'X.4', 'X.5')
bits=c(1, 0, 1, 1, 0)
paste(vars[which(bits==1)],collapse=+)

HTH,

Jorge


Wow .. that is beautiful :-) .. and exactly what I was looking
for (and suspected existed).

I ended up doing this:

   eqn=(paste(vars[which(bits==1)],collapse= + ))
   eqn=paste(c(Y.data ~), eqn)
   GLM.9 - glm(as.formula(eqn) , family=gaussian(identity), data=simpleData)

For some reason I couldn't collapse the two eqn/paste statements into one
statement but this seems to work.

Thank again Jorge and everyone else .. this group is a big help.

Esmail

__
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] Automatically generating new column names (and columns)

2008-05-06 Thread Esmail Bonakdarian

Once again I need to tap into the collective knowledge here.

Let's say I have the following columns and data below

Y X1 X2 X3 X4

I would like to generate additional new columns and column names
(ie the data would be squared - and I'd like the column names to
reflect this) like:

Y X1 X2 X3 X4  X1^2 X2^2 X3^2 X4^2


I believe I can compute the values correctly with the code below, but
I am unable to generate the column names. (This is a short example, in
reality I have many more columns and more complicated names)

for(i in 2:5)
{
   name=get(paste(names(simpleData)[i],^2, sep=))
   simpleData- transform(simpleData, name=(simpleData[,i]^2))
}

print(simpleData)

Any suggestions/hints .. I've searched the FAQ and web (this is how I
came across the get() function) but no luck so far.

Thanks,
Esmail

__
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] efficiency profiling? (was: Why R is 200 times slower than Matlab ?)

2008-04-30 Thread esmail bonakdarian

This has been an interesting discussion, and brings up two questions
for me:

Is there a good collection of hints/suggestions for R language idoms in terms
of efficiency? For instance I read not to use for-loops, so I used apply only to
later read that apply is internally implemented as a for so nothing gained
here. Warnings about pitfalls (such as nested loops), hints, suggestions would
be great.

The second question - is there some sort of profiling tool available that would
make it easy to recognize where the script is spending most of its time? Might
be especially useful for newbies like me.

Thanks all,

Esmail

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

_
Spell a grand slam in this game where word skill meets World Series. Get in the 
game.

08
[[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] efficiency profiling?

2008-04-30 Thread esmail bonakdarian


 See ?Rprof for the tool.  For the tips, I think you just need to hang 
 around here a while.  I don't know of a nice collection (but I'm sure 
 there are several.)
 
 Duncan Murdoch


Hi,

thanks .. several folks pointed me to Rprof, I'll take a look.

Yes, I have been reading the list, the amount of messages per day
is simply amazing, I can hardly keep up. Do most of you read this
on the web or get it as digest? I am getting them as individual
e-mails (thank god for filters) ... :-)

Esmail

_
Back to work after baby–how do you know when you’re ready?

5797498ocid=T067MSN40A0701A
[[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] Documentation General Comments

2008-04-21 Thread esmail bonakdarian





 I realize the R developers are probably overwhelmed and have little time
 for this, but the documentation really needs some serious reorganizaton.
 A good through description of basic variable types would help a lot,
 e.g. the difference between lists, arrays, matrices and frames. 

Agreed, esp since I am right now at this moment struggling with the
differences of lists, arrays and vectors. (I have two vectors that I am
trying to return from a function as an array of two rows - for some reason 
that's not working)

I just started with the R last week , and I am trying to get up to
speed with this language. And while I have many years
of experience with various programming languages, many which
I have learned on my own, I am encountering a number of things
that are slowing me down. I have downloaded the various intro/reference
materials.

I am super-happy to have found this list though, everyone seems
friendly and helpful, and I am learning a lot from it. Any new/additional
documentation would be much welcome.

_


esh_getintouch_042008
[[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] Documentation General Comments

2008-04-21 Thread esmail bonakdarian
Thank you Rob, I just downloaded it and it looks very useful.

In the meantime I think I solved my immediate problem (and while
pluggin' away also deepened my understanding - or so I will at least
claim :-)

Esmail
 
 I don't know if you will find this helpful, but one of the better online 
 chapters on R Data Objects that I've found is by Tomas Aragon
 and Wayne Enanoria.  Among other nice attributes it has some tables that 
 summarize improtant functions for working with different data objects.
 The URL is:
 http://www.medepi.net/epir/epir_chap02.pdf
 
 
 Rob Baer


_
Pack up or back up–use SkyDrive to transfer files or keep extra copies. Learn 
how.

sh_skydrive_packup_042008
[[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] representing binary data for Genetic Algorithm in R

2008-04-20 Thread esmail bonakdarian
Hello all,

I am trying to implement a simple Genetic Algorithm. I am doing this
in R since I want access to the statistical functions (eg lm) it
provides.

I'm not new to programming, or GAs, but I am totally new to R (the
package and the language), and I am hoping someone could help with
these questions:

1. I am going to use a binary representation, it seems that vectors of
   Factors would be what I should use? I would specify values of
   TRUE/FALSE instead? Or is there a better choice for manipulating
   binary strings? Does this seem like a legitimate way to create a
   random one: sample(0:1, vec_size, replace=T)

2. Am I correct in assuming that R is interpreted (and hence is going to
   be noticeably slower than compiled languages)?

3. Is there some sort of debugging facility? 

4. Does anyone know how to do an effective search for R in google (or
   other search engines). The fact that this is a single letters seems
   to have most applications ignore this input. This might help me in
   finding answers to some other basic questions I have (such as is
   there an equivalent function to printf in R? cat and print
   are not quite working right for me -- but I need to dig deeper into
   the documentation)

I am not sure if this belongs into the help or development group, so
I'm giving this a try. If this is the wrong group to post in, please
let me know and I'll repost.

There will probably be more elementary questions (I am reading the
various manuals too .. but if anyone has some other favorite sites
they want to recommend please do so)

Thanks,
Esmail

ps: I there a USENET group dedicated to R?

pps: I am also exploring ways of calling R functions from Java, if
 anyone has any comments regarding that please share.
_
Going green? See the top 12 foods to eat organic.

1N1653A
[[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] representing binary data for Genetic Algorithm in R

2008-04-20 Thread esmail bonakdarian




Hello!

 Dear Esmail,
 
 you really have to have a look at some introduction to R (e.g. 
 http://cran.r-project.org/doc/manuals/R-intro.pdf), but see the Manual 
 section in R website (http://www.r-project.org/). That would answer many 
 of your questions.

point well taken, I am a bit in a hurry with a project I inherited unexpectedly,
so I was trying to take some shortcuts in trying to get up to speed intending to
dig into the documentation more in depth as soon as possible. I do have the
documents you mention and have been trying to scan them.

thanks for your post,

Esmail

_
Going green? See the top 12 foods to eat organic.

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