Re: [R] delta and sd parameters for power.t.test

2017-12-14 Thread David Winsemius

> On Dec 14, 2017, at 2:26 PM, Lasse Kliemann  wrote:
> 
> What is the rationale behind having both the delta and sd parameters for
> the power.t.test function?

One is the standard deviation of the hypothesized data (or pooled sd in the 
case of two sample) under the "alternative" and one is the mean of that data 
(or equivalently the differences if this is a two-sample test).

> For the relevant noncentrality parameter, we
> only need the ratio delta/sd. If my effect size is given as Cohen's d,
> then I only got that ratio and not sd.
> 

If the data has been "standardized", then Cohen's d could be given to the 
function as the value for delta since the sd default is 1.


> As far as I see, in such a case, I can specify delta=d and leave sd at
> its default value 1. Is this correct or am I missing something?
> 

Actually it sounds as though we are missing something. The power.t.test 
function makes no mention of Cohen's d or effect size. Is this question in 
response to a homework assignment about which we have not been informed?

Further questions should have some actual R code to make this an on-topic 
discussion for Rhelp.


> Thanks.
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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

'Any technology distinguishable from magic is insufficiently advanced.'   
-Gehm's Corollary to Clarke's Third Law

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] delta and sd parameters for power.t.test

2017-12-14 Thread Lasse Kliemann
What is the rationale behind having both the delta and sd parameters for
the power.t.test function? For the relevant noncentrality parameter, we
only need the ratio delta/sd. If my effect size is given as Cohen's d,
then I only got that ratio and not sd.

As far as I see, in such a case, I can specify delta=d and leave sd at
its default value 1. Is this correct or am I missing something?

Thanks.


signature.asc
Description: PGP signature
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] GAM Poisson

2017-12-14 Thread Miluji Sb
Dear all,

I apologize as this may not be a strictly R question. I am running GAM
models using the mgcv package.

I was wondering if the interpretation of the smooth splines of the 'x'
variable is the same in the following two cases:

# Linear probability model
m1 <- gam(count ~ factor(city) + factor(year) + s(x),
data=data,na.action=na.omit)

# Poisson
m2 <- gam(count ~ factor(city) + factor(year) + s(x),data=data,
family=poisson,na.action=na.omit)

Thank you!

Sincerely,

Milu

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Errors in reading in txt files

2017-12-14 Thread MacQueen, Don
In addition to which, I would recommend

df <- read.table("DATAM", header = TRUE, fill = TRUE, stringsAsFactors=FALSE)

and then converting the Time column to POSIXct date-time values using
  as.POSIXct()
specifying the format using formatting codes found in
  ?strptime
because the times are not in the POSIXct default format.


This example might indicate the idea:

> as.POSIXct('2012-10-12 13:14')
[1] "2012-10-12 13:14:00 PDT"
> class(as.POSIXct('2012-10-12 13:14'))
[1] "POSIXct" "POSIXt" 

-Don

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 12/14/17, 11:01 AM, "R-help on behalf of Ista Zahn" 
 wrote:

On Thu, Dec 14, 2017 at 1:58 PM, Berend Hasselman  wrote:
>
>> On 14 Dec 2017, at 19:36, lily li  wrote:
>>
>> Hi R users,
>>
>> I have a question about reading from text files. The file has the 
structure
>> below:
>>
>> TimeColumn1   Column2
>> 01.01.2001-12:00:00
>
> This line does not contain 3 elements; only one.
> You'll have to fix that line. Delete it, prepend it with a comment 
character of add enough columns.

I definitely don't recommend that. Instead, read

?read.table

to learn about the "fill" and "header" arguments.

df = read.table("DATAM", header = TRUE, fill = TRUE)

will probably work.

Best,
Ista


>
>
> Berend
>
>> 01.01.2001-24:00:0012 11
>> 01.02.2001-12:00:0013 10
>> 01.02.2001-24:00:0011 12
>> 01.03.2001-12:00:0015 11
>> 01.03.2001-24:00:0016 10
>> ...
>>
>> I just use the simple script to open it: df = read.table('DATAM', 
head=T).
>>
>> But it has the error and thus cannot read the file:
>> Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
>> dec,  :
>>  line 1 did not have 3 elements
>>
>> How to read it with three fixed columns, and how to read the time format 
in
>> the first column correctly? Thanks for your help.
>>
>>   [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] Errors in reading in txt files

2017-12-14 Thread Berend Hasselman

> On 14 Dec 2017, at 20:01, Ista Zahn  wrote:
> 
> On Thu, Dec 14, 2017 at 1:58 PM, Berend Hasselman  wrote:
>> 
>>> On 14 Dec 2017, at 19:36, lily li  wrote:
>>> 
>>> Hi R users,
>>> 
>>> I have a question about reading from text files. The file has the structure
>>> below:
>>> 
>>> TimeColumn1   Column2
>>> 01.01.2001-12:00:00
>> 
>> This line does not contain 3 elements; only one.
>> You'll have to fix that line. Delete it, prepend it with a comment character 
>> of add enough columns.
> 
> I definitely don't recommend that. Instead, read
> 
> ?read.table
> 
> to learn about the "fill" and "header" arguments.
> 
> df = read.table("DATAM", header = TRUE, fill = TRUE)
> 
> will probably work.
> 

Yes. I agree. It's much better.
I should have experimented some more.

Berend

> Best,
> Ista
> 
> 
>> 
>> 
>> Berend
>> 
>>> 01.01.2001-24:00:0012 11
>>> 01.02.2001-12:00:0013 10
>>> 01.02.2001-24:00:0011 12
>>> 01.03.2001-12:00:0015 11
>>> 01.03.2001-24:00:0016 10
>>> ...
>>> 
>>> I just use the simple script to open it: df = read.table('DATAM', head=T).
>>> 
>>> But it has the error and thus cannot read the file:
>>> Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
>>> dec,  :
>>> line 1 did not have 3 elements
>>> 
>>> How to read it with three fixed columns, and how to read the time format in
>>> the first column correctly? Thanks for your help.
>>> 
>>>  [[alternative HTML version deleted]]
>>> 
>>> __
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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 -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
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] Errors in reading in txt files

2017-12-14 Thread Ista Zahn
On Thu, Dec 14, 2017 at 1:58 PM, Berend Hasselman  wrote:
>
>> On 14 Dec 2017, at 19:36, lily li  wrote:
>>
>> Hi R users,
>>
>> I have a question about reading from text files. The file has the structure
>> below:
>>
>> TimeColumn1   Column2
>> 01.01.2001-12:00:00
>
> This line does not contain 3 elements; only one.
> You'll have to fix that line. Delete it, prepend it with a comment character 
> of add enough columns.

I definitely don't recommend that. Instead, read

?read.table

to learn about the "fill" and "header" arguments.

df = read.table("DATAM", header = TRUE, fill = TRUE)

will probably work.

Best,
Ista


>
>
> Berend
>
>> 01.01.2001-24:00:0012 11
>> 01.02.2001-12:00:0013 10
>> 01.02.2001-24:00:0011 12
>> 01.03.2001-12:00:0015 11
>> 01.03.2001-24:00:0016 10
>> ...
>>
>> I just use the simple script to open it: df = read.table('DATAM', head=T).
>>
>> But it has the error and thus cannot read the file:
>> Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
>> dec,  :
>>  line 1 did not have 3 elements
>>
>> How to read it with three fixed columns, and how to read the time format in
>> the first column correctly? Thanks for your help.
>>
>>   [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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 -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] Errors in reading in txt files

2017-12-14 Thread lily li
Thanks, Berend. I thought R can recognize the space automatically, such as
na.strings="", or sep=' '.

On Thu, Dec 14, 2017 at 11:58 AM, Berend Hasselman  wrote:

>
> > On 14 Dec 2017, at 19:36, lily li  wrote:
> >
> > Hi R users,
> >
> > I have a question about reading from text files. The file has the
> structure
> > below:
> >
> > TimeColumn1   Column2
> > 01.01.2001-12:00:00
>
> This line does not contain 3 elements; only one.
> You'll have to fix that line. Delete it, prepend it with a comment
> character of add enough columns.
>
>
> Berend
>
> > 01.01.2001-24:00:0012 11
> > 01.02.2001-12:00:0013 10
> > 01.02.2001-24:00:0011 12
> > 01.03.2001-12:00:0015 11
> > 01.03.2001-24:00:0016 10
> > ...
> >
> > I just use the simple script to open it: df = read.table('DATAM',
> head=T).
> >
> > But it has the error and thus cannot read the file:
> > Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
> > dec,  :
> >  line 1 did not have 3 elements
> >
> > How to read it with three fixed columns, and how to read the time format
> in
> > the first column correctly? Thanks for your help.
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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 -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] help with recursive function

2017-12-14 Thread William Dunlap via R-help
recursive_funlp <- function(dataset = dat1, func = funlp2) {
...
if (!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
   else {
df2 <- recursive_funlp() # GIVE SOME ARGUMENTS HERE
return(df2)
}
}

When you recurse into recursve_funip(), give it at least a new dataset
argument.
Otherwise it just processes the entire dat1 again and the recursion never
stops.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] <
nilesh.di...@monsanto.com> wrote:

> Eric:  I will try and see if I can figure out the issue by debugging as
> you suggested. I don’t know why my code after stopifnot is not getting
> executed where I like the code to run the funlp2 function when the if
> statement is TRUE but when it is false, I like it to keep running until the
> stopifnot condition is met.
>
> When the stopifnot condition is met, I like to get the output from if
> statement saved.
> Anyway,  I will keep trying.
> Again, Thanks for your help!
> Nilesh
>
> From: Eric Berger [mailto:ericjber...@gmail.com]
> Sent: Thursday, December 14, 2017 10:29 AM
> To: DIGHE, NILESH [AG/2362] 
> Cc: r-help 
> Subject: Re: [R] help with recursive function
>
> If you are trying to understand why the "stopifnot" condition is met you
> can replace it by something like:
>
> if ( any(dat2$norm_sd >= 1) )
>browser()
>
> This will put you in a debugging session where you can examine your
> variables, e.g.
>
> > dat$norm_sd
>
> HTH,
> Eric
>
>
>
> On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger  > wrote:
> The message is coming from your stopifnot() condition being met.
>
>
> On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <
> nilesh.di...@monsanto.com> wrote:
> Hi, I accidently left out few lines of code from the calclp function.
> Updated function is pasted below.
> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is
> not TRUE“
>
> I would appreciate any help.
> Nilesh
> dput(calclp)
> function (dataset)
> {
> dat1 <- funlp1(dataset)
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
> names(.)), funs(norm = round(scale(.), 3)))
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, sd, na.rm = TRUE), 3)
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, function(x) {
> max(abs(x), na.rm = TRUE)
> }), 3)
> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
> "\\.") %>% spread(treatment, value) %>%
> mutate(outlier = NA)
> stopifnot(!(any(data1$norm_sd >= 1)))
> if (!(any(data1$norm_sd >= 1))) {
> df1 <- dat1
> return(df1)
> }
>else {
> df2 <- recursive_funlp()
> return(df2)
> }
> }
> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
> df3
> }
>
>
> From: DIGHE, NILESH [AG/2362]
> Sent: Thursday, December 14, 2017 9:01 AM
> To: 'Eric Berger' >
> Cc: r-help >
> Subject: RE: [R] help with recursive function
>
> Eric:  Thanks for taking time to look into my problem.  Despite of making
> the change you suggested, I am still getting the same error.  I am
> wondering if the logic I am using in the stopifnot and if functions is a
> problem.
> I like the recursive function to stop whenever the norm_sd column has zero
> values that are above or equal to 1. Below is the calclp function after the
> changes you suggested.
> Thanks. Nilesh
>
> dput(calclp)
> function (dataset)
> {
> dat1 <- funlp1(dataset)
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
> names(.)), funs(norm = round(scale(.), 3)))
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, sd, na.rm = TRUE), 3)
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, function(x) {
> max(abs(x), na.rm = TRUE)
> }), 3)
> stopifnot(!(any(dat2$norm_sd >= 1)))
> if (!(any(dat2$norm_sd >= 1))) {
> df1 <- dat1
> return(df1)
> }
> else {

Re: [R] Errors in reading in txt files

2017-12-14 Thread Berend Hasselman

> On 14 Dec 2017, at 19:36, lily li  wrote:
> 
> Hi R users,
> 
> I have a question about reading from text files. The file has the structure
> below:
> 
> TimeColumn1   Column2
> 01.01.2001-12:00:00

This line does not contain 3 elements; only one.
You'll have to fix that line. Delete it, prepend it with a comment character of 
add enough columns.


Berend

> 01.01.2001-24:00:0012 11
> 01.02.2001-12:00:0013 10
> 01.02.2001-24:00:0011 12
> 01.03.2001-12:00:0015 11
> 01.03.2001-24:00:0016 10
> ...
> 
> I just use the simple script to open it: df = read.table('DATAM', head=T).
> 
> But it has the error and thus cannot read the file:
> Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
> dec,  :
>  line 1 did not have 3 elements
> 
> How to read it with three fixed columns, and how to read the time format in
> the first column correctly? Thanks for your help.
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] Errors in reading in txt files

2017-12-14 Thread lily li
Hi R users,

I have a question about reading from text files. The file has the structure
below:

TimeColumn1   Column2
01.01.2001-12:00:00
01.01.2001-24:00:0012 11
01.02.2001-12:00:0013 10
01.02.2001-24:00:0011 12
01.03.2001-12:00:0015 11
01.03.2001-24:00:0016 10
...

I just use the simple script to open it: df = read.table('DATAM', head=T).

But it has the error and thus cannot read the file:
Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
dec,  :
  line 1 did not have 3 elements

How to read it with three fixed columns, and how to read the time format in
the first column correctly? Thanks for your help.

[[alternative HTML version deleted]]

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


Re: [R] help with recursive function

2017-12-14 Thread DIGHE, NILESH [AG/2362]
When I run the code without stopifnot, the code takes 5 min to run and then it 
throws an error listed below without producing any results.
Error: node stack overflow
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Error during wrapup: node stack overflow

Thanks.
Nilesh

From: William Dunlap [mailto:wdun...@tibco.com]
Sent: Thursday, December 14, 2017 11:26 AM
To: DIGHE, NILESH [AG/2362] 
Cc: Eric Berger ; r-help 
Subject: Re: [R] help with recursive function

Your code contains the lines
stopifnot(!(any(data1$norm_sd >= 1)))
if(!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}

stop() "throws an error", causing the current function and all functions in the 
call
stack to abort and return nothing.  It does not mean to stop now and return a 
result.
Does the function give the correct results if you just leave out the stopifnot 
line?


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] 
> wrote:
Eric:  I will try and see if I can figure out the issue by debugging as you 
suggested. I don’t know why my code after stopifnot is not getting executed 
where I like the code to run the funlp2 function when the if statement is TRUE 
but when it is false, I like it to keep running until the stopifnot condition 
is met.

When the stopifnot condition is met, I like to get the output from if statement 
saved.
Anyway,  I will keep trying.
Again, Thanks for your help!
Nilesh

From: Eric Berger [mailto:ericjber...@gmail.com]
Sent: Thursday, December 14, 2017 10:29 AM
To: DIGHE, NILESH [AG/2362] 
>
Cc: r-help >
Subject: Re: [R] help with recursive function

If you are trying to understand why the "stopifnot" condition is met you can 
replace it by something like:

if ( any(dat2$norm_sd >= 1) )
   browser()

This will put you in a debugging session where you can examine your variables, 
e.g.

> dat$norm_sd

HTH,
Eric



On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger 
>>
 wrote:
The message is coming from your stopifnot() condition being met.


On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] 
>>
 wrote:
Hi, I accidently left out few lines of code from the calclp function.  Updated 
function is pasted below.
I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not 
TRUE“

I would appreciate any help.
Nilesh
dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
-norm_sd) %>% separate(key, c("field_rep", "treatment"),
"\\.") %>% spread(treatment, 
value) %>% mutate(outlier = NA)
stopifnot(!(any(data1$norm_sd >= 1)))
if (!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
   else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: DIGHE, NILESH [AG/2362]
Sent: Thursday, December 14, 2017 9:01 AM
To: 'Eric Berger' 
>>
Cc: r-help 
>>
Subject: RE: [R] help with recursive function

Eric:  Thanks for taking time to look into my problem.  Despite of making the 
change you suggested, I am still getting the same error.  I am wondering if the 
logic I am using in the stopifnot and if functions is a problem.
I like the recursive function to stop whenever the norm_sd column has zero 
values that are above or equal to 1. Below is the calclp function after the 
changes you suggested.
Thanks. Nilesh

dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- 

Re: [R] help with recursive function

2017-12-14 Thread William Dunlap via R-help
Your code contains the lines
stopifnot(!(any(data1$norm_sd >= 1)))
if(!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}

stop() "throws an error", causing the current function and all functions in
the call
stack to abort and return nothing.  It does not mean to stop now and return
a result.
Does the function give the correct results if you just leave out the
stopifnot line?


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Dec 14, 2017 at 9:11 AM, DIGHE, NILESH [AG/2362] <
nilesh.di...@monsanto.com> wrote:

> Eric:  I will try and see if I can figure out the issue by debugging as
> you suggested. I don’t know why my code after stopifnot is not getting
> executed where I like the code to run the funlp2 function when the if
> statement is TRUE but when it is false, I like it to keep running until the
> stopifnot condition is met.
>
> When the stopifnot condition is met, I like to get the output from if
> statement saved.
> Anyway,  I will keep trying.
> Again, Thanks for your help!
> Nilesh
>
> From: Eric Berger [mailto:ericjber...@gmail.com]
> Sent: Thursday, December 14, 2017 10:29 AM
> To: DIGHE, NILESH [AG/2362] 
> Cc: r-help 
> Subject: Re: [R] help with recursive function
>
> If you are trying to understand why the "stopifnot" condition is met you
> can replace it by something like:
>
> if ( any(dat2$norm_sd >= 1) )
>browser()
>
> This will put you in a debugging session where you can examine your
> variables, e.g.
>
> > dat$norm_sd
>
> HTH,
> Eric
>
>
>
> On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger  > wrote:
> The message is coming from your stopifnot() condition being met.
>
>
> On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <
> nilesh.di...@monsanto.com> wrote:
> Hi, I accidently left out few lines of code from the calclp function.
> Updated function is pasted below.
> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is
> not TRUE“
>
> I would appreciate any help.
> Nilesh
> dput(calclp)
> function (dataset)
> {
> dat1 <- funlp1(dataset)
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
> names(.)), funs(norm = round(scale(.), 3)))
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, sd, na.rm = TRUE), 3)
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, function(x) {
> max(abs(x), na.rm = TRUE)
> }), 3)
> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
> "\\.") %>% spread(treatment, value) %>%
> mutate(outlier = NA)
> stopifnot(!(any(data1$norm_sd >= 1)))
> if (!(any(data1$norm_sd >= 1))) {
> df1 <- dat1
> return(df1)
> }
>else {
> df2 <- recursive_funlp()
> return(df2)
> }
> }
> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
> df3
> }
>
>
> From: DIGHE, NILESH [AG/2362]
> Sent: Thursday, December 14, 2017 9:01 AM
> To: 'Eric Berger' >
> Cc: r-help >
> Subject: RE: [R] help with recursive function
>
> Eric:  Thanks for taking time to look into my problem.  Despite of making
> the change you suggested, I am still getting the same error.  I am
> wondering if the logic I am using in the stopifnot and if functions is a
> problem.
> I like the recursive function to stop whenever the norm_sd column has zero
> values that are above or equal to 1. Below is the calclp function after the
> changes you suggested.
> Thanks. Nilesh
>
> dput(calclp)
> function (dataset)
> {
> dat1 <- funlp1(dataset)
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
> names(.)), funs(norm = round(scale(.), 3)))
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, sd, na.rm = TRUE), 3)
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
> 1, function(x) {
> max(abs(x), na.rm = TRUE)
> }), 3)
> stopifnot(!(any(dat2$norm_sd >= 1)))
> if (!(any(dat2$norm_sd >= 1))) {
> df1 <- dat1
> return(df1)
> }
> else {
> 

[R] permutation test for Cox proportional hazards regression model

2017-12-14 Thread linda porz
I would like to perform a permutation test for Cox proportional hazards
regression model. I only find it for t-test and other tests (e.g. comparing
two medians).

Is there a way that I can perform a Cox PH model in R or SAS for the
LR-test?

I am doing the following

B <- 1000; LRtestx <- rep(NA,B);

Srv <- Surv(Time, Event);

for(j in 1:B){ LRtestx[j] <- cph(Srv~sample(x,length(x),replace=F))$stat[3]};

LRtest <- cph(Srv~x)$stat[3];

sum(LRtestx > LRtest)/B

Many thanks

Linda

[[alternative HTML version deleted]]

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


Re: [R] help with recursive function

2017-12-14 Thread DIGHE, NILESH [AG/2362]
Eric:  I will try and see if I can figure out the issue by debugging as you 
suggested. I don’t know why my code after stopifnot is not getting executed 
where I like the code to run the funlp2 function when the if statement is TRUE 
but when it is false, I like it to keep running until the stopifnot condition 
is met.

When the stopifnot condition is met, I like to get the output from if statement 
saved.
Anyway,  I will keep trying.
Again, Thanks for your help!
Nilesh

From: Eric Berger [mailto:ericjber...@gmail.com]
Sent: Thursday, December 14, 2017 10:29 AM
To: DIGHE, NILESH [AG/2362] 
Cc: r-help 
Subject: Re: [R] help with recursive function

If you are trying to understand why the "stopifnot" condition is met you can 
replace it by something like:

if ( any(dat2$norm_sd >= 1) )
   browser()

This will put you in a debugging session where you can examine your variables, 
e.g.

> dat$norm_sd

HTH,
Eric



On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger 
> wrote:
The message is coming from your stopifnot() condition being met.


On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] 
> wrote:
Hi, I accidently left out few lines of code from the calclp function.  Updated 
function is pasted below.
I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not 
TRUE“

I would appreciate any help.
Nilesh
dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
-norm_sd) %>% separate(key, c("field_rep", "treatment"),
"\\.") %>% spread(treatment, value) %>% mutate(outlier = 
NA)
stopifnot(!(any(data1$norm_sd >= 1)))
if (!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
   else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: DIGHE, NILESH [AG/2362]
Sent: Thursday, December 14, 2017 9:01 AM
To: 'Eric Berger' >
Cc: r-help >
Subject: RE: [R] help with recursive function

Eric:  Thanks for taking time to look into my problem.  Despite of making the 
change you suggested, I am still getting the same error.  I am wondering if the 
logic I am using in the stopifnot and if functions is a problem.
I like the recursive function to stop whenever the norm_sd column has zero 
values that are above or equal to 1. Below is the calclp function after the 
changes you suggested.
Thanks. Nilesh

dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
stopifnot(!(any(dat2$norm_sd >= 1)))
if (!(any(dat2$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: Eric Berger [mailto:ericjber...@gmail.com]
Sent: Thursday, December 14, 2017 8:17 AM
To: DIGHE, NILESH [AG/2362] 
>
Cc: r-help >
Subject: Re: [R] help with recursive function

My own typo ... whoops ...

!( any(dat2$norm_sd >= 1 ))



On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger 
> wrote:
You seem to have a typo at this expression (and some others like it)

Namely, you write

any(!dat2$norm_sd) >= 1

when you possibly meant to write

!( any(dat2$norm_sd) >= 1 )

i.e. I think your ! 

Re: [R] help with recursive function

2017-12-14 Thread Eric Berger
If you are trying to understand why the "stopifnot" condition is met you
can replace it by something like:

if ( any(dat2$norm_sd >= 1) )
   browser()

This will put you in a debugging session where you can examine your
variables, e.g.

> dat$norm_sd

HTH,
Eric



On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger  wrote:

> The message is coming from your stopifnot() condition being met.
>
>
> On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <
> nilesh.di...@monsanto.com> wrote:
>
>> Hi, I accidently left out few lines of code from the calclp function.
>> Updated function is pasted below.
>>
>> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is
>> not TRUE“
>>
>>
>>
>> I would appreciate any help.
>>
>> Nilesh
>>
>> dput(calclp)
>>
>> function (dataset)
>>
>> {
>>
>> dat1 <- funlp1(dataset)
>>
>> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>>
>> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>>
>> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>>
>> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
>> grep("_",
>>
>> names(.)), funs(norm = round(scale(.), 3)))
>>
>> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, sd, na.rm = TRUE), 3)
>>
>> dat2$norm_max <- round(apply(dat2[, grep("lp_norm",
>> names(dat2))],
>>
>> 1, function(x) {
>>
>> max(abs(x), na.rm = TRUE)
>>
>> }), 3)
>>
>> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
>>
>> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
>>
>> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)
>>
>> stopifnot(!(any(data1$norm_sd >= 1)))
>>
>> if (!(any(data1$norm_sd >= 1))) {
>>
>> df1 <- dat1
>>
>> return(df1)
>>
>> }
>>
>>else {
>>
>> df2 <- recursive_funlp()
>>
>> return(df2)
>>
>> }
>>
>> }
>>
>> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>>
>> df3
>>
>> }
>>
>>
>>
>>
>>
>> *From:* DIGHE, NILESH [AG/2362]
>> *Sent:* Thursday, December 14, 2017 9:01 AM
>> *To:* 'Eric Berger' 
>> *Cc:* r-help 
>> *Subject:* RE: [R] help with recursive function
>>
>>
>>
>> Eric:  Thanks for taking time to look into my problem.  Despite of making
>> the change you suggested, I am still getting the same error.  I am
>> wondering if the logic I am using in the stopifnot and if functions is a
>> problem.
>>
>> I like the recursive function to stop whenever the norm_sd column has
>> zero values that are above or equal to 1. Below is the calclp function
>> after the changes you suggested.
>>
>> Thanks. Nilesh
>>
>>
>>
>> dput(calclp)
>>
>> function (dataset)
>>
>> {
>>
>> dat1 <- funlp1(dataset)
>>
>> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>>
>> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>>
>> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>>
>> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
>> grep("_",
>>
>> names(.)), funs(norm = round(scale(.), 3)))
>>
>> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, sd, na.rm = TRUE), 3)
>>
>> dat2$norm_max <- round(apply(dat2[, grep("lp_norm",
>> names(dat2))],
>>
>> 1, function(x) {
>>
>> max(abs(x), na.rm = TRUE)
>>
>> }), 3)
>>
>> stopifnot(!(any(dat2$norm_sd >= 1)))
>>
>> if (!(any(dat2$norm_sd >= 1))) {
>>
>> df1 <- dat1
>>
>> return(df1)
>>
>> }
>>
>> else {
>>
>> df2 <- recursive_funlp()
>>
>> return(df2)
>>
>> }
>>
>> }
>>
>> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>>
>> df3
>>
>> }
>>
>>
>>
>>
>>
>> *From:* Eric Berger [mailto:ericjber...@gmail.com ]
>>
>> *Sent:* Thursday, December 14, 2017 8:17 AM
>> *To:* DIGHE, NILESH [AG/2362] 
>> *Cc:* r-help 
>> *Subject:* Re: [R] help with recursive function
>>
>>
>>
>> My own typo ... whoops ...
>>
>>
>>
>> !( any(dat2$norm_sd >= 1 ))
>>
>>
>>
>>
>>
>>
>>
>> On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger 
>> wrote:
>>
>> You seem to have a typo at this expression (and some others like it)
>>
>>
>>
>> Namely, you write
>>
>>
>>
>> any(!dat2$norm_sd) >= 1
>>
>>
>>
>> when you possibly meant to write
>>
>>
>>
>> !( any(dat2$norm_sd) >= 1 )
>>
>>
>>
>> i.e. I think your ! seems to be in the wrong place.
>>
>>
>>
>> HTH,
>> Eric
>>
>>
>>
>>
>>
>> On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <
>> nilesh.di...@monsanto.com> wrote:
>>
>> Hi, I need some help with running a recursive function. I like to run
>> funlp2 recursively.
>> 

Re: [ESS] connection to Oracle does not work in ESS

2017-12-14 Thread Bond, Stephen
ESS or emacs stores sth between sessions! I tried quitting the R session and 
connecting from a new session in the same emacs window and it did not work.
Today I closed the whole emacs window and reopened the same R code and it 
worked.

Stephen 


-Original Message-
From: Vitalie Spinu [mailto:spinu...@gmail.com] 
Sent: Wednesday, December 13, 2017 5:34 PM
To: Bond, Stephen
Cc: 'ess-help@r-project.org'
Subject: Re: [ESS] connection to Oracle does not work in ESS


Hi Stephen,

I doubt it's problem with ESS. Running R from emacs shell and with ESS should 
be equivalent. Make sure that it's indeed the same R instance and RODBC what 
are called in your experiments. Do you have a custom 
inferior-ess-r-program-name setup?

Do you issue Rterm or R in the emacs shell?

  Vitalie

>> On Wed, Dec 13 2017 22:23, Bond, Stephen wrote:

> Further info:

> When I use Rterm in a cmd shell the connection works.
> When I use Rterm in a shell within Emacs the connection works.
> Something must be going wrong in the way ESS does things.

> Stephen

> From: Bond, Stephen
> Sent: Wednesday, December 13, 2017 12:53 PM
> To: 'ess-help@r-project.org'
> Subject: connection to Oracle does not work in ESS

> On 64-bit win7:

> I have a DSN which tests successfully in control panel. I can connect from 
> RGui with:
> con=odbcConnect("DSNname",uid="myuid",pwd="mypass")

> it does not work in ESS:

> R version 3.4.2 (2017-09-28) -- "Short Summer"
> Copyright (C) 2017 The R Foundation for Statistical Computing
> Platform: x86_64-w64-mingw32/x64 (64-bit)

>> > options(chmhelp=FALSE, help_type="text")
>> options(STERM='iESS', str.dendrogram.last="'", 
>> editor='emacsclient.exe', show.error.locations=TRUE)
>> library(RODBC)
>> con=odbcConnect("DSNname",uid="myuid",pwd="mypass")

> Warning messages:
> 1: In RODBC::odbcDriverConnect() :
>   [RODBC] ERROR: state 08004, code 12154, message 
> [Oracle][ODBC][Ora]ORA-12154: TNS:could not resolve the connect 
> identifier specified
> 2: In RODBC::odbcDriverConnect() :
>   ODBC connection failed

> I have all the paths setup and the driver is installed correctly. Tnsping 
> finds the db.

> Let me know if anybody can help.

> Stephen

>   [[alternative HTML version deleted]]

> __
> ESS-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/ess-help

__
ESS-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/ess-help


Re: [R] help with recursive function

2017-12-14 Thread Eric Berger
The message is coming from your stopifnot() condition being met.


On Thu, Dec 14, 2017 at 5:31 PM, DIGHE, NILESH [AG/2362] <
nilesh.di...@monsanto.com> wrote:

> Hi, I accidently left out few lines of code from the calclp function.
> Updated function is pasted below.
>
> I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is
> not TRUE“
>
>
>
> I would appreciate any help.
>
> Nilesh
>
> dput(calclp)
>
> function (dataset)
>
> {
>
> dat1 <- funlp1(dataset)
>
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
>
> names(.)), funs(norm = round(scale(.), 3)))
>
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, sd, na.rm = TRUE), 3)
>
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, function(x) {
>
> max(abs(x), na.rm = TRUE)
>
> }), 3)
>
> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
>
> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
>
> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)
>
> stopifnot(!(any(data1$norm_sd >= 1)))
>
> if (!(any(data1$norm_sd >= 1))) {
>
> df1 <- dat1
>
> return(df1)
>
> }
>
>else {
>
> df2 <- recursive_funlp()
>
> return(df2)
>
> }
>
> }
>
> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>
> df3
>
> }
>
>
>
>
>
> *From:* DIGHE, NILESH [AG/2362]
> *Sent:* Thursday, December 14, 2017 9:01 AM
> *To:* 'Eric Berger' 
> *Cc:* r-help 
> *Subject:* RE: [R] help with recursive function
>
>
>
> Eric:  Thanks for taking time to look into my problem.  Despite of making
> the change you suggested, I am still getting the same error.  I am
> wondering if the logic I am using in the stopifnot and if functions is a
> problem.
>
> I like the recursive function to stop whenever the norm_sd column has zero
> values that are above or equal to 1. Below is the calclp function after the
> changes you suggested.
>
> Thanks. Nilesh
>
>
>
> dput(calclp)
>
> function (dataset)
>
> {
>
> dat1 <- funlp1(dataset)
>
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
>
> names(.)), funs(norm = round(scale(.), 3)))
>
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, sd, na.rm = TRUE), 3)
>
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, function(x) {
>
> max(abs(x), na.rm = TRUE)
>
> }), 3)
>
> stopifnot(!(any(dat2$norm_sd >= 1)))
>
> if (!(any(dat2$norm_sd >= 1))) {
>
> df1 <- dat1
>
> return(df1)
>
> }
>
> else {
>
> df2 <- recursive_funlp()
>
> return(df2)
>
> }
>
> }
>
> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>
> df3
>
> }
>
>
>
>
>
> *From:* Eric Berger [mailto:ericjber...@gmail.com ]
>
> *Sent:* Thursday, December 14, 2017 8:17 AM
> *To:* DIGHE, NILESH [AG/2362] 
> *Cc:* r-help 
> *Subject:* Re: [R] help with recursive function
>
>
>
> My own typo ... whoops ...
>
>
>
> !( any(dat2$norm_sd >= 1 ))
>
>
>
>
>
>
>
> On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger 
> wrote:
>
> You seem to have a typo at this expression (and some others like it)
>
>
>
> Namely, you write
>
>
>
> any(!dat2$norm_sd) >= 1
>
>
>
> when you possibly meant to write
>
>
>
> !( any(dat2$norm_sd) >= 1 )
>
>
>
> i.e. I think your ! seems to be in the wrong place.
>
>
>
> HTH,
> Eric
>
>
>
>
>
> On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <
> nilesh.di...@monsanto.com> wrote:
>
> Hi, I need some help with running a recursive function. I like to run
> funlp2 recursively.
> When I try to run recursive function in another function named "calclp" I
> get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".
>
> I have never built a recursive function before so having trouble executing
> it in this case.  I would appreciate any help or guidance to resolve this
> issue. Please see my data and the three functions that I am using below.
> Please note that calclp is the function I am running and the other two
> functions are within this calclp function.
>
> # code:
> Test<- calclp(dataset = dat)
>
> # calclp function
>
> calclp<- function (dataset)
>
> {

Re: [R] help with recursive function

2017-12-14 Thread DIGHE, NILESH [AG/2362]
Hi, I accidently left out few lines of code from the calclp function.  Updated 
function is pasted below.
I am still getting the same error “Error: !(any(data1$norm_sd >= 1)) is not 
TRUE“

I would appreciate any help.
Nilesh
dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
-norm_sd) %>% separate(key, c("field_rep", "treatment"),
"\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)
stopifnot(!(any(data1$norm_sd >= 1)))
if (!(any(data1$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
   else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: DIGHE, NILESH [AG/2362]
Sent: Thursday, December 14, 2017 9:01 AM
To: 'Eric Berger' 
Cc: r-help 
Subject: RE: [R] help with recursive function

Eric:  Thanks for taking time to look into my problem.  Despite of making the 
change you suggested, I am still getting the same error.  I am wondering if the 
logic I am using in the stopifnot and if functions is a problem.
I like the recursive function to stop whenever the norm_sd column has zero 
values that are above or equal to 1. Below is the calclp function after the 
changes you suggested.
Thanks. Nilesh

dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
stopifnot(!(any(dat2$norm_sd >= 1)))
if (!(any(dat2$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: Eric Berger [mailto:ericjber...@gmail.com]
Sent: Thursday, December 14, 2017 8:17 AM
To: DIGHE, NILESH [AG/2362] 
>
Cc: r-help >
Subject: Re: [R] help with recursive function

My own typo ... whoops ...

!( any(dat2$norm_sd >= 1 ))



On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger 
> wrote:
You seem to have a typo at this expression (and some others like it)

Namely, you write

any(!dat2$norm_sd) >= 1

when you possibly meant to write

!( any(dat2$norm_sd) >= 1 )

i.e. I think your ! seems to be in the wrong place.

HTH,
Eric


On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] 
> wrote:
Hi, I need some help with running a recursive function. I like to run funlp2 
recursively.
When I try to run recursive function in another function named "calclp" I get 
this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".

I have never built a recursive function before so having trouble executing it 
in this case.  I would appreciate any help or guidance to resolve this issue. 
Please see my data and the three functions that I am using below.
Please note that calclp is the function I am running and the other two 
functions are within this calclp function.

# code:
Test<- calclp(dataset = dat)

# calclp function

calclp<- function (dataset)

{

dat1 <- funlp1(dataset)

recursive_funlp <- function(dataset = dat1, func = funlp2) {

dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%

mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%

spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",

names(.)), funs(norm = round(scale(.), 3)))

dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, sd, na.rm = TRUE), 3)

dat2$norm_max <- 

Re: [R] help with recursive function

2017-12-14 Thread DIGHE, NILESH [AG/2362]
Eric:  Thanks for taking time to look into my problem.  Despite of making the 
change you suggested, I am still getting the same error.  I am wondering if the 
logic I am using in the stopifnot and if functions is a problem.
I like the recursive function to stop whenever the norm_sd column has zero 
values that are above or equal to 1. Below is the calclp function after the 
changes you suggested.
Thanks. Nilesh

dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",
names(.)), funs(norm = round(scale(.), 3)))
dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, sd, na.rm = TRUE), 3)
dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
1, function(x) {
max(abs(x), na.rm = TRUE)
}), 3)
stopifnot(!(any(dat2$norm_sd >= 1)))
if (!(any(dat2$norm_sd >= 1))) {
df1 <- dat1
return(df1)
}
else {
df2 <- recursive_funlp()
return(df2)
}
}
df3 <- recursive_funlp(dataset = dat1, func = funlp2)
df3
}


From: Eric Berger [mailto:ericjber...@gmail.com]
Sent: Thursday, December 14, 2017 8:17 AM
To: DIGHE, NILESH [AG/2362] 
Cc: r-help 
Subject: Re: [R] help with recursive function

My own typo ... whoops ...

!( any(dat2$norm_sd >= 1 ))



On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger 
> wrote:
You seem to have a typo at this expression (and some others like it)

Namely, you write

any(!dat2$norm_sd) >= 1

when you possibly meant to write

!( any(dat2$norm_sd) >= 1 )

i.e. I think your ! seems to be in the wrong place.

HTH,
Eric


On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] 
> wrote:
Hi, I need some help with running a recursive function. I like to run funlp2 
recursively.
When I try to run recursive function in another function named "calclp" I get 
this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".

I have never built a recursive function before so having trouble executing it 
in this case.  I would appreciate any help or guidance to resolve this issue. 
Please see my data and the three functions that I am using below.
Please note that calclp is the function I am running and the other two 
functions are within this calclp function.

# code:
Test<- calclp(dataset = dat)

# calclp function

calclp<- function (dataset)

{

dat1 <- funlp1(dataset)

recursive_funlp <- function(dataset = dat1, func = funlp2) {

dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%

mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%

spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",

names(.)), funs(norm = round(scale(.), 3)))

dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, sd, na.rm = TRUE), 3)

dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, function(x) {

max(abs(x), na.rm = TRUE)

}), 3)

stopifnot(any(!dat2$norm_sd) >= 1)

if (any(!dat2$norm_sd) >= 1) {

df1 <- dat1

return(df1)

}

else {

df2 <- recursive_funlp()

return(df2)

}

}

df3 <- recursive_funlp(dataset = dat1, func = funlp2)

df3

}


# funlp1 function

funlp1<- function (dataset)

{

dat2 <- dataset %>% select(field, set, ent_num, rep_num,

lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>%

unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep,

"lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>%

mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.),

3)))

dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, sd, na.rm = TRUE), 3)

dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, function(x) {

max(abs(x), na.rm = TRUE)

}), 3)

data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,

-norm_sd) %>% separate(key, c("field_rep", "treatment"),

"\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)

df_clean <- with(data1, data1[norm_sd < 1, ])

datD <- with(data1, data1[norm_sd >= 1, ])

s <- split(datD, datD$uniqueid)

sdf <- lapply(s, function(x) {

data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,

ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),

x$lp <- with(x, 

Re: [R] help with recursive function

2017-12-14 Thread Eric Berger
My own typo ... whoops ...

!( any(dat2$norm_sd >= 1 ))



On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger  wrote:

> You seem to have a typo at this expression (and some others like it)
>
> Namely, you write
>
> any(!dat2$norm_sd) >= 1
>
> when you possibly meant to write
>
> !( any(dat2$norm_sd) >= 1 )
>
> i.e. I think your ! seems to be in the wrong place.
>
> HTH,
> Eric
>
>
> On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <
> nilesh.di...@monsanto.com> wrote:
>
>> Hi, I need some help with running a recursive function. I like to run
>> funlp2 recursively.
>> When I try to run recursive function in another function named "calclp" I
>> get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".
>>
>> I have never built a recursive function before so having trouble
>> executing it in this case.  I would appreciate any help or guidance to
>> resolve this issue. Please see my data and the three functions that I am
>> using below.
>> Please note that calclp is the function I am running and the other two
>> functions are within this calclp function.
>>
>> # code:
>> Test<- calclp(dataset = dat)
>>
>> # calclp function
>>
>> calclp<- function (dataset)
>>
>> {
>>
>> dat1 <- funlp1(dataset)
>>
>> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>>
>> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>>
>> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>>
>> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
>> grep("_",
>>
>> names(.)), funs(norm = round(scale(.), 3)))
>>
>> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, sd, na.rm = TRUE), 3)
>>
>> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, function(x) {
>>
>> max(abs(x), na.rm = TRUE)
>>
>> }), 3)
>>
>> stopifnot(any(!dat2$norm_sd) >= 1)
>>
>> if (any(!dat2$norm_sd) >= 1) {
>>
>> df1 <- dat1
>>
>> return(df1)
>>
>> }
>>
>> else {
>>
>> df2 <- recursive_funlp()
>>
>> return(df2)
>>
>> }
>>
>> }
>>
>> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>>
>> df3
>>
>> }
>>
>>
>> # funlp1 function
>>
>> funlp1<- function (dataset)
>>
>> {
>>
>> dat2 <- dataset %>% select(field, set, ent_num, rep_num,
>>
>> lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>%
>>
>> unite(field_rep, field, rep_num) %>% mutate(field_rep =
>> paste(field_rep,
>>
>> "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>%
>>
>> mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.),
>>
>> 3)))
>>
>> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, sd, na.rm = TRUE), 3)
>>
>> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>>
>> 1, function(x) {
>>
>> max(abs(x), na.rm = TRUE)
>>
>> }), 3)
>>
>> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
>>
>> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
>>
>> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)
>>
>> df_clean <- with(data1, data1[norm_sd < 1, ])
>>
>> datD <- with(data1, data1[norm_sd >= 1, ])
>>
>> s <- split(datD, datD$uniqueid)
>>
>> sdf <- lapply(s, function(x) {
>>
>> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,
>>
>> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),
>>
>> x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))
>>
>> x
>>
>> })
>>
>> sdf2 <- bind_rows(sdf)
>>
>> all_dat <- bind_rows(df_clean, sdf2)
>>
>> all_dat
>>
>> }
>>
>>
>> # funlp2 function
>>
>> funlp2<-function (dataset)
>>
>> {
>>
>> data1 <- dataset
>>
>> df_clean <- with(data1, data1[norm_sd < 1, ])
>>
>> datD <- with(data1, data1[norm_sd >= 1, ])
>>
>> s <- split(datD, datD$uniqueid)
>>
>> sdf <- lapply(s, function(x) {
>>
>> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,
>>
>> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),
>>
>> x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))
>>
>> x
>>
>> })
>>
>> sdf2 <- bind_rows(sdf)
>>
>> all_dat <- bind_rows(df_clean, sdf2)
>>
>> all_dat
>>
>> }
>>
>>
>> # dataset
>> dput(dat)
>> structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
>> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", 

Re: [R] help with recursive function

2017-12-14 Thread Eric Berger
You seem to have a typo at this expression (and some others like it)

Namely, you write

any(!dat2$norm_sd) >= 1

when you possibly meant to write

!( any(dat2$norm_sd) >= 1 )

i.e. I think your ! seems to be in the wrong place.

HTH,
Eric


On Thu, Dec 14, 2017 at 3:26 PM, DIGHE, NILESH [AG/2362] <
nilesh.di...@monsanto.com> wrote:

> Hi, I need some help with running a recursive function. I like to run
> funlp2 recursively.
> When I try to run recursive function in another function named "calclp" I
> get this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".
>
> I have never built a recursive function before so having trouble executing
> it in this case.  I would appreciate any help or guidance to resolve this
> issue. Please see my data and the three functions that I am using below.
> Please note that calclp is the function I am running and the other two
> functions are within this calclp function.
>
> # code:
> Test<- calclp(dataset = dat)
>
> # calclp function
>
> calclp<- function (dataset)
>
> {
>
> dat1 <- funlp1(dataset)
>
> recursive_funlp <- function(dataset = dat1, func = funlp2) {
>
> dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%
>
> mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%
>
> spread(key = field_rep, value = lp) %>% mutate_at(.vars =
> grep("_",
>
> names(.)), funs(norm = round(scale(.), 3)))
>
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, sd, na.rm = TRUE), 3)
>
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, function(x) {
>
> max(abs(x), na.rm = TRUE)
>
> }), 3)
>
> stopifnot(any(!dat2$norm_sd) >= 1)
>
> if (any(!dat2$norm_sd) >= 1) {
>
> df1 <- dat1
>
> return(df1)
>
> }
>
> else {
>
> df2 <- recursive_funlp()
>
> return(df2)
>
> }
>
> }
>
> df3 <- recursive_funlp(dataset = dat1, func = funlp2)
>
> df3
>
> }
>
>
> # funlp1 function
>
> funlp1<- function (dataset)
>
> {
>
> dat2 <- dataset %>% select(field, set, ent_num, rep_num,
>
> lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>%
>
> unite(field_rep, field, rep_num) %>% mutate(field_rep =
> paste(field_rep,
>
> "lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>%
>
> mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.),
>
> 3)))
>
> dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, sd, na.rm = TRUE), 3)
>
> dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],
>
> 1, function(x) {
>
> max(abs(x), na.rm = TRUE)
>
> }), 3)
>
> data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,
>
> -norm_sd) %>% separate(key, c("field_rep", "treatment"),
>
> "\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)
>
> df_clean <- with(data1, data1[norm_sd < 1, ])
>
> datD <- with(data1, data1[norm_sd >= 1, ])
>
> s <- split(datD, datD$uniqueid)
>
> sdf <- lapply(s, function(x) {
>
> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,
>
> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),
>
> x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))
>
> x
>
> })
>
> sdf2 <- bind_rows(sdf)
>
> all_dat <- bind_rows(df_clean, sdf2)
>
> all_dat
>
> }
>
>
> # funlp2 function
>
> funlp2<-function (dataset)
>
> {
>
> data1 <- dataset
>
> df_clean <- with(data1, data1[norm_sd < 1, ])
>
> datD <- with(data1, data1[norm_sd >= 1, ])
>
> s <- split(datD, datD$uniqueid)
>
> sdf <- lapply(s, function(x) {
>
> data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,
>
> ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),
>
> x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))
>
> x
>
> })
>
> sdf2 <- bind_rows(sdf)
>
> all_dat <- bind_rows(df_clean, sdf2)
>
> all_dat
>
> }
>
>
> # dataset
> dput(dat)
> structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
> "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01",
> "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
> "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
> "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
> "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
> "OL01", 

[R] help with recursive function

2017-12-14 Thread DIGHE, NILESH [AG/2362]
Hi, I need some help with running a recursive function. I like to run funlp2 
recursively.
When I try to run recursive function in another function named "calclp" I get 
this "Error: any(!dat2$norm_sd) >= 1 is not TRUE".

I have never built a recursive function before so having trouble executing it 
in this case.  I would appreciate any help or guidance to resolve this issue. 
Please see my data and the three functions that I am using below.
Please note that calclp is the function I am running and the other two 
functions are within this calclp function.

# code:
Test<- calclp(dataset = dat)

# calclp function

calclp<- function (dataset)

{

dat1 <- funlp1(dataset)

recursive_funlp <- function(dataset = dat1, func = funlp2) {

dat2 <- dataset %>% select(uniqueid, field_rep, lp) %>%

mutate(field_rep = paste(field_rep, "lp", sep = ".")) %>%

spread(key = field_rep, value = lp) %>% mutate_at(.vars = grep("_",

names(.)), funs(norm = round(scale(.), 3)))

dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, sd, na.rm = TRUE), 3)

dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, function(x) {

max(abs(x), na.rm = TRUE)

}), 3)

stopifnot(any(!dat2$norm_sd) >= 1)

if (any(!dat2$norm_sd) >= 1) {

df1 <- dat1

return(df1)

}

else {

df2 <- recursive_funlp()

return(df2)

}

}

df3 <- recursive_funlp(dataset = dat1, func = funlp2)

df3

}


# funlp1 function

funlp1<- function (dataset)

{

dat2 <- dataset %>% select(field, set, ent_num, rep_num,

lp) %>% unite(uniqueid, set, ent_num, sep = ".") %>%

unite(field_rep, field, rep_num) %>% mutate(field_rep = paste(field_rep,

"lp", sep = ".")) %>% spread(key = field_rep, value = lp) %>%

mutate_at(.vars = grep("_", names(.)), funs(norm = round(scale(.),

3)))

dat2$norm_sd <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, sd, na.rm = TRUE), 3)

dat2$norm_max <- round(apply(dat2[, grep("lp_norm", names(dat2))],

1, function(x) {

max(abs(x), na.rm = TRUE)

}), 3)

data1 <- dat2 %>% gather(key, value, -uniqueid, -norm_max,

-norm_sd) %>% separate(key, c("field_rep", "treatment"),

"\\.") %>% spread(treatment, value) %>% mutate(outlier = NA)

df_clean <- with(data1, data1[norm_sd < 1, ])

datD <- with(data1, data1[norm_sd >= 1, ])

s <- split(datD, datD$uniqueid)

sdf <- lapply(s, function(x) {

data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,

ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),

x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))

x

})

sdf2 <- bind_rows(sdf)

all_dat <- bind_rows(df_clean, sdf2)

all_dat

}


# funlp2 function

funlp2<-function (dataset)

{

data1 <- dataset

df_clean <- with(data1, data1[norm_sd < 1, ])

datD <- with(data1, data1[norm_sd >= 1, ])

s <- split(datD, datD$uniqueid)

sdf <- lapply(s, function(x) {

data.frame(x, x$outlier <- ifelse(is.na(x$lp_norm), NA,

ifelse(abs(x$lp_norm) == x$norm_max, "yes", "no")),

x$lp <- with(x, ifelse(outlier == "yes", NA, lp)))

x

})

sdf2 <- bind_rows(sdf)

all_dat <- bind_rows(df_clean, sdf2)

all_dat

}


# dataset
dput(dat)
structure(list(field = c("LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01",
"LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "LM01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01",
"OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "OL01", "SGI1",
"SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",
"SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",
"SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",
"SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1",
"SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", "SGI1", 

[R] multiple instances of predictor variable per model

2017-12-14 Thread Sima Usvyatsov
I’m running a model on animal behavior in response to shipping. In most
cases, there is only one ship in the study area at one time. Ship length,
distance from the animals, speed, angle from animals, and ship direction
(as east/west bound) are among shipping-related covariates (with multiple
interactions).

The tricky part is that sometimes there are 2 ships in the area. I could
add all the same covariates, but doubled-up for the second ship. However,
this really hurts convergence. And - conceptually - why would ship length
affect the animals differently between ship 1 and ship 2? I would think
that animals would react similarly to both ships (and the effect would just
add up), so I don’t want the model to estimate two covariates that I think
are the same. And if I had 5 ships instead of 2, those dfs would really
rack up.

Note that I can’t just double the vessel values, since their speeds,
directions, lengths, etc all differ.

Here's a little mock data set for 3 surveys - 2 have a single ship, and 1
has two ships. Note that each survey is only done once, so if there are 2
ships (or more), the number of animals (and all other survey-related info)
is just copied over on another line

df <- data.frame(Survey = c(1, 1, 2, 3), NAnimals = c(10, 10, 1, 0),
Vessel = c("A", "B", "C", "D"), VesselLength = c(20, 50, 40, 70),
VesselSpeed = c(10, 5, 4, 5), Direction = c("West", "East", "West", "West"))

Disclaimer: this is a crosspost from here (
https://stats.stackexchange.com/questions/318667/multiple-instances-of-predictor-variables
).

Many thanks.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] change in behavior of c.trellis

2017-12-14 Thread Duncan Murdoch
This looks like a bug in latticeExtra:  in the c.trellis function there 
are these lines:


## some prepanel functions require a 'subscripts' argument in each 
'panel.args'

if ("subscripts" %in% c(names(formals(obj1$prepanel.default)),
names(formals(obj1$prepanel {

But obj1$prepanel is NULL in your test case.  NULL is not a legal 
argument to formals(), and it now warns about it.


Duncan Murdoch

On 14/12/2017 1:12 AM, Richard M. Heiberger wrote:

library(latticeExtra)

Loading required package: lattice
Loading required package: RColorBrewer

t11 <- xyplot(1 ~ 1)
t11
c(t11, t11)

Warning message:
In formals(fun) : argument is not a function

version

_
platform   x86_64-w64-mingw32
arch   x86_64
os mingw32
system x86_64, mingw32
status Patched
major  3
minor  4.3
year   2017
month  12
day12
svn rev73903
language   R
version.string R version 3.4.3 Patched (2017-12-12 r73903)
nickname   Kite-Eating Tree





the c.trellis seems to work correctly except for the new warning.

There was no warning in 3.3.3.
I first noticed it in 3.4.1

the c.trellis function itself is not changed between 3.3.3 and 3.4.3 Patched.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] overlay two histograms ggplot

2017-12-14 Thread Thierry Onkelinx
Dear Elahe,

Something like

joint <- rbind(
  data.frame(x = gg$Az, veg = gg$veg, type = "Alz"),
  data.frame(x = tt$Cont, veg = gg$veg, type = "Cont")
)
ggplot(joint, aes(x = x, fill = veg, colour = type)) + geom_plot(alpha = 0.2)

Best regards,

ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE
AND FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkel...@inbo.be
Kliniekstraat 25, B-1070 Brussel
www.inbo.be

///
To call in the statistician after the experiment is done may be no
more than asking him to perform a post-mortem examination: he may be
able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data. ~ John Tukey
///


Van 14 tot en met 19 december 2017 verhuizen we uit onze vestiging in
Brussel naar het Herman Teirlinckgebouw op de site Thurn & Taxis.
Vanaf dan ben je welkom op het nieuwe adres: Havenlaan 88 bus 73, 1000 Brussel.

///



2017-12-13 13:14 GMT+01:00 Elahe chalabi via R-help :
> Hi all,
>
> How can I overlay these two histograms?
>
> ggplot(gg, aes(gg$Alz, fill = gg$veg)) + geom_histogram(alpha = 0.2)
> ggplot(tt, aes(tt$Cont, fill = tt$veg)) + geom_histogram(alpha = 0.2)
>
> thanks for any help!
> Elahe
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] Distributions for gbm models

2017-12-14 Thread Patrick Connolly
On page 409 of "Applied Predictive Modeling" by Max Kuhn, it states
that the gbm function can accomodate only two class problems when
referring to the distribution parameter.  

>From gbm help re: the distribution parameter:


  Currently available options are "gaussian" (squared error),
  "laplace" (absolute loss), "tdist" (t-distribution loss),
  "bernoulli" (logistic regression for 0-1 outcomes),
  "huberized" (huberized hinge loss for 0-1 outcomes),
  "multinomial" (classification when there are more than 2
  classes), "adaboost" (the AdaBoost exponential loss for 0-1
  outcomes), "poisson" (count outcomes), "coxph" (right
  censored observations), "quantile", or "pairwise" (ranking
  measure using the LambdaMart algorithm).


I would have thought that huberized and multinomial would also be
possible.  Is that not so?  In any case, how would anything different
from bernoulli (the default) be specified when using the caret train
function since distribution appears not to be among the list of
parameters that caret recognises?

> getModelInfo("gbm")[["gbm"]]$parameters
  parameter   class   label
1   n.trees numeric   # Boosting Iterations
2 interaction.depth numeric  Max Tree Depth
3 shrinkage numeric   Shrinkage
4n.minobsinnode numeric Min. Terminal Node Size

Is that a limitation of the caret package?  Or is there something I'm
not getting?

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] Aggregation across two variables in data.table

2017-12-14 Thread PIKAL Petr
Hi

Are you aware of function aggregate?

result <- with(data_tmp, aggregate(Theta, list(Marital, Education), mean))

should do the trick.

Cheers
Petr

> -Original Message-
> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Michael
> Haenlein
> Sent: Thursday, December 14, 2017 8:48 AM
> To: r-help@r-project.org
> Cc: Michael Haenlein 
> Subject: [R] Aggregation across two variables in data.table
>
> Dear all,
>
> I have a data.frame that includes a series of demographic variables for a
> set of respondents plus a dependent variable (Theta). For example:
>
>AgeEducation   Marital Familysize
> IncomeHousingTheta
> 1:  50 Associate degree  Divorced  4
>  70K+Owned with mortgage 9.14
> 2:  65  Bachelor degree   Married  1
> 10-15K Owned without mortgage 7.345036
> 3:  33  Bachelor degree   Married  2
> 30-40KOwned with mortgage 7.974937
> 4:  69  Bachelor degree Never married  1
>  70K+Owned with mortgage 7.733053
> 5:  54 Some college, less than college graduate Never married  3
> 30-40K Rented 7.648642
> 6:  35 Associate degree Separated  2
> 10-15K Rented 7.496411
>
> My objective is to calculate the average of Theta across all pairs of two
> demographics.
>
> For 1 demographic this is straightforward:
>
> Demo_names <- c("Age", "Education", "Marital", "Familysize", "Income",
> "Housing")
> means1 <- as.list(rep(0, length(Demo_names)))
> for (i in 1:length(Demo_names)) {
> Demo_tmp <- Demo_names[i]
> means1[[i]] <- data_tmp[,list(mean(Theta)),by=Demo_tmp]}
>
> Is there an easy way to extent this logic to more than 1 variable? I know
> how to do this manually, e.g.,
> data_tmp[,list(mean(Theta)),by=list(Marital, Education)]
>
> But I don't know how to integrate this into a loop.
>
> Thanks,
>
> Michael
>
>   [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs