[R] Problem with uniroot.all

2013-12-26 Thread Aurélien Philippot
Dear R experts,
I am trying to find all the solutions of an equation. Here is an example:


integrand1<- function(x){1/x*dnorm(x)}
integrand2<- function(x){1/(2*x-50)*dnorm(x)}

integrand3<- function(x,C){
  cd<- 1/(x+C)
  return(cd)
}

res<- function(C){
  ce<-integrate(integrand1, lower=1, upper=50)$value+ integrate(integrand2,
lower=50, upper=100)$value-integrate(integrand3, C=C,lower=1,
upper=100)$value
  return(ce)
}

I want to find all the roots of res.

First, I used uniroot to ensure that a solution existed, and it works.
uniroot(res, c(1,1000))

But,  there might be other roots, and I wanted to use the function uniroot.
library(rootSolve)
uniroot.all(res, c(1,1000))

I obtain an error message:
"Error in integrate(integrand3, C = C, lower = 1, upper = 100) :
  evaluation of function gave a result of wrong length
In addition: Warning message:
In x + C : longer object length is not a multiple of shorter object length"

I don't understand what is wrong? Thank you very much for any suggestion!
Aurelien

[[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] Problem with uniroot.all

2013-12-26 Thread Duncan Murdoch

On 13-12-26 6:26 AM, Aurélien Philippot wrote:

Dear R experts,
I am trying to find all the solutions of an equation. Here is an example:


integrand1<- function(x){1/x*dnorm(x)}
integrand2<- function(x){1/(2*x-50)*dnorm(x)}

integrand3<- function(x,C){
   cd<- 1/(x+C)
   return(cd)
}

res<- function(C){
   ce<-integrate(integrand1, lower=1, upper=50)$value+ integrate(integrand2,
lower=50, upper=100)$value-integrate(integrand3, C=C,lower=1,
upper=100)$value
   return(ce)
}

I want to find all the roots of res.

First, I used uniroot to ensure that a solution existed, and it works.
uniroot(res, c(1,1000))

But,  there might be other roots, and I wanted to use the function uniroot.
library(rootSolve)
uniroot.all(res, c(1,1000))

I obtain an error message:
"Error in integrate(integrand3, C = C, lower = 1, upper = 100) :
   evaluation of function gave a result of wrong length
In addition: Warning message:
In x + C : longer object length is not a multiple of shorter object length"

I don't understand what is wrong? Thank you very much for any suggestion!


You can often use traceback() to find where the error came from:

3: integrate(integrand3, C = C, lower = 1, upper = 100) at #2
2: f(xseq, ...)
1: uniroot.all(res, c(1, 1000))

Not very informative, but notice that on line 2 the argument to f is 
called xseq.  If you call debug(res) and try again, it will break on the 
first call to res, and you can print C:


Browse[2]> C
  [1]1.00   10.99   20.98   30.97   40.96   50.95   60.94   70.93 
 80.92   90.91  100.90  110.89  120.88  130.87  140.86  150.85
 [17]  160.84  170.83  180.82  190.81  200.80  210.79  220.78  230.77 
240.76  250.75  260.74  270.73  280.72  290.71  300.70  310.69
 [33]  320.68  330.67  340.66  350.65  360.64  370.63  380.62  390.61 
400.60  410.59  420.58  430.57  440.56  450.55  460.54  470.53
 [49]  480.52  490.51  500.50  510.49  520.48  530.47  540.46  550.45 
560.44  570.43  580.42  590.41  600.40  610.39  620.38  630.37
 [65]  640.36  650.35  660.34  670.33  680.32  690.31  700.30  710.29 
720.28  730.27  740.26  750.25  760.24  770.23  780.22  790.21
 [81]  800.20  810.19  820.18  830.17  840.16  850.15  860.14  870.13 
880.12  890.11  900.10  910.09  920.08  930.07  940.06  950.05

 [97]  960.04  970.03  980.02  990.01 1000.00

Aha!  uniroot.all() passes a vector of values to the function, whereas 
uniroot passes values one at a time.  You need to make sure res can 
handle a vector of C values.  The easiest method is to use Vectorize to 
vectorize it:


res <- Vectorize(res)

This isn't very fast, but in your function, it's fine.

Duncan Murdoch

P.S.  You might want to write to the maintainer of uniroot.all to point 
out that the documentation doesn't mention this difference from uniroot.


__
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] Erroneous Column Removing Result From User-Defined Function.....

2013-12-26 Thread Adams, Jean
Levent,

I would suggest that you get your data the way you want it first, then use
the assign() function at the very end of your for() loop.  I also think you
could simplify the arguments in your assign() function.

myfun <- function(dyolu, dadi, dsayi) {
for(i in 1:dsayi) {
ad <- paste(dadi, i, sep="")
 yol <- paste(dyolu, dadi, i, ".csv", sep="")
dat <- read.csv(yol, sep=";", dec=",", header=T)
 dat <- dat[, -12]
assign(ad, dat, envir=globalenv())
}
 }

Jean



On Wed, Dec 25, 2013 at 11:09 AM, Levent TERLEMEZ
wrote:

> Dear Users,
> I have a little problem with user-defined function. I would like to remove
> columns from a data frame using a user-defined function but i am getting a
> "Error in ad[, -12] : incorrect number of dimensions” error.  The
> "ad<-ad[,-12]” and similar commands work from R console but i couldn’t make
> them run from my function. The function and the type imformation of one of
> my obtained data objects is below.
>
> Thanks in advance for your tips and helpful answers,
> Levent TERLEMEZ.
>
>
> Error:
> Error in ad[, -12] : incorrect number of dimensions
>
> My Function:
> function (dyolu,dadi,dsayi)
> {
> for(i in 1:dsayi)
> {
> ad<-paste(dadi,i,sep="")
> yol<-paste(dyolu,dadi,i,".csv",sep="")
> print(ad)
> print(yol)
> assign(eval(substitute(ad)),
> read.csv(yol,sep=";",dec=",",header=T), envir = globalenv())
> ad<-ad[,-12]> Here I would like to remove
> some columns but i even can’t remove one. I tried all the suggestion on
> internet forums.
> #dat[ , -which(names(dat) %in% c("z","u"))]
> }
> }
>
> dyolu: The string type variable containing the path of the csv  files that
> will be read.
> dadi: The string type variable containing the name of csv files that will
> be read.
> dsayi: The numeri type variabe containing the number of files that will be
> read.
>
> My Data Object’s Attributes:
> > str(be1)
> 'data.frame': 74 obs. of  16 variables:
>  $ Avi.TimeStamp: Factor w/ 74 levels " 0:00:40.00",..: 2 1 3 4 5 6 7 8 9
> 10 ...
>  $ Frame: int  32949 32950 32952 32953 32954 32955 32957 32961
> 32962 32963 ...
>  $ Spot.x   : num  274 275 275 275 275 ...
>  $ Spot.y   : num  277 277 277 277 277 ...
>  $ Pupil.x  : num  240 242 242 241 241 ...
>  $ Pupil.y  : num  320 320 320 320 320 ...
>  $ Pupil.r  : num  47.7 47.5 47.6 47.8 47.7 ...
>  $ Scene.x  : num  46.2 30.3 34.6 30.4 43.1 ...
>  $ Scene.y  : num  102.5 71 71.9 73.7 92.2 ...
>  $ Mouse.x  : int  287 287 287 287 287 287 287 287 287 287 ...
>  $ Mouse.y  : int  570 570 570 570 570 570 570 570 570 570 ...
>  $ X: logi  NA NA NA NA NA NA ...
>  $ H: int  0 0 0 0 0 0 0 0 0 0 ...
>  $ M: int  0 0 0 0 0 0 0 0 0 0 ...
>  $ S: int  40 40 41 41 41 41 41 41 41 41 ...
>  $ Split: int  96 0 8 12 16 20 28 44 48 52 …
>
> This data frame is obtained with the assign command from my function.
>
>
> [[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.
>
>

[[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] Fitdistr and mle

2013-12-26 Thread Ben Bolker
On 13-12-26 04:13 AM, Tia Borrelli wrote:
> 
> Thank you, this is code i'm running, very simple but my problem was on
> the interpretation of the difference between the functions.
> 
> library(fImport)
> data.oggi = Sys.timeDate()
> ftse_mib = yahooSeries("FTSEMIB.MI", from="2009-09-01", to=data.oggi)
> portaf <- ftse_mib[,6]
> mrk_ftse <- portaf$FTSEMIB.MI.Adj.Close
> returns(mrk_ftse)
> library(quantmod)
> ret <- dailyReturn(portaf[,1])
> library(MASS)
> fitting <- fitdistr(ret,densfun="normal")
> print(c(mean(ret),sd(ret)))
> library(stats4)
> loglik <- function(media=0, devstd=1){
>   -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
> }
> mle(loglik)
> 

  Thank you.

  I was wrong: a closer look inside the guts of MASS::fitdistr (which
you can do yourself) reveals that for densfun="normal" it is in fact
just calculating the mean and standard deviation of the data (and
scaling the standard deviation to convert from the usual unbiased
estimate of the standard deviation to the MLE).  In contrast, mle() is
using numerical optimization.

  If you look at the coefficients:

  fitdistr   mle
mean -3.270946e-06 -2.332673e-06
sd1.692252e-02  1.697018e-02

and the standard errors shown by fitdistr
   mean sd
 ( 5.102331e-04) ( 3.607893e-04)

  you'll see that although the mle and fitdistr estimate of the mean
vary *relatively* (the magnitude of the fitdistr estimate is 50% larger
than that from mle), relative to the standard errors they are tiny (the
differences in the mean estimate are two orders of magnitude smaller
than the standard error of the mean).  It is a fact of life when you're
doing numerical optimization that you're never going to get exactly the
right answer, due to various forms of numerical "fuzz" -- you just have
to know enough about the methods, and about your problem, to know
whether the answers are correct within sensible error bounds.


> 
> Il Mercoledì 25 Dicembre 2013 17:49, Ben Bolker  ha
> scritto:
> Tia Borrelli  yahoo.it> writes:
> 
>>  Thanks for answering, in ret i've the returns of FTSE MIB (the
>> benchmark stock market index in Italy) and i'm estimating the
>> parametres of the distribution of the returns of the index using
>> different methods. 
> 
>   OK, but this still isn't a *reproducible* example (see e.g.
> http://tinyurl.com/reproducible-000 )
> 
>> I need the mle and i found this two function and i could not
>> understand why the result were different: it's possibile that i
>> obtain different result because in the mle() i don't need to know
>> the original distribution and in the fitdistr() i don't need to know
>> the function i had to maximize?
> 
>   In your example fitdistr() and mle() are doing the same thing under
> the hood, i.e.  using the built-in optim() function to minimize a
> negative log-likelihood function based on the built-in dnorm().
> fitdistr() picks the distribution for you based on your specification
> of which distribution to use; mle() requires you to specify the
> negative log-likelihood function (the mle2() function in the bbmle
> package is an extension of stats4::mle that offers a middle ground,
> e.g. you can say y ~ dnorm(mu,sigma) to specify the fit of a Normal
> distribution).  The differences between the results you get will be
> based on small numerical differences, e.g. the starting values of the
> parameters, or differences in the control parameters for optimization.
> In general you should get very similar, but not necessarily identical,
> answers from these two functions; big differences would probably
> indicate some kind of wonky data or numerical problem.  Again, we
> would need a reproducible example to see precisely what is going on.
> 
> __
> 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-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] Fitdistr and mle

2013-12-26 Thread Tia Borrelli
Thank you so much, you have been very helpful!



Il Giovedì 26 Dicembre 2013 15:39, Ben Bolker  ha scritto:
 
On 13-12-26 04:13 AM, Tia Borrelli wrote:
> 
> Thank you, this is code i'm running, very simple but my problem was on
> the interpretation of the difference between the functions.
> 
> library(fImport)
> data.oggi = Sys.timeDate()
> ftse_mib = yahooSeries("FTSEMIB.MI", from="2009-09-01", to=data.oggi)
> portaf <- ftse_mib[,6]
> mrk_ftse <- portaf$FTSEMIB.MI.Adj.Close
> returns(mrk_ftse)
> library(quantmod)
> ret <- dailyReturn(portaf[,1])
> library(MASS)
> fitting <- fitdistr(ret,densfun="normal")
> print(c(mean(ret),sd(ret)))
> library(stats4)
> loglik <- function(media=0, devstd=1){
>   -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
> }
> mle(loglik)
> 

  Thank you.

  I was wrong: a closer look inside the guts of MASS::fitdistr (which
you can do yourself) reveals that for densfun="normal" it is in fact
just calculating the mean and standard deviation of the data (and
scaling the standard deviation to convert from the usual unbiased
estimate of the standard deviation to the MLE).  In contrast, mle() is
using numerical optimization.

  If you look at the coefficients:

          fitdistr           mle
mean -3.270946e-06 -2.332673e-06
sd    1.692252e-02  1.697018e-02

and the standard errors shown by fitdistr
       mean             sd
( 5.102331e-04) ( 3.607893e-04)

  you'll see that although the mle and fitdistr estimate of the mean
vary *relatively* (the magnitude of the fitdistr estimate is 50% larger
than that from mle), relative to the standard errors they are tiny (the
differences in the mean estimate are two orders of magnitude smaller
than the standard error of the mean).  It is a fact of life when you're
doing numerical optimization that you're never going to get exactly the
right answer, due to various forms of numerical "fuzz" -- you just have
to know enough about the methods, and about your problem, to know
whether the answers are correct within sensible error bounds.


> 
> Il Mercoledì 25 Dicembre 2013 17:49, Ben Bolker  ha
> scritto:
> Tia Borrelli  yahoo.it> writes:
> 
>>  Thanks for answering, in ret i've the returns of FTSE MIB (the
>> benchmark stock market index in Italy) and i'm estimating the
>> parametres of the distribution of the returns of the index using
>> different methods. 
> 
>   OK, but this still isn't a *reproducible* example (see e.g.
> http://tinyurl.com/reproducible-000 )
> 
>> I need the mle and i found this two function and i could not
>> understand why the result were different: it's possibile that i
>> obtain different result because in the mle() i don't need to know
>> the original distribution and in the fitdistr() i don't need to know
>> the function i had to maximize?
> 
>   In your example fitdistr() and mle() are doing the same thing under
> the hood, i.e.  using the built-in optim() function to minimize a
> negative log-likelihood function based on the built-in dnorm().
> fitdistr() picks the distribution for you based on your specification
> of which distribution to use; mle() requires you to specify the
> negative log-likelihood function (the mle2() function in the bbmle
> package is an extension of stats4::mle that offers a middle ground,
> e.g. you can say y ~ dnorm(mu,sigma) to specify the fit of a Normal
> distribution).  The differences between the results you get will be
> based on small numerical differences, e.g. the starting values of the
> parameters, or differences in the control parameters for optimization.
> In general you should get very similar, but not necessarily identical,
> answers from these two functions; big differences would probably
> indicate some kind of wonky data or numerical problem.  Again, we
> would need a reproducible example to see precisely what is going on.
> 
> __
> R-help@r-project.org  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> 

> and provide commented, minimal, self-contained, reproducible code.
> 
> 
[[alternative HTML version deleted]]

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


[R] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread Bartłomiej Taciak
Hi everyone,

My data looks like this one:
   AB C
1 a 1 ff
2 b 1 re
3 c 1 sd
5 a 2 as
6 c 4 fe
7 d 5  tt
8 d 5  tt
9 d 9  oi

I want to add a new column D, which will contain how many an element from
the column A is repeated, like this:
   AB C   D
1 a 1 ff2
2 b 1 re  1
3 c 1 sd  2
5 a 2 as  2
6 c 4 fe   2
7 d 5 tt   3
8 d 5 tt   3
9 d 9 oi   3

I don't want to simplify my data and grouping raws like in this thread:
https://stat.ethz.ch/pipermail/r-help/2011-March/270481.html

Thanks in advance for any help,
best regards,
Bartek

[[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] Здорово, появился вариант, заставить вернуться здоровое зрение

2013-12-26 Thread Alisa Esperova
  Практически каждой субъект, имеет очки, может Это снять и с поддержкой 
нетрудных восстановить свои глазодвигательные мышцы и снова глядеть на мир без 
очков и линз. Это вполне возможно, широкодоступно, неоднократно опробовано и об 
вот этом идет разговор. 
  И если уж Вы хотите восстановить свои глазодвигательные мышцы, прочтите до 
финала. И если уж Вам не достает терпения либо отрезка времени на считывание и 
действительное использование методик - таскайте очки. 
  Успехи были впечатляющими: совершенно все пациенты, кто на тот момент 
имел очки от - трёх до + трёх, целиком избавились от нЭто. Результат метода 
среди больных вдохновил утвердить его В облике простого пошагового цикла. 
http://goo.gl/5o78ou

__
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] Results from Vegan metaMDS varry depending on set.seed

2013-12-26 Thread Vinny Moriarty
I've got an ecological data set that I've worked up to the point of having
a relative abundance matrix I created with the decostand() command in Vegan.


Here is the distance matrix data:

S1<-c(0.4451517, 0.37919827,   0.10590466, 0.06974540)
S2<-c(0.5064846, 0.32464164,  0.09679181, 0.07208191)
S3<-c(0.4481876, 0.26556447, 0.10486995, 0.18137797)
S4<-c(0.5090950, 0.11474913, 0.17636805, 0.19978778)
S5<-c(0.5996147, 0.05875069,  0.24532196, 0.09631260)
S6<-c(0.7122068, 0.04365640, 0.14552133, 0.09861543)
S7<-c(0.6490743, 0.06382979,  0.14396242, 0.14313346)
S8<-c(0.5958636, 0.10774176,  0.1690, 0.12730576)

DF<-rbind(S1,S2,S3,S4,S5,S6,S7,S8)


At first I was having issues with metaMDS producing two distinctly
different NMDS plots at seemingly random intervals as I re-ran my analysis
over multiple runs. I figured out it was because I was not using
set.seedfor my metaMDS call. But now I am concerned that the seemingly
small change
of setting set.seed() has such a large impact on my analysis.

As can be seen in the below oridplots, it looks to me like there is a
change in relative distances between some of the latter  'sites'.



set.seed(1)
mds10<- metaMDS(DF, dist='bray')

ordiplot(mds10,display='sites',type='text')


vs.

set.seed(999)
mds10<- metaMDS(DF, dist='bray')

ordiplot(mds10,display='sites',type='text')


The difference between the two plots is large enough that it would change
my interpretation of my analysis, so as this is my first foray into NMDS I
am a bit concerned. Can someone tell me if this is just part of how NMDS or
Vegan works (different local minimums)? Or does this imply a certain
ambiguity about my data set? Or am I completely misreading the plots.


If I add vector arrows for the 'species' influence like so:

envfit10<-envfit(mds10, DF,perm=999)
plot(mds10, display='sites',type='t')
plot(envfit10)


I can see that the two plots have different 'species' vectors, but it looks
like the relative distance between S5 and some of the other sites changes
between the two plots.

Is one ordiplot more 'correct' than the other? If not, what am I to make of
the difference between plots?

[[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] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread arun
Hi,
Try:
dat1 <- read.table(text="A    B    C
1 a    1    ff
2 b    1    re
3 c    1    sd
5 a    2    as
6 c    4    fe
7 d    5  tt
8 d    5  tt
9 d    9  oi",sep="",header=TRUE,stringsAsFactors=FALSE)

 within(dat1,D<-ave(seq_along(A),A,FUN=length))
A.K.




On Thursday, December 26, 2013 11:28 AM, Bartłomiej Taciak 
 wrote:
Hi everyone,

My data looks like this one:
   A    B     C
1 a     1     ff
2 b     1     re
3 c     1     sd
5 a     2     as
6 c     4     fe
7 d     5      tt
8 d     5      tt
9 d     9      oi

I want to add a new column D, which will contain how many an element from
the column A is repeated, like this:
   A    B     C   D
1 a     1     ff    2
2 b     1     re  1
3 c     1     sd  2
5 a     2     as  2
6 c     4     fe   2
7 d     5     tt   3
8 d     5     tt   3
9 d     9     oi   3

I don't want to simplify my data and grouping raws like in this thread:
https://stat.ethz.ch/pipermail/r-help/2011-March/270481.html

Thanks in advance for any help,
best regards,
Bartek

    [[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-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] Counts of duplicate rows as a new column without grouping of duplicates raws.

2013-12-26 Thread arun
Also, you could try:
library(plyr)
ddply(dat1,.(A),mutate, D=length(A))

#or
library(data.table)
 dt1 <- data.table(dat1,key='A')
 dt1[,D:=.N,by='A']
A.K.




On , arun  wrote:
Hi,
Try:
dat1 <- read.table(text="A    B    C
1 a    1    ff
2 b    1    re
3 c    1    sd
5 a    2    as
6 c    4    fe
7 d    5  tt
8 d    5  tt
9 d    9  oi",sep="",header=TRUE,stringsAsFactors=FALSE)

 within(dat1,D<-ave(seq_along(A),A,FUN=length))
A.K.





On Thursday, December 26, 2013 11:28 AM, Bartłomiej Taciak 
 wrote:
Hi everyone,

My data looks like this one:
   A    B     C
1 a     1     ff
2 b     1     re
3 c     1     sd
5 a     2     as
6 c     4     fe
7 d     5      tt
8 d     5      tt
9 d     9      oi

I want to add a new column D, which will contain how many an element from
the column A is repeated, like this:
   A    B     C   D
1 a     1     ff    2
2 b     1     re  1
3 c     1     sd  2
5 a     2     as  2
6 c     4     fe   2
7 d     5     tt   3
8 d     5     tt   3
9 d     9     oi   3

I don't want to simplify my data and grouping raws like in this thread:
https://stat.ethz.ch/pipermail/r-help/2011-March/270481.html

Thanks in advance for any help,
best regards,
Bartek

    [[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-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] spending most of my time in assignments?

2013-12-26 Thread Ross Boylan
On Thu, 2013-12-19 at 20:37 -0500, Duncan Murdoch wrote:
> On 13-12-19 6:37 PM, Ross Boylan wrote:
> > My code seems to be spending most of its time in assignment statements,
> > in some cases simple assignment of a model frame or model matrix.
> >
> > Can anyone provide any insights into what's going on, or how to speed
> > things up?
> 
> You are seeing a lot of time being spent on complex assignments.  For 
> example, line 158 is
> 
> data(sims.c1[[k]]) <- sp
> 
> That makes a function call to `data<-` to do the assignment, and that 
> could be slow.  Since it's an S4 method there's a bunch of machinery 
> involved in dispatching it; most of that would not have line number 
> information, so it'll be charged to that line.
> 
> I can't really suggest how to speed it up.
> 
> Duncan Murdoch
Simply reexpressing the same computations without assigning to S4 slots
or dispatching using S4 cut the execution time to under 50% of what it
had been.

I attempted to recreate this with a small bit of code, but got nothing
like the effect in the original. 

Here's the example; v1 is like my original code and v2 is like the
restructured code:
slow <- setClass("slow",
 representation(form="formula",  data="data.frame", 
mm="matrix"))
slowb <- setClass("slowb", representation(form="formula", data="data.frame", 
mm="matrix"))
slowc <- setClass("slowc", representation(form="formula", data="data.frame", 
mm="matrix"))
# original had only 3 classes
# fake data.  Using my real data didn't seem to affect relative times much.
# real data had ~ 100 columns in model matrix   


  
mydata <- data.frame(a=rnorm(1500), b=rnorm(1500), 
c=rnorm(1500),d=as.factor(rep(c("x", "y", "z"), 500)))
myformula <- ~a*d + b*d + c*d
mymm <- model.matrix(myformula, mydata)

if (!isGeneric("putData<-"))
  setGeneric("putData<-", function(obj, value) standardGeneric("putData<-"))

setMethod("putData<-", c("slow", "data.frame"),
  function(obj, value){
myf <- model.frame(obj@form, value)
obj@data <- myf
obj@mm <- model.matrix(obj@form, myf)
obj
  }
)
setMethod("putData<-", c("slowb", "data.frame"),
  function(obj, value){
myf <- model.frame(obj@form, value)
obj@data <- myf
obj@mm <- model.matrix(obj@form, myf)
obj
  }
)
setMethod("putData<-", c("slowc", "data.frame"),
  function(obj, value){
myf <- model.frame(obj@form, value)
obj@data <- myf
obj@mm <- model.matrix(obj@form, myf)
obj
  }
)

v1 <- function(n) {
  s <- list(slow(form=myformula, mm=mymm), 1:5)
  for (i in 1:n) {
mydata$b <- rnorm(nrow(mydata))
putData(s[[1]]) <- mydata
  }
  mm <- s[[1]]@mm
}

# v2 eliminates the dispatch on putData and the assignment
# to the S4 slot
v2 <- function(n) {
  s <- slow(form=myformula, mm=mymm)
  for (i in 1:n) {
mydata$b <- rnorm(nrow(mydata))
myf <- model.frame(s@form, mydata)
mm <- model.matrix(s@form, myf)
  }
  mm
}

> system.time(r <- v1(100)) 
>   
>   
>   
user  system elapsed
   0.304   0.000   0.307


   
> system.time(r <- v2(100)) 
>   
>   
>   
user  system elapsed
0.260.000.26

Ross Boylan

> 
> >
> > For starters, is it possible that the reports are not accurate, or that
> > I am misreading them.  In R 3.0.1 (running under ESS):
> >   > Rprof(line.profiling=TRUE)
> >   > system.time(r <- totalEffect(dodata[[1]], dodata[[2]], 1:3, 4))
> >  user  system elapsed
> >21.629   0.756  22.469
> > !> Rprof(NULL)
> >   > summaryRprof(lines="both")
> >   $by.self
> >  self.time self.pct total.time total.pct
> >   box.R#158   6.7429.56  13.06 57.28
> >   simulator.multinomial.R#64  2.9212.81   2.96 12.98
> >   simulator.multinomial.R#63  2.7612.11   2.76 12.11
> >   box.R#171   2.5411.14   5.08 22.28
> >   simulator.d1.R#70   

Re: [R] The difference between SAS and R

2013-12-26 Thread Greg Snow
When I first saw that comic, my thought was "where would APL fit in
that graph?" (http://en.wikipedia.org/wiki/APL_(programming_language))

On Sun, Dec 22, 2013 at 2:00 AM, peter dalgaard  wrote:
>
> On 20 Dec 2013, at 18:53 , Kevin Wright  wrote:
>
>> SAS uses words.  R uses symbols.
>>
>> See this:
>> http://xkcd.com/1306/
>>
>> (Yes, I know IML uses plenty of symbols.  It's just supposed to be funny.
>> And somewhat true.)
>>
>
> Actually, R is on par with C++ when it comes to _prefixed_ symbols like 
> #missedthepoint, which is what the XKCD strip is about. In terms of general 
> operators, SAS would be approaching COBOL (famous for "ADD YEARS TO AGE" like 
> construct) were it not for the DATA step and the %MACRO language.
>
> (The preceding XKCD (1305) is quite nice, by the way.)
>
>> --
>> Kevin Wright
>>
>>   [[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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd@cbs.dk  Priv: pda...@gmail.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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] R command execution at specific time from within R

2013-12-26 Thread Greg Snow
If you really have to do this within R (see Dirk's reply for other
options) then the tclTaskSchedule function in the tcltk2 package is
one option.  However this can be dangerous if you are using the same
instance of R while waiting.  You might accidentally change something
that affects or is affected by the scheduled code and create a hard to
find bug.


Another is just to use a repeat loop with Sys.sleep for the delays.
This will not allow you to use that instance of R for anything else.



On Mon, Dec 23, 2013 at 8:57 AM, Costas Vorlow  wrote:
> Hello,
>
> I am trying to write a code that executes an R command at specific time
> intervals.
>
> É want R to do that instead of the operating system.
>
> Any help/pointer extremely welcome.
>
> Thanks in advance,
> Costas
>
> [[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.
>



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] Categorial data analysis

2013-12-26 Thread Michael Friendly

On 12/25/2013 3:58 AM, Guy Rotem wrote:

Hi,
I want to analysis the effect of two continuous independent variables (rain
and slope) on a categorical dependent variable (soil type).
Do you know how doing it?


You could look a my short course notes for Visualizing Categorical Data 
with SAS and R, http://www.datavis.ca/courses/VCD/,

Section 5, Polytomous response models.  Various possibilities exist,
depending on whether soil type is ordinal or nominal.

Some of the R functions (among others) for fitting these models are:
polr in the MASS package
multinom in the nnet package


HTH
-Michael

--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

__
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] Problems installing some packages using Ubuntu 12.04

2013-12-26 Thread Hallikainen Ville (METLA)
Dear R people using Ubuntu

I'd like to run R in my Ubuntu 12.04. I have R v. 3.02. Many packages are 
available, but package mi is not. Loading indicates a zero status. I have tried 
to install it from carn using the terminal commands and via R-studio. I have 
install it into my Mac recently and it works. What's wrong with it?

Ville Hallikainen, The Finnish Forest Research Institute

[[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] Problems installing some packages using Ubuntu 12.04

2013-12-26 Thread Uwe Ligges



On 26.12.2013 23:39, Hallikainen Ville (METLA) wrote:

Dear R people using Ubuntu

I'd like to run R in my Ubuntu 12.04. I have R v. 3.02. Many packages are 
available, but package mi is not. Loading indicates a zero status. I have tried 
to install it from carn using the terminal commands and via R-studio. I have 
install it into my Mac recently and it works. What's wrong with it?



And what is the error message?

Best,
Uwe Ligges


Ville Hallikainen, The Finnish Forest Research Institute

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