[R] unique mismatch in R and Excel

2013-12-24 Thread Koushik Saha
i have a wired problem. i want to count the unique entry in a certain
column.Here i have attached my csv file.

i am doing this to get the unique entries in the column.

dat-read.csv(C:/Project/Gawk-scripts/Book1.csv)
names(dat)-c(user_name)
unique(dat$user_name)

results says i have 170 unique values.


But i am doing remove duplicate entries  in excel i am having 147 unique
entries in the column.

Can anyone explain why there is a mismatch of the results or i am doing
something wrong.

Regards
Koushik
__
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] Test to determine if there is a difference between two means

2013-12-24 Thread wesley bell
Hi,
I have a data set where there are 20 experiments which each ran for 10 minutes. 
In each experiment an insect had a choice to spend time in one of two chambers. 
Each experiment therefore has number of seconds spent in each chamber. I want 
to know whether there is a difference in the mean time spent in each chamber.

I was going to do a t-test but was advised that there was a better way, 
something about introducing random numbers? I was hoping someone could help?

Thanks
Wes
[[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] Mean: category wise within a data frame

2013-12-24 Thread Nandini Jayakumar
Hello all

I have a table a sample of which is as follows:
















Categories



Variable (x)



Frequencies





1

1

1

1

1



0.009

0.867

0.567

0.765

0.445



1003

1200

987

134

890





2

2

2

2

2



0.007

0.768

0.789

0.544

0.987



899

707

865

678

889





3

3

3

3

3



0.898

0.887

0.560

0.098

0.987



544

677

934

467

876





40

40

40

40

40



0.786

0.342

0.456

0.987

0.123



843

987

675

467

223






Basically I have 40 categories and each category has several hundred variables. 
I want to calculate the average per category, that is variable * 
frequency/Summation of frequencies. I want to do it for each category 
separately. Since i have many categories i do not want to use the subset() 
function 40 times. Is it possible to do it within a single data frame?

Really appreciate any help. Thank you.
  
[[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] error in ca.jo

2013-12-24 Thread mamush bukana
Dear Pat,
I tried your suggestion:
for(i in 1:N1){
for(j in 1:N2){
co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
i,j,],y1[i,j,])),type=trace, K=2,
spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
NaN)
}}

and the earlier error does not show up. However, when I try to extract some
results from the co.jo function, it does not work the way it does without
the tryCatch thing and there appears another error. For example:

for(i in 1:N1){
for(j in 1:N2){
co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
i,j,],y1[i,j,])),type=trace, K=2,
spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
NaN)

cor1-cajorls(co,r=1)
}}

Error in cajorls(vecm, r = 1) :
Please, provide object of class 'ca.jo' or 'cajo.test' as 'z'.

So it seems tryCatch is disabling ca.jo from running properly. My
intention is to run this function (ca.jo) and extract some estimates for
further computation.

regards

Bukana







On Mon, Dec 23, 2013 at 5:09 PM, Patrick Burns pbu...@pburns.seanet.comwrote:

 There is a fundamental problem with your
 code, and there is the problem that you
 have (sort of) identified.

 The fundamental problem is that you are
 only going to get the results of the last
 call to 'ca.jo' that is done -- assuming
 it were to run.  You presumably want to
 save some information from each of the
 computations.

 You can get the loops to run even when you
 run into an error with some combinations
 by using 'try' or 'tryCatch'.  There is an
 example in Circle 8.3.13 of 'The R Inferno'.

 http://www.burns-stat.com/documents/books/the-r-inferno/

 If you have a question related to the actual
 function as opposed to general problems with
 R, then the r-sig-finance mailing list would
 be appropriate (you need to subscribe before
 posting).

 I'm not sure if this is enough of a hint for
 you or not.  If not, then trying to formulate
 a more explicit question might help.  (There
 are some suggestions in Circle 9 of 'The R
 Inferno'.)

 Pat



 On 23/12/2013 17:07, mamush bukana wrote:

 Dear all,
 I fit co-integration function between two integrated variables(y1 and y2)
 over different grid points:

 for(i in 1:N1){
 for(j in 1:N2){
 co-ca.jo(data.frame(cbind(y2[i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL)
 }}

 I have already extracted grid points with integrated time series. However,
 when I run the above function, there happens an error

 Error in solve.default(t(V) %*% SKK %*% V) :
system is computationally singular: reciprocal condition number =
 1.10221e-35

 May you suggest me how to fix this problem please?

 Thanks in advance

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


 --
 Patrick Burns
 pbu...@pburns.seanet.com
 twitter: @burnsstat @portfolioprobe
 http://www.portfolioprobe.com/blog
 http://www.burns-stat.com
 (home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')


[[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-24 Thread Ben Bolker
Tia Borrelli tiaborrelli at yahoo.it writes:

 
 Hello, i'm using R for the exploration of a time series and i'm stuck in a
problem with the fitting of the distribution.
 What's the difference between fitdistr and mle?

  Hard to say without a reproducible example.  In the example below
the answers are not identical (different starting values etc.) but
they're closer than in your example.

  (I assume that what you're really doing is more complicated than
the trivial example shown here, since the MLEs of the Normal distribution
parameters are very easy ...)

set.seed(101)
ret - rnorm(1,mean=-1.5e-5,sd=1.69e-2)
MASS::fitdistr(ret,densfun=normal)
##meansd 
##   7.419639e-05   1.678380e-02 
##  (1.678380e-04) (1.186794e-04)

library(stats4)
loglink - function(media=0, devstd=1){
  -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
}
mle(loglink)
##media   devstd 
## 7.402637e-05 1.680457e-02 


 library(MASS)
 fitting - fitdistr(ret,densfun=normal)
 print(c(mean(ret),sd(ret)))
 -
 The output of fitdistr is: 
   mean             sd      
   -1.526547e-05    1.692554e-02 
  ( 5.105564e-04) ( 3.610179e-04)
 -
 
 library(stats4)
 loglink - function(media=0, devstd=1){
   -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
 }
 mle(loglink)
 -
 
 The output of mle is:
 Call:
 mle(minuslogl = loglink)
 
 Coefficients:
         media        devstd 
 -1.593559e-05  1.695075e-02 
 
 Thank you for the help.

__
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] Mean: category wise within a data frame

2013-12-24 Thread David Carlson
Please do not use html formatting in your messages to the list.
The format codes are stripped and you table becomes a single
long column (see below). Only use plain text emails and use the
results of dput(mydata) to insert your data into the message.

As to your question. Look at the aggregate() function:

?aggregate

David Carlson

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Nandini
Jayakumar
Sent: Tuesday, December 24, 2013 4:36 AM
To: r-help@r-project.org
Subject: [R] Mean: category wise within a data frame

Hello all

I have a table a sample of which is as follows:
















Categories



Variable (x)



Frequencies





1

1

1

1

1



0.009

0.867

0.567

0.765

0.445



1003

1200

987

134

890





2

2

2

2

2



0.007

0.768

0.789

0.544

0.987



899

707

865

678

889





3

3

3

3

3



0.898

0.887

0.560

0.098

0.987



544

677

934

467

876





40

40

40

40

40



0.786

0.342

0.456

0.987

0.123



843

987

675

467

223






Basically I have 40 categories and each category has several
hundred variables. I want to calculate the average per category,
that is variable * frequency/Summation of frequencies. I want to
do it for each category separately. Since i have many categories
i do not want to use the subset() function 40 times. Is it
possible to do it within a single data frame?

Really appreciate any help. Thank you.
  
[[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] Test to determine if there is a difference between two means

2013-12-24 Thread Bert Gunter
Inline below.

 Cheers,

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom.
H. Gilbert Welch




On Tue, Dec 24, 2013 at 7:38 AM, wesley bell wesleybel...@yahoo.com wrote:
 Hi,
 I have a data set where there are 20 experiments which each ran for 10 
 minutes. In each experiment an insect had a choice to spend time in one of 
 two chambers. Each experiment therefore has number of seconds spent in each 
 chamber. I want to know whether there is a difference in the mean time spent 
 in each chamber.

Yes, there is. Always.


 I was going to do a t-test but was advised that there was a better way, 
 something about introducing random numbers? I was hoping someone could help?

This list is about R, not statistics, although they certainly overlap.
 I suggest you post on stats.stackexchange.com instead for statistics
help. Better yet, you might do well to talk with a local expert about
statistical issues, as you are obviously weak here.


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


[R] replace NA with another vector

2013-12-24 Thread Kathryn Lord
Dear R users,

I have two different vectors like below

x - c( NA, NA, 3, NA, 1)
y - c( 20, 40 ,50)

Combining x and y, I'd like to create new vector z

z - c(20, 40, 3, 50, 1)


Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[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] unique mismatch in R and Excel

2013-12-24 Thread Duncan Murdoch

On 13-12-24 4:08 AM, Koushik Saha wrote:

i have a wired problem. i want to count the unique entry in a certain
column.Here i have attached my csv file.

i am doing this to get the unique entries in the column.

dat-read.csv(C:/Project/Gawk-scripts/Book1.csv)
names(dat)-c(user_name)
unique(dat$user_name)

results says i have 170 unique values.


But i am doing remove duplicate entries  in excel i am having 147 unique
entries in the column.

Can anyone explain why there is a mismatch of the results or i am doing
something wrong.



Surely you can just compare the lists.  147 is not that many entries, 
and if they are sorted, it will be easy.


Duncan Murdoch


Regards
Koushik



__
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] replace NA with another vector

2013-12-24 Thread arun
 z - x
 z[is.na(z)] - y
 z
#[1] 20 40  3 50  1


A.K.



On Tuesday, December 24, 2013 12:06 PM, Kathryn Lord 
kathryn.lord2...@gmail.com wrote:
Dear R users,

I have two different vectors like below

x - c( NA, NA, 3, NA, 1)
y - c( 20, 40 ,50)

Combining x and y, I'd like to create new vector z

z - c(20, 40, 3, 50, 1)


Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

    [[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] Mean: category wise within a data frame

2013-12-24 Thread arun
HI,
May be this helps:


Please use ?dput() to show the example dataset
dat1 - read.table(text=Categories  Variable Frequencies
1 0.009 1003
1 0.867 1200
1 0.567 987
1 0.765 134
1 0.445 890 
2 0.007 899   
2 0.768 707    
2 0.789 865 
2 0.544  678    
2  0.987 889,sep=,header=TRUE)
library(plyr)
res -  
ddply(transform(dat1,NewCol=Variable*Frequencies),.(Categories),summarize, 
Avg=mean(NewCol/sum(Frequencies)))


A.K.




On Tuesday, December 24, 2013 10:43 AM, Nandini Jayakumar 
nandini_d...@hotmail.com wrote:
Hello all

I have a table a sample of which is as follows:




    
    
    
    



    
    
    
    
        
            Categories

        
        
            Variable (x)

        
        
            Frequencies

        
    
    
        
            1

            1

            1

            1

            1

        
        
            0.009

            0.867

            0.567

            0.765

            0.445

        
        
            1003

            1200

            987

            134

            890

        
    
    
        
            2

            2

            2

            2

            2

        
        
            0.007

            0.768

            0.789

            0.544

            0.987

        
        
            899

            707

            865

            678

            889

        
    
    
        
            3

            3

            3

            3

            3

        
        
            0.898

            0.887

            0.560

            0.098

            0.987

        
        
            544

            677

            934

            467

            876

        
    
    
        
            40

            40

            40

            40

            40

        
        
            0.786

            0.342

            0.456

            0.987

            0.123

        
        
            843

            987

            675

            467

            223

        
    



Basically I have 40 categories and each category has several hundred variables. 
I want to calculate the average per category, that is variable * 
frequency/Summation of frequencies. I want to do it for each category 
separately. Since i have many categories i do not want to use the subset() 
function 40 times. Is it possible to do it within a single data frame?

Really appreciate any help. Thank you.
                          
    [[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] error in ca.jo

2013-12-24 Thread Jeff Newmiller
Once you start describing your code in language that indicates you have read 
some of the recommended materials, we can make some progress. For example, you 
are still posting in HTML format so your code is getting mangled (see Posting 
Guide). You don't seem to understand what tryCatch does (see ?tryCatch; hint... 
it does not alter the result returned by ca.jo, nor does it guarantee that any 
result will be returned). For that matter, you don't seem to understand how to 
give valid inputs to ca.jo, but then you don't seem to understand how to 
provide a reproducible example either (see for example 
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example).

In short, if you ask us why one divided by zero doesn't give 3, we have to 
wonder if you don't belong in some other educational forum, because this is not 
a mathematics theory support group... it is about R. Please read or otherwise 
absorb the recommended background materials mentioned here and then ask clear 
questions here about R, or find some one-on-one help offline.
---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

mamush bukana mamushbuk...@gmail.com wrote:
Dear Pat,
I tried your suggestion:
for(i in 1:N1){
for(j in 1:N2){
co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
i,j,],y1[i,j,])),type=trace, K=2,
spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
NaN)
}}

and the earlier error does not show up. However, when I try to extract
some
results from the co.jo function, it does not work the way it does
without
the tryCatch thing and there appears another error. For example:

for(i in 1:N1){
for(j in 1:N2){
co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
i,j,],y1[i,j,])),type=trace, K=2,
spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
NaN)

cor1-cajorls(co,r=1)
}}

Error in cajorls(vecm, r = 1) :
Please, provide object of class 'ca.jo' or 'cajo.test' as 'z'.

So it seems tryCatch is disabling ca.jo from running properly. My
intention is to run this function (ca.jo) and extract some estimates
for
further computation.

regards

Bukana







On Mon, Dec 23, 2013 at 5:09 PM, Patrick Burns
pbu...@pburns.seanet.comwrote:

 There is a fundamental problem with your
 code, and there is the problem that you
 have (sort of) identified.

 The fundamental problem is that you are
 only going to get the results of the last
 call to 'ca.jo' that is done -- assuming
 it were to run.  You presumably want to
 save some information from each of the
 computations.

 You can get the loops to run even when you
 run into an error with some combinations
 by using 'try' or 'tryCatch'.  There is an
 example in Circle 8.3.13 of 'The R Inferno'.

 http://www.burns-stat.com/documents/books/the-r-inferno/

 If you have a question related to the actual
 function as opposed to general problems with
 R, then the r-sig-finance mailing list would
 be appropriate (you need to subscribe before
 posting).

 I'm not sure if this is enough of a hint for
 you or not.  If not, then trying to formulate
 a more explicit question might help.  (There
 are some suggestions in Circle 9 of 'The R
 Inferno'.)

 Pat



 On 23/12/2013 17:07, mamush bukana wrote:

 Dear all,
 I fit co-integration function between two integrated variables(y1
and y2)
 over different grid points:

 for(i in 1:N1){
 for(j in 1:N2){
 co-ca.jo(data.frame(cbind(y2[i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL)
 }}

 I have already extracted grid points with integrated time series.
However,
 when I run the above function, there happens an error

 Error in solve.default(t(V) %*% SKK %*% V) :
system is computationally singular: reciprocal condition number =
 1.10221e-35

 May you suggest me how to fix this problem please?

 Thanks in advance

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


 --
 Patrick Burns
 pbu...@pburns.seanet.com
 twitter: @burnsstat @portfolioprobe
 http://www.portfolioprobe.com/blog
 http://www.burns-stat.com
 (home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')


   [[alternative HTML version deleted]]

__
R-help@r-project.org 

[R] Simplifying an expression with an integral

2013-12-24 Thread Aurélien Philippot
Dear R experts,
I am computing the following integral.

[image: \int_{1,100} \frac{1}{x+Max(x-50,0)} g(x)dx], where g is the
density of the standard normal, and [1,100] is the domain.

1) I use the following code which works fine:
integrand1- function(x){1/x*dnorm(x)}
integrand2- function(x){1/(2*x-50)*dnorm(x)}

res1- integrate(integrand1, lower=1, upper=50)$value+
integrate(integrand2, lower=50, upper=100)$value
res1
0.1116

In other words, I split the max function depending on the value of x in the
domain.

2) Alternatively, I can also compute it by vectorizing the max function
integrand- function (x) ifelse(x50, 1/x*dnorm(x) , 1/(2*x-50)*dnorm(x))
res4- integrate(integrand, lower=1, upper=100)$value
res4
0.1116

3) However, in both cases, the syntax is a little bit heavy and not very
convenient if I want to add more integrals, all of them with a max in the
integrand. Is there a way to have a more concise syntax?
Using max or pmax directly in the definition of the integrand does not work.
For example:
integrand- function(x) {1/(x+max(x-50,0)*dnorm(x))}

res- integrate(integrand, lower=1, upper=100)$value
res
4.60517

Thank you for any suggestion, and merry Christmas!

[[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] unique mismatch in R and Excel

2013-12-24 Thread David Winsemius

On Dec 24, 2013, at 1:08 AM, Koushik Saha wrote:

 i have a wired problem. i want to count the unique entry in a certain
 column.Here i have attached my csv file.

Files named with extension .csv do not typically make it through the R-help 
mail server.

 
 i am doing this to get the unique entries in the column.
 
 dat-read.csv(C:/Project/Gawk-scripts/Book1.csv)
 names(dat)-c(user_name)
 unique(dat$user_name)
 
 results says i have 170 unique values.
 
 
 But i am doing remove duplicate entries  in excel i am having 147 unique
 entries in the column.
 
 Can anyone explain why there is a mismatch of the results or i am doing
 something wrong.
 

Rename the file to have an extension of .txt. Then you mail-client will 
probably label it correctly as a MIME-TEXT file.

-- 
David Winsemius
Alameda, CA, USA

__
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] error in ca.jo

2013-12-24 Thread mamush bukana
Hi Jeff,
From your words (if our words really describe us), I hope you don't expect
me to teach you that this is r-help room. I don't expect you to tell me
that I am a layman. I already know it and that is why I am here seeking a
help. If I am an expert of things I asked here, there is no need for me to
come here with such stupid (in your understanding) question. I know there
are people on this planet who consider themselves advanced only when they
meet stupid people like me - you may be one of them. It is always helpful
for others if you could write a single line with a helping mind than
writing tones of useless junk words. R is applied statistical language, to
be used in different professions. So, don't expect all R users to be
experts like you are. If you can't help people, at least keep yourself
away from helping environment.

Cheers

Bukana


On Tue, Dec 24, 2013 at 5:23 PM, Jeff Newmiller jdnew...@dcn.davis.ca.uswrote:

 Once you start describing your code in language that indicates you have
 read some of the recommended materials, we can make some progress. For
 example, you are still posting in HTML format so your code is getting
 mangled (see Posting Guide). You don't seem to understand what tryCatch
 does (see ?tryCatch; hint... it does not alter the result returned by
 ca.jo, nor does it guarantee that any result will be returned). For that
 matter, you don't seem to understand how to give valid inputs to ca.jo,
 but then you don't seem to understand how to provide a reproducible example
 either (see for example
 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
 ).

 In short, if you ask us why one divided by zero doesn't give 3, we have to
 wonder if you don't belong in some other educational forum, because this is
 not a mathematics theory support group... it is about R. Please read or
 otherwise absorb the recommended background materials mentioned here and
 then ask clear questions here about R, or find some one-on-one help offline.
 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live
 Go...
   Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
 /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.

 mamush bukana mamushbuk...@gmail.com wrote:
 Dear Pat,
 I tried your suggestion:
 for(i in 1:N1){
 for(j in 1:N2){
 co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
 i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
 NaN)
 }}
 
 and the earlier error does not show up. However, when I try to extract
 some
 results from the co.jo function, it does not work the way it does
 without
 the tryCatch thing and there appears another error. For example:
 
 for(i in 1:N1){
 for(j in 1:N2){
 co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
 i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
 NaN)
 
 cor1-cajorls(co,r=1)
 }}
 
 Error in cajorls(vecm, r = 1) :
 Please, provide object of class 'ca.jo' or 'cajo.test' as 'z'.
 
 So it seems tryCatch is disabling ca.jo from running properly. My
 intention is to run this function (ca.jo) and extract some estimates
 for
 further computation.
 
 regards
 
 Bukana
 
 
 
 
 
 
 
 On Mon, Dec 23, 2013 at 5:09 PM, Patrick Burns
 pbu...@pburns.seanet.comwrote:
 
  There is a fundamental problem with your
  code, and there is the problem that you
  have (sort of) identified.
 
  The fundamental problem is that you are
  only going to get the results of the last
  call to 'ca.jo' that is done -- assuming
  it were to run.  You presumably want to
  save some information from each of the
  computations.
 
  You can get the loops to run even when you
  run into an error with some combinations
  by using 'try' or 'tryCatch'.  There is an
  example in Circle 8.3.13 of 'The R Inferno'.
 
  http://www.burns-stat.com/documents/books/the-r-inferno/
 
  If you have a question related to the actual
  function as opposed to general problems with
  R, then the r-sig-finance mailing list would
  be appropriate (you need to subscribe before
  posting).
 
  I'm not sure if this is enough of a hint for
  you or not.  If not, then trying to formulate
  a more explicit question might help.  (There
  are some suggestions in Circle 9 of 'The R
  Inferno'.)
 
  Pat
 
 
 
  On 23/12/2013 17:07, mamush bukana wrote:
 
  Dear all,
  I fit co-integration function between two integrated variables(y1
 and y2)
  over different grid points:
 
  for(i in 1:N1){
  for(j in 1:N2){
  co-ca.jo(data.frame(cbind(y2[i,j,],y1[i,j,])),type=trace, K=2,
  

[R] Transferring data from R to MATLAB via Rmatlab package

2013-12-24 Thread Anton Sylchenko
Hi all,

I spent a whole day on the internet trying to figure this out but no luck,
so I was wondering if anyone would be able to help me.

I am running code that requires me set up variables in R, establish a
connection to MATLAB, set matlab variables based on the previous variables
in R via the connection, close the connection, and then use the established
variables for analysis in MATLAB.

I set up the server through MATLAB by adding the path that contains
MatlabServer.m, and I got this in MATLAB:

 MatlabServer
Running MatlabServer v1.3.5
MATLAB v7.x or higher detected.
Saving with option -V6.
Added InputStreamByteWrapper to dynamic Java CLASSPATH.
--
MATLAB server started!
--
Trying to open server socket (port )...done.

After this, I install and load R.oo, R.utils, and R.matlab into R, and
enter:

matlab - Matlab()
isOpen - open(matlab)
Sys.sleep(15)
print(matlab)

then I set the variables and close the connection.

in my MATLAB window, each variable comes through like this:

Received cmd: 3
Will read MAT file:
/var/folders/4c/s3s3b1rx2yq8n3blvp27cmtwgn/T//RtmpvXWCry/file45dbfd4ac9.mat

however, once i close the connection and try to run matlab code (in matlab)
that uses these variables, they are all undefined and i get an error.

How do I fix this ? The majority of my code is in Matlab and I can't
continue without these variables.

The R.matlab package pdf said something  about needing to establish a
remote connection in order for the values to be stored but I don't know how
to do this eitherR tells me the connection is remote=FALSE so it seems
this is the default.

Can anyone please help me with this ? Any advice would be greatly
appreciated. Even if there is a better way to do this, that would be OK
because I just need the variables to go through.

[[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-24 Thread Tia Borrelli
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. 

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 addition to that i'm going to use quadratic programming for portfolio 
optimization.



Il Martedì 24 Dicembre 2013 17:53, Ben Bolker bbol...@gmail.com ha scritto:
 
Tia Borrelli tiaborrelli at yahoo.it writes:

 
 Hello, i'm using R for the exploration of a time series and i'm stuck in a
problem with the fitting of the distribution.
 What's the difference between fitdistr and mle?

  Hard to say without a reproducible example.  In the example below
the answers are not identical (different starting values etc.) but
they're closer than in your example.

  (I assume that what you're really doing is more complicated than
the trivial example shown here, since the MLEs of the Normal distribution
parameters are very easy ...)

set.seed(101)
ret - rnorm(1,mean=-1.5e-5,sd=1.69e-2)
MASS::fitdistr(ret,densfun=normal)
##        mean            sd    
##   7.419639e-05   1.678380e-02 
##  (1.678380e-04) (1.186794e-04)

library(stats4)
loglink - function(media=0, devstd=1){
  -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
}
mle(loglink)
##        media       devstd 
## 7.402637e-05 1.680457e-02 


 library(MASS)
 fitting - fitdistr(ret,densfun=normal)
 print(c(mean(ret),sd(ret)))
 -
 The output of fitdistr is: 
   mean             sd      
   -1.526547e-05    1.692554e-02 
  ( 5.105564e-04) ( 3.610179e-04)
 -
 
 library(stats4)
 loglink - function(media=0, devstd=1){
   -sum(dnorm(ret, mean=media, sd=devstd, log=TRUE))
 }
 mle(loglink)
 -
 
 The output of mle is:
 Call:
 mle(minuslogl = loglink)
 
 Coefficients:
         media        devstd 
 -1.593559e-05  1.695075e-02 
 
 Thank you for the help.

__
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] [R-SIG-Finance] error in ca.jo

2013-12-24 Thread Joshua Ulrich
On Tue, Dec 24, 2013 at 3:36 PM, mamush bukana mamushbuk...@gmail.com wrote:
 Hi Jeff,
 From your words (if our words really describe us), I hope you don't expect
 me to teach you that this is r-help room. I don't expect you to tell me
 that I am a layman. I already know it and that is why I am here seeking a
 help. If I am an expert of things I asked here, there is no need for me to
 come here with such stupid (in your understanding) question. I know there
 are people on this planet who consider themselves advanced only when they
 meet stupid people like me - you may be one of them. It is always helpful
 for others if you could write a single line with a helping mind than
 writing tones of useless junk words. R is applied statistical language, to
 be used in different professions. So, don't expect all R users to be
 experts like you are. If you can't help people, at least keep yourself
 away from helping environment.

This isn't just R-help.  You've also cross-posted (which many feel is
rude) to R-SIG-Finance, even though this is off-topic for
R-SIG-Finance.  Your problem is with R's control-flow and
error-handling, not anything finance-related.  Also, these being
help forums and you being a laymen does not mean you can ignore the
guidelines.

Rather than (again) not following the suggestions in the posting guide
(If you feel insulted by some response to a post of yours, don't make
any hasty response in return - you're more likely than not to regret
it.), you would be wise to follow Jeff's advice:
1) Stop posting in HTML, so people can copy/paste your code.
2) Provide a small, self-contained reproducible example.
3) Carefully read ?try and ?tryCatch; use rseek.org to search for examples.
4) Skip 1-3 and find some one-on-one help in person.

It would also be wise to follow *both* of Pat's recommendations (you
ignored his suggestion to save some information from the ca.jo
calculations).

In short, please follow the posing guide, rather than expect people to
voluntarily help you on your terms.

 Cheers

 Bukana


 On Tue, Dec 24, 2013 at 5:23 PM, Jeff Newmiller 
 jdnew...@dcn.davis.ca.uswrote:

 Once you start describing your code in language that indicates you have
 read some of the recommended materials, we can make some progress. For
 example, you are still posting in HTML format so your code is getting
 mangled (see Posting Guide). You don't seem to understand what tryCatch
 does (see ?tryCatch; hint... it does not alter the result returned by
 ca.jo, nor does it guarantee that any result will be returned). For that
 matter, you don't seem to understand how to give valid inputs to ca.jo,
 but then you don't seem to understand how to provide a reproducible example
 either (see for example
 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
 ).

 In short, if you ask us why one divided by zero doesn't give 3, we have to
 wonder if you don't belong in some other educational forum, because this is
 not a mathematics theory support group... it is about R. Please read or
 otherwise absorb the recommended background materials mentioned here and
 then ask clear questions here about R, or find some one-on-one help offline.
 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live
 Go...
   Live:   OO#.. Dead: OO#..  Playing
 Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
 /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
 ---
 Sent from my phone. Please excuse my brevity.

 mamush bukana mamushbuk...@gmail.com wrote:
 Dear Pat,
 I tried your suggestion:
 for(i in 1:N1){
 for(j in 1:N2){
 co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
 i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
 NaN)
 }}
 
 and the earlier error does not show up. However, when I try to extract
 some
 results from the co.jo function, it does not work the way it does
 without
 the tryCatch thing and there appears another error. For example:
 
 for(i in 1:N1){
 for(j in 1:N2){
 co-tryCatch(ca.jo http://ca.jo(data.frame(cbind(y2[
 i,j,],y1[i,j,])),type=trace, K=2,
 spec=transitory,ecdet=const,season=NULL,dumvar=NULL),error=function(e)
 NaN)
 
 cor1-cajorls(co,r=1)
 }}
 
 Error in cajorls(vecm, r = 1) :
 Please, provide object of class 'ca.jo' or 'cajo.test' as 'z'.
 
 So it seems tryCatch is disabling ca.jo from running properly. My
 intention is to run this function (ca.jo) and extract some estimates
 for
 further computation.
 
 regards
 
 Bukana
 
 
 
 
 
 
 
 On Mon, Dec 23, 2013 at 5:09 PM, Patrick Burns
 pbu...@pburns.seanet.comwrote:
 
  There is a fundamental problem with your
  code, and there is the problem that you
  have 

[R] label in scatter3d plot

2013-12-24 Thread eliza botto
Dear Users of R,
I plotted the following data by
scatterplot3d(x,y,z, main=3D Scatterplot)
Then i wanted to label the points on that plot w.r.t column 4. i unsuccessfully 
tried
textxy()  text3d() 
Kindly guide me through
 dput(test)
structure(list(x = c(458750L, 460350L, 415750L, 356250L, 387450L, 412350L, 
411950L, 436750L, 428350L, 508450L, 437450L, 432550L, 433650L, 430050L, 
457150L, 445350L, 444350L, 389150L, 421050L, 413450L, 420050L, 433850L, 
421750L, 380850L, 337050L, 348550L, 399350L, 405750L, 406050L, 407550L, 
507950L, 358150L, 374950L, 380350L, 319450L, 444150L, 329950L, 335150L, 
330750L, 343350L, 401650L, 398550L, 423150L, 456550L, 457350L, 402150L, 
367150L, 360050L, 408750L, 350650L, 360850L, 394850L, 388950L, 437950L, 
418450L, 398850L, 476650L, 476450L, 469250L, 376650L, 382450L, 351150L, 
351850L, 394750L, 393250L, 366550L, 377250L, 350450L, 401850L, 372950L, 
371650L, 373250L, 390450L, 421450L, 341750L, 373750L, 364750L, 359650L, 
499150L, 502950L, 366950L, 340750L, 429150L, 416950L, 436650L, 466850L, 
365150L, 371650L, 385450L, 430850L, 450150L, 431350L, 392750L, 398550L, 
407850L, 406650L, 402350L, 424850L, 411850L, 419450L, 401450L, 467750L, 
439950L, 436550L, 362150L, 352950L, 385850L, 38395!
 0L, 503650L), y = c(5062550L, 5053950L, 5090250L, 5076750L, 5052050L, 
4981450L, 4978150L, 4947050L, 4931550L, 4946050L, 5109250L, 4904950L, 4917350L, 
4898150L, 4934550L, 4926550L, 4918450L, 4909550L, 4959850L, 4900350L, 5058650L, 
5044050L, 500L, 4976850L, 4982050L, 4980550L, 5039050L, 4895750L, 4897450L, 
4900950L, 4959150L, 5064350L, 5065150L, 5060150L, 4995650L, 5129050L, 4977450L, 
4993350L, 4992050L, 4994150L, 4904450L, 4896150L, 5041450L, 4927550L, 4922150L, 
5080550L, 4953650L, 4917850L, 5078650L, 4927150L, 4926250L, 5015450L, 5020650L, 
5081750L, 4907150L, 4888650L, 4938950L, 4940850L, 4924850L, 5036150L, 5033350L, 
4962950L, 4973850L, 4909750L, 4897050L, 4948750L, 5003150L, 4950750L, 4983450L, 
4954850L, 4978750L, 4956750L, 5010350L, 4931050L, 5059450L, 4988350L, 4988550L, 
5042650L, 4949950L, 4941650L, 4906150L, 4913250L, 5075850L, 5076450L, 5052550L, 
5094350L, 5016450L, 5012750L, 5041250L, 5060850L, 5079250L, 5054150L, 4911050L, 
4921450L, 4905250L, 4888050L, 4926650L!
 , 4932650L, 4892350L, 4893850L, 4886350L, 5051150L, 5110850L, 4928850L
, 4940150L, 4939350L, 4892550L, 4895250L, 4939050L), z = c(1167L, 1167L, 4572L, 
3179L, 3141L, 585L, 585L, 876L, 876L, 1678L, 2667L, 1369L, 1369L, 1369L, 1381L, 
1381L, 1381L, 2284L, 410L, 2109L, 2507L, 2579L, 2507L, 1436L, 3234L, 3234L, 
2792L, 2569L, 2569L, 2569L, 1669L, 4743L, 4743L, 4743L, 3403L, 3197L, 3267L, 
3583L, 3583L, 3583L, 2584L, 2584L, 2579L, 1241L, 1241L, 4174L, 2366L, 2618L, 
4487L, 3196L, 3196L, 2107L, 2107L, 2427L, 1814L, 2622L, 1268L, 1268L, 1268L, 
3885L, 3885L, 3092L, 3234L, 2625L, 2625L, 3760L, 4743L, 3707L, 4743L, 3760L, 
3885L, 3760L, 4743L, 782L, 3343L, 2697L, 2697L, 3915L, 1678L, 1678L, 3197L, 
2957L, 4530L, 4530L, 4530L, 2131L, 3618L, 3618L, 3335L, 2512L, 2390L, 1616L, 
3197L, 3197L, 2625L, 2622L, 3197L, 3197L, 2622L, 2622L, 2622L, 368L, 4572L, 
863L, 3716L, 3716L, 2697L, 2697L, 1358L), V4 = 1:109), .Names = c(x, y, 
z, V4), row.names = c(NA, -109L), class = data.frame)


Thanks in advance,
Eliza 
[[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] label in scatter3d plot

2013-12-24 Thread eliza botto
Dear ligges,
Thanks for your reply. it worked out.
Happy Christmas,
Eliza

 Date: Wed, 25 Dec 2013 01:09:52 +0100
 From: lig...@statistik.tu-dortmund.de
 To: eliza_bo...@hotmail.com; r-help@r-project.org
 Subject: Re: [R] label in scatter3d plot
 
 See what scatterplot3d() return. This is rather helpful, i.e. try
 
 s3d - scatterplot3d(x,y,z, main=3D Scatterplot)
 text(s3d$xyz.convert(x,y,z)$x, s3d$xyz.convert(x,y,z)$y, V4, pos=4, cex=0.7)
 
 Well, you still don't see too much, but at least it does what you were 
 asking for.
 
 Best,
 Uwe Ligges
 
 
 
 On 25.12.2013 00:26, eliza botto wrote:
  Dear Users of R,
  I plotted the following data by
  scatterplot3d(x,y,z, main=3D Scatterplot)
  Then i wanted to label the points on that plot w.r.t column 4. i 
  unsuccessfully tried
  textxy()  text3d()
  Kindly guide me through
  dput(test)
  structure(list(x = c(458750L, 460350L, 415750L, 356250L, 387450L, 412350L, 
  411950L, 436750L, 428350L, 508450L, 437450L, 432550L, 433650L, 430050L, 
  457150L, 445350L, 444350L, 389150L, 421050L, 413450L, 420050L, 433850L, 
  421750L, 380850L, 337050L, 348550L, 399350L, 405750L, 406050L, 407550L, 
  507950L, 358150L, 374950L, 380350L, 319450L, 444150L, 329950L, 335150L, 
  330750L, 343350L, 401650L, 398550L, 423150L, 456550L, 457350L, 402150L, 
  367150L, 360050L, 408750L, 350650L, 360850L, 394850L, 388950L, 437950L, 
  418450L, 398850L, 476650L, 476450L, 469250L, 376650L, 382450L, 351150L, 
  351850L, 394750L, 393250L, 366550L, 377250L, 350450L, 401850L, 372950L, 
  371650L, 373250L, 390450L, 421450L, 341750L, 373750L, 364750L, 359650L, 
  499150L, 502950L, 366950L, 340750L, 429150L, 416950L, 436650L, 466850L, 
  365150L, 371650L, 385450L, 430850L, 450150L, 431350L, 392750L, 398550L, 
  407850L, 406650L, 402350L, 424850L, 411850L, 419450L, 401450L, 467750L, 
  439950L, 436550L, 362150L, 352950L, 385850L, 3!
 8395!
0L, 503650L), y = c(5062550L, 5053950L, 5090250L, 5076750L, 5052050L, 
  4981450L, 4978150L, 4947050L, 4931550L, 4946050L, 5109250L, 4904950L, 
  4917350L, 4898150L, 4934550L, 4926550L, 4918450L, 4909550L, 4959850L, 
  4900350L, 5058650L, 5044050L, 500L, 4976850L, 4982050L, 4980550L, 
  5039050L, 4895750L, 4897450L, 4900950L, 4959150L, 5064350L, 5065150L, 
  5060150L, 4995650L, 5129050L, 4977450L, 4993350L, 4992050L, 4994150L, 
  4904450L, 4896150L, 5041450L, 4927550L, 4922150L, 5080550L, 4953650L, 
  4917850L, 5078650L, 4927150L, 4926250L, 5015450L, 5020650L, 5081750L, 
  4907150L, 4888650L, 4938950L, 4940850L, 4924850L, 5036150L, 5033350L, 
  4962950L, 4973850L, 4909750L, 4897050L, 4948750L, 5003150L, 4950750L, 
  4983450L, 4954850L, 4978750L, 4956750L, 5010350L, 4931050L, 5059450L, 
  4988350L, 4988550L, 5042650L, 4949950L, 4941650L, 4906150L, 4913250L, 
  5075850L, 5076450L, 5052550L, 5094350L, 5016450L, 5012750L, 5041250L, 
  5060850L, 5079250L, 5054150L, 4911050L, 4921450L, 4905250L, 4888050L, 492!
 6650L!
, 4932650L, 4892350L, 4893850L, 4886350L, 5051150L, 5110850L, 4928850L
  , 4940150L, 4939350L, 4892550L, 4895250L, 4939050L), z = c(1167L, 1167L, 
  4572L, 3179L, 3141L, 585L, 585L, 876L, 876L, 1678L, 2667L, 1369L, 1369L, 
  1369L, 1381L, 1381L, 1381L, 2284L, 410L, 2109L, 2507L, 2579L, 2507L, 1436L, 
  3234L, 3234L, 2792L, 2569L, 2569L, 2569L, 1669L, 4743L, 4743L, 4743L, 
  3403L, 3197L, 3267L, 3583L, 3583L, 3583L, 2584L, 2584L, 2579L, 1241L, 
  1241L, 4174L, 2366L, 2618L, 4487L, 3196L, 3196L, 2107L, 2107L, 2427L, 
  1814L, 2622L, 1268L, 1268L, 1268L, 3885L, 3885L, 3092L, 3234L, 2625L, 
  2625L, 3760L, 4743L, 3707L, 4743L, 3760L, 3885L, 3760L, 4743L, 782L, 3343L, 
  2697L, 2697L, 3915L, 1678L, 1678L, 3197L, 2957L, 4530L, 4530L, 4530L, 
  2131L, 3618L, 3618L, 3335L, 2512L, 2390L, 1616L, 3197L, 3197L, 2625L, 
  2622L, 3197L, 3197L, 2622L, 2622L, 2622L, 368L, 4572L, 863L, 3716L, 3716L, 
  2697L, 2697L, 1358L), V4 = 1:109), .Names = c(x, y, z, V4), 
  row.names = c(NA, -109L), class = data.frame)
 
 
  Thanks in advance,
  Eliza   
  [[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] Simplifying an expression with an integral

2013-12-24 Thread David Winsemius

On Dec 24, 2013, at 9:39 AM, Aurélien Philippot wrote:

 Dear R experts,
 I am computing the following integral.
 
 [image: \int_{1,100} \frac{1}{x+Max(x-50,0)} g(x)dx], where g is the
 density of the standard normal, and [1,100] is the domain.
 
 1) I use the following code which works fine:
 integrand1- function(x){1/x*dnorm(x)}
 integrand2- function(x){1/(2*x-50)*dnorm(x)}
 
 res1- integrate(integrand1, lower=1, upper=50)$value+
 integrate(integrand2, lower=50, upper=100)$value
 res1
 0.1116
 
 In other words, I split the max function depending on the value of x in the
 domain.
 
 2) Alternatively, I can also compute it by vectorizing the max function
 integrand- function (x) ifelse(x50, 1/x*dnorm(x) , 1/(2*x-50)*dnorm(x))
 res4- integrate(integrand, lower=1, upper=100)$value
 res4
 0.1116
 
 3) However, in both cases, the syntax is a little bit heavy and not very
 convenient if I want to add more integrals, all of them with a max in the
 integrand. Is there a way to have a more concise syntax?
 Using max or pmax directly in the definition of the integrand does not work.
 For example:
 integrand- function(x) {1/(x+max(x-50,0)*dnorm(x))}

Is boolean math more to your liking?

integr_both- function(x){ 
   (x  50)*(1/x*dnorm(x))+
   (x= 50  x100)*(  1/(2*x-50)*dnorm(x) ) }
res_b- integrate(integr_both, lower=1, upper=100)$value

 res_b
[1] 0.1116587

-- 
David

 
 res- integrate(integrand, lower=1, upper=100)$value
 res
 4.60517
 
 Thank you for any suggestion, and merry Christmas!
 
   [[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.

David Winsemius
Alameda, CA, USA

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