Re: [R] How to avoid a divide by zero issue.

2022-01-25 Thread Jim Lemon
Hi Neha,
One sure cure for divide by zero is to omit zeros in the denominator variable.

num<-sample(0:10,20,TRUE)
denom<-sample(0:10,20,TRUE)
zeros<-denom == 0
num[!zeros]/denom[!zeros]

If you don't want to lose those data and there are no negative values,
you could add a small number to all denominator values if that would
still give useful output.

Jim

On Wed, Jan 26, 2022 at 7:51 AM Neha gupta  wrote:
>
> Hello everyone
>
> I have an output variable (0/1) and I want to evaluate how the model is
> biased when we have 1 and when 0 value.
>
> The problem is that the package of R I use here is designed originally to
> check bias for input metrics. When I evaluate it for this output variable,
> it gives me NA values, probably a divide by zero problem. Can I avoid this
> issue and how (I am not very expert of R language).
>
> Warm regards
>
> [[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.


Re: [R] Constructing confidence interval ellipses with R

2022-01-25 Thread John Fox

Dear Paul,

This looks like a version of the question you asked a couple of weeks 
ago. As I explained then, I'm pretty sure that you want concentration 
(i.e., data) ellipses and not confidence ellipses, which pertain to 
parameters (e.g., regression coefficients). Also, the hand-drawn 
concentration contours in your example graph don't look elliptical, so 
I'm not sure that you really want ellipses, but I'll assume that you do.


Since as far as I can see you didn't share your data, here's a similar 
example using the scatterplot() function in the car package:


library("car")
scatterplot(prestige ~ income | type, data=Prestige, ellipse=TRUE, 
smooth=FALSE, regLine=FALSE)


By default, this draws 50% and 95% concentration ellipses assuming 
bivariate normality in each group, but that and other aspects of the 
graph can be customized -- see ?scatterplot.


I hope this helps,
 John

--
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

On 2022-01-24 4:24 p.m., Paul Bernal wrote:

Dear friends,

I will be sharing a dataset which has the following columns:
1. Scenario
2. Day of Transit date
3. Canal Ampliado
4. Canal Original

Basically, I need to create a scatter plot diagram, with the Canal Ampliado
column values in the x-axis, and the Canal Original column values in the
y-axis, but also, I need to create confidence interval ellipses grouping
the points on the scatterplot, based on the different scenarios.

So I need to have in one graph, the scatterplot of Canal Ampliado vs Canal
Original and then, on the same graph, construct the confidence interval
ellipses.

I will attach an image depicting what I need to accomplish, as well as the
dataset, for your reference.

Any help and/or guidance will be greatly appreciated.

Cheers,
Paul


__
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] function problem: multi selection in one argument

2022-01-25 Thread Martin Maechler
> Rui Barradas 
> on Tue, 25 Jan 2022 14:22:47 + writes:

> Hello,
> Here are 3 functions that do what the question asks for. The first 2 are 
> tidyverse solutions, the last one a base R function.

> I don't understand why the group_by and mutate, that's why I've included 
> a 2nd tidyverse function. And the base R function follows the same lines 
> of outputing the table only without data transformation.

> And the test dataset is not iris, it only has one factor variable. It 
> doesn't make sense to table a continuous variable such as Petal.Width.

> The data set mtcars has several numeric variables that are in fact 
> categorical ("vs", "am") and one, "cyl", that can be tabled. The 
> examples below use mtcars.



> f3 <- function(data, ...){
>   groups <- unlist(list(...))
>   temp <- data %>%
> select(all_of({{groups}})) %>%
> group_by(across(all_of({{groups}}))) %>%
> mutate(numbering = row_number(), max = n())
>   temp %>%
> select(all_of({{groups}})) %>%
> table()
> }

> f4 <- function(data, ...){
>   groups <- unlist(list(...))
>   data %>%
> select(all_of({{groups}})) %>%
> table()
> }

> f5 <- function(data, ...){
>   temp <- lapply(list(...), \(col) data[[col]])
>   table(setNames(temp, list(...)))
> }

> f3(mtcars, "am", "cyl")
> f4(mtcars, "am", "cyl")
> f5(mtcars, "am", "cyl")

> Hope this helps,
> Rui Barradas

Thank you, Rui!

Note that your base R solution can be vastly simplified :

> f6 <- function(data, ...) table(data[, unlist(list(...))])
> f6(mtcars, "am", "cyl")
   cyl
am   4  6  8
  0  3  4 12
  1  8  3  2
> 

If you started measuring carefully, I'm sure this would not only
be the shortest but also by far the fastest solution ...

Best,
Martin

--
Martin Maechler
ETH Zurich  and   R Core Team


> Às 00:14 de 25/01/2022, Kai Yang via R-help escreveu:
>> Hello Team,
>> I can run the function below:
>> 
>> library(tidyverse)
>> 
>> f2 <- function(indata, subgrp1){
>>   indata0 <- indata
>>   temp<- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>%
>> group_by({{subgrp1}}) %>%
>> mutate(numbering =row_number(), max=max(numbering))
>>   view(temp)
>>   f_table <- table(temp$Species)
>>   view(f_table)
>>   return(f_table)
>> }
>> f2(iris, Species)
>> 
>> You can see the second argument I use Species only, and it works fine.
>> But If I say, I want the 2nd argument = Petal.Width, Species , how 
should I write the argument? I did try f2(iris, c(Petal.Width, Species)), but I 
got error message:
>> Error: arrange() failed at implicit mutate() step.
>> * Problem with `mutate()` column `..1`.
>> i `..1 = c(Petal.Width, Species)`.
>> i `..1` must be size 150 or 1, not 300.
>> 
>> I'm not sure how to fix the problem either in function or can fix it 
when using the function.
>> Thank you,
>> Kai
>> [[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] function problem: multi selection in one argument

2022-01-25 Thread Rui Barradas

Hello,

Here are 3 functions that do what the question asks for. The first 2 are 
tidyverse solutions, the last one a base R function.


I don't understand why the group_by and mutate, that's why I've included 
a 2nd tidyverse function. And the base R function follows the same lines 
of outputing the table only without data transformation.


And the test dataset is not iris, it only has one factor variable. It 
doesn't make sense to table a continuous variable such as Petal.Width.


The data set mtcars has several numeric variables that are in fact 
categorical ("vs", "am") and one, "cyl", that can be tabled. The 
examples below use mtcars.




f3 <- function(data, ...){
  groups <- unlist(list(...))
  temp <- data %>%
    select(all_of({{groups}})) %>%
    group_by(across(all_of({{groups}}))) %>%
    mutate(numbering = row_number(), max = n())
  temp %>%
    select(all_of({{groups}})) %>%
    table()
}

f4 <- function(data, ...){
  groups <- unlist(list(...))
  data %>%
    select(all_of({{groups}})) %>%
    table()
}

f5 <- function(data, ...){
  temp <- lapply(list(...), \(col) data[[col]])
  table(setNames(temp, list(...)))
}

f3(mtcars, "am", "cyl")
f4(mtcars, "am", "cyl")
f5(mtcars, "am", "cyl")


Hope this helps,

Rui Barradas

Às 00:14 de 25/01/2022, Kai Yang via R-help escreveu:

Hello Team,
I can run the function below:

library(tidyverse)

f2 <- function(indata, subgrp1){
   indata0 <- indata
   temp    <- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>%
     group_by({{subgrp1}}) %>%
     mutate(numbering =row_number(), max=max(numbering))
   view(temp)
   f_table <- table(temp$Species)
   view(f_table)
   return(f_table)
}
f2(iris, Species)

You can see the second argument I use Species only, and it works fine.
But If I say, I want the 2nd argument = Petal.Width, Species , how should I 
write the argument? I did try f2(iris, c(Petal.Width, Species)), but I got 
error message:
Error: arrange() failed at implicit mutate() step.
* Problem with `mutate()` column `..1`.
i `..1 = c(Petal.Width, Species)`.
i `..1` must be size 150 or 1, not 300.

I'm not sure how to fix the problem either in function or can fix it when using 
the function.
Thank you,
Kai
[[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.


Re: [R] function problem: multi selection in one argument

2022-01-25 Thread PIKAL Petr
Hallo

You should explain better what do you want as many people here do not use 
tidyverse functions.

I am not sure what the function should do.
table(iris$Species)
give the same result and whatever you do in tidyverse part it always finalise 
in table(...$Species)

Cheers
Petr

> -Original Message-
> From: R-help  On Behalf Of Kai Yang via R-help
> Sent: Tuesday, January 25, 2022 1:14 AM
> To: R-help Mailing List 
> Subject: [R] function problem: multi selection in one argument
>
> Hello Team,
> I can run the function below:
>
> library(tidyverse)
>
> f2 <- function(indata, subgrp1){
>   indata0 <- indata
>   temp<- indata0 %>% select({{subgrp1}}) %>% arrange({{subgrp1}}) %>%
> group_by({{subgrp1}}) %>%
> mutate(numbering =row_number(), max=max(numbering))
>   view(temp)
>   f_table <- table(temp$Species)
>   view(f_table)
>   return(f_table)
> }
> f2(iris, Species)
>
> You can see the second argument I use Species only, and it works fine. But 
> If I
> say, I want the 2nd argument = Petal.Width, Species , how should I write the
> argument? I did try f2(iris, c(Petal.Width, Species)), but I got error 
> message:
> Error: arrange() failed at implicit mutate() step.
> * Problem with `mutate()` column `..1`.
> i `..1 = c(Petal.Width, Species)`.
> i `..1` must be size 150 or 1, not 300.
>
> I'm not sure how to fix the problem either in function or can fix it when 
> using the
> function.
> Thank you,
> Kai
>   [[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.