Re: [R] Sensitivity and Specificity

2022-10-24 Thread Prof. Dr. Matthias Kohl

MKclass::perfMeasures(predict_testing, truth = testing$case, namePos = 1)

should also work and computes 80 performance measures.

Best Matthias

Am 25.10.22 um 06:42 schrieb Jin Li:

Hi Greg,

This can be done by:
spm::pred.acc(testing$case,  predict_testing)

It will return both sensitivity and specificity, along with a few other
commonly used measures.

Hope this helps,
Jin

On Tue, Oct 25, 2022 at 6:01 AM Rui Barradas  wrote:


Às 16:50 de 24/10/2022, greg holly escreveu:

Hi Michael,

I appreciate your writing. Here are what I have after;


predict_testing <- ifelse(predict > 0.5,1,0)

head(predict)

   1  2  3  5  7  8
0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920


# Sensitivity and Specificity





sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100

Error in predict_testing[2, 2] : incorrect number of dimensions

sensitivity

function (data, ...)
{
  UseMethod("sensitivity")
}








specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100

Error in predict_testing[1, 1] : incorrect number of dimensions

specificity

function (data, ...)
{
  UseMethod("specificity")
}



On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey 
wrote:


Rather hard to know without seeing what output you expected and what
error message you got if any but did you mean to summarise your variable
predict before doing anything with it?

Michael

On 24/10/2022 16:17, greg holly wrote:

Hi all R-Help ,

After partitioning my data to testing and training (please see below),

I

need to estimate the Sensitivity and Specificity. I failed. It would be
appropriate to get your help.

Best regards,
Greg


inTrain <- createDataPartition(y=data$case,
  p=0.7,
  list=FALSE)
training <- data[ inTrain,]
testing  <- data[-inTrain,]

attach(training)
#model training and prediction
data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
training, family = binomial(link="logit"))

predict <- predict(data_training, data_predict = testing, type =

"response")


predict_testing <- ifelse(predict > 0.5,1,0)

# Sensitivity and Specificity





  
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100

sensitivity





  
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100

specificity

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



--
Michael
http://www.dewey.myzen.co.uk/home.html



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


Hello,

Instead of computing by hand, why not use package caret?


tbl <- table(predict_testing, testing$case)
caret::sensitivity(tbl)
caret::specificity(tbl)


Hope this helps,

Rui Barradas

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






--
Prof. Dr. Matthias Kohl
www.stamats.de

__
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] Sensitivity and Specificity

2022-10-24 Thread Jin Li
Hi Greg,

This can be done by:
spm::pred.acc(testing$case,  predict_testing)

It will return both sensitivity and specificity, along with a few other
commonly used measures.

Hope this helps,
Jin

On Tue, Oct 25, 2022 at 6:01 AM Rui Barradas  wrote:

> Às 16:50 de 24/10/2022, greg holly escreveu:
> > Hi Michael,
> >
> > I appreciate your writing. Here are what I have after;
> >
> >> predict_testing <- ifelse(predict > 0.5,1,0)
> >>
> >> head(predict)
> >   1  2  3  5  7  8
> > 0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920
> >>
> >> # Sensitivity and Specificity
> >>
> >>
> >
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> > Error in predict_testing[2, 2] : incorrect number of dimensions
> >> sensitivity
> > function (data, ...)
> > {
> >  UseMethod("sensitivity")
> > }
> > 
> > 
> >>
> >>
> >
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> > Error in predict_testing[1, 1] : incorrect number of dimensions
> >> specificity
> > function (data, ...)
> > {
> >  UseMethod("specificity")
> > }
> > 
> > 
> >
> > On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey 
> > wrote:
> >
> >> Rather hard to know without seeing what output you expected and what
> >> error message you got if any but did you mean to summarise your variable
> >> predict before doing anything with it?
> >>
> >> Michael
> >>
> >> On 24/10/2022 16:17, greg holly wrote:
> >>> Hi all R-Help ,
> >>>
> >>> After partitioning my data to testing and training (please see below),
> I
> >>> need to estimate the Sensitivity and Specificity. I failed. It would be
> >>> appropriate to get your help.
> >>>
> >>> Best regards,
> >>> Greg
> >>>
> >>>
> >>> inTrain <- createDataPartition(y=data$case,
> >>>  p=0.7,
> >>>  list=FALSE)
> >>> training <- data[ inTrain,]
> >>> testing  <- data[-inTrain,]
> >>>
> >>> attach(training)
> >>> #model training and prediction
> >>> data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
> >>> training, family = binomial(link="logit"))
> >>>
> >>> predict <- predict(data_training, data_predict = testing, type =
> >> "response")
> >>>
> >>> predict_testing <- ifelse(predict > 0.5,1,0)
> >>>
> >>> # Sensitivity and Specificity
> >>>
> >>>
> >>
>  
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> >>>sensitivity
> >>>
> >>>
> >>
>  
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> >>>specificity
> >>>
> >>>[[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.
> >>>
> >>
> >> --
> >> Michael
> >> http://www.dewey.myzen.co.uk/home.html
> >>
> >
> >   [[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.
>
> Hello,
>
> Instead of computing by hand, why not use package caret?
>
>
> tbl <- table(predict_testing, testing$case)
> caret::sensitivity(tbl)
> caret::specificity(tbl)
>
>
> Hope this helps,
>
> Rui Barradas
>
> __
> 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.
>


-- 
Jin
--
Jin Li, PhD
Founder, Data2action, Australia
https://www.researchgate.net/profile/Jin_Li32
https://scholar.google.com/citations?user=Jeot53EJ=en

[[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] cannot print a list with cat

2022-10-24 Thread Steven T. Yen

Thanks to all, who have helped greatly. I essentially followed Rui to do:

  fmt_string<-paste0("\ntol = %.1e","\nreltol  = %.1e","\nsteptol = 
%.1e","\ngradtol = %.1e")
#msg<-sprintf(fmt_string,mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol) 
#works

msg<-with(mycontrol,sprintf(fmt_string,tol,reltol,steptol,gradtol))
  cat(msg)

tol = 0.0e+00
reltol  = 0.0e+00
steptol = 1.0e-08
gradtol = 1.0e-10

Thids has worked great! Thanks again to all.

Steven Yen

On 10/25/2022 3:23 AM, Rui Barradas wrote:

Às 16:21 de 24/10/2022, Steven T. Yen escreveu:
Thanks to everyone. I read ? sprint and the following is best I came 
up with. If there are ways to collapse the lines I'd be glad to know. 
Otherwise, I will live with this. Thanks again.


cat(sprintf("\ntol = %e",mycontrol$tol),
 sprintf("\nreltol  = %e",mycontrol$reltol),
 sprintf("\nsteptol = %e",mycontrol$steptol),
 sprintf("\ngradtol = %e",mycontrol$gradtol))

tol = 0.00e+00
reltol  = 0.00e+00
steptol = 1.00e-08
gradtol = 1.00e-10

On 10/24/2022 10:02 PM, Rui Barradas wrote:

Hello,

There's also ?message.


msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",

mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
message(msg)


Hope this helps,

Rui Barradas

Às 14:25 de 24/10/2022, Steven T. Yen escreveu:

Thank, Boris and Ivan.

The simple command suggested by Ivan ( print(t(mycontrol)) ) 
worked. I went along with Boris' suggestion and do/get the following:


cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))

(tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 
1.00E-08 1.00E-12


This works great. Thanks.

Steven

On 10/24/2022 9:05 PM, Boris Steipe wrote:

???  t() is the transpose function. It just happens to return your 
list unchanged. The return value is then printed to console if it 
is not assigned, or returned invisibly. Transposing your list is 
probably not what you wanted to do.


Returned values do not get printed from within a loop or from a 
source()'d script. That's why it "works" interactively, but not 
from a script file.


If you want to print the contents of your list, just use:
   print(mycontrol)

Or use some incantation with sprintf() if you want more control 
about the format of what gets printed. Eg:


  cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, 
mycontrol$reltol))


etc.


B.




On 2022-10-24, at 08:47, Ivan Krylov  wrote:

В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:


Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored.
t() doesn't print, it returns a value. In R, there's 
auto-printing in
the toplevel context (see ?withAutoprint), but not when you move 
away
from the interactive prompt. I think that it should be possible 
to use

an explicit print(t(mycontrol)) to get the behaviour you desire.

--
Best regards,
Ivan

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


Hello,

Here is a way. I leave it in may code lines to make it more 
understandale, I hope.



# From Spencer's post
(mycontrol <- list(tol=0, reltol=0, steptol=1e-8, gradtol=1e-12))
#> $tol
#> [1] 0
#>
#> $reltol
#> [1] 0
#>
#> $steptol
#> [1] 1e-08
#>
#> $gradtol
#> [1] 1e-12

fmt_string <- paste0(
  "\ntol = %e",
  "\nreltol  = %e",
  "\nsteptol = %e",
  "\ngradtol = %e"
)

msg <- sprintf(fmt_string, mycontrol$tol, mycontrol$reltol, 
mycontrol$steptol, mycontrol$gradtol)


msg
#> [1] "\ntol = 0.00e+00\nreltol  = 0.00e+00\nsteptol = 
1.00e-08\ngradtol = 1.00e-12"


cat(msg)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12

message(msg)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12


You also can write the format string all in a row.


msg2 <- with(mycontrol, sprintf("\ntol = %e\nreltol  = %e\nsteptol 
= %e\ngradtol = %e", tol, reltol, steptol, gradtol))

cat(msg2)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12


Hope this helps,

Rui Barradas



__
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 

Re: [R] cannot print a list with cat

2022-10-24 Thread Rui Barradas

Às 16:21 de 24/10/2022, Steven T. Yen escreveu:
Thanks to everyone. I read ? sprint and the following is best I came up 
with. If there are ways to collapse the lines I'd be glad to know. 
Otherwise, I will live with this. Thanks again.


cat(sprintf("\ntol = %e",mycontrol$tol),
     sprintf("\nreltol  = %e",mycontrol$reltol),
     sprintf("\nsteptol = %e",mycontrol$steptol),
     sprintf("\ngradtol = %e",mycontrol$gradtol))

tol = 0.00e+00
reltol  = 0.00e+00
steptol = 1.00e-08
gradtol = 1.00e-10

On 10/24/2022 10:02 PM, Rui Barradas wrote:

Hello,

There's also ?message.


msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",

mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
message(msg)


Hope this helps,

Rui Barradas

Às 14:25 de 24/10/2022, Steven T. Yen escreveu:

Thank, Boris and Ivan.

The simple command suggested by Ivan ( print(t(mycontrol)) ) worked. 
I went along with Boris' suggestion and do/get the following:


cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))

(tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 1.00E-08 
1.00E-12


This works great. Thanks.

Steven

On 10/24/2022 9:05 PM, Boris Steipe wrote:

???  t() is the transpose function. It just happens to return your 
list unchanged. The return value is then printed to console if it is 
not assigned, or returned invisibly. Transposing your list is 
probably not what you wanted to do.


Returned values do not get printed from within a loop or from a 
source()'d script. That's why it "works" interactively, but not from 
a script file.


If you want to print the contents of your list, just use:
   print(mycontrol)

Or use some incantation with sprintf() if you want more control 
about the format of what gets printed. Eg:


  cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, 
mycontrol$reltol))


etc.


B.




On 2022-10-24, at 08:47, Ivan Krylov  wrote:

В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:


Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored.

t() doesn't print, it returns a value. In R, there's auto-printing in
the toplevel context (see ?withAutoprint), but not when you move away
from the interactive prompt. I think that it should be possible to use
an explicit print(t(mycontrol)) to get the behaviour you desire.

--
Best regards,
Ivan

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


Hello,

Here is a way. I leave it in may code lines to make it more 
understandale, I hope.



# From Spencer's post
(mycontrol <- list(tol=0, reltol=0, steptol=1e-8, gradtol=1e-12))
#> $tol
#> [1] 0
#>
#> $reltol
#> [1] 0
#>
#> $steptol
#> [1] 1e-08
#>
#> $gradtol
#> [1] 1e-12

fmt_string <- paste0(
  "\ntol = %e",
  "\nreltol  = %e",
  "\nsteptol = %e",
  "\ngradtol = %e"
)

msg <- sprintf(fmt_string, mycontrol$tol, mycontrol$reltol, 
mycontrol$steptol, mycontrol$gradtol)


msg
#> [1] "\ntol = 0.00e+00\nreltol  = 0.00e+00\nsteptol = 
1.00e-08\ngradtol = 1.00e-12"


cat(msg)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12

message(msg)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12


You also can write the format string all in a row.


msg2 <- with(mycontrol, sprintf("\ntol = %e\nreltol  = %e\nsteptol = 
%e\ngradtol = %e", tol, reltol, steptol, gradtol))

cat(msg2)
#>
#> tol = 0.00e+00
#> reltol  = 0.00e+00
#> steptol = 1.00e-08
#> gradtol = 1.00e-12


Hope this helps,

Rui Barradas

__
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] Sensitivity and Specificity

2022-10-24 Thread Rui Barradas

Às 16:50 de 24/10/2022, greg holly escreveu:

Hi Michael,

I appreciate your writing. Here are what I have after;


predict_testing <- ifelse(predict > 0.5,1,0)

head(predict)

  1  2  3  5  7  8
0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920


# Sensitivity and Specificity



sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
Error in predict_testing[2, 2] : incorrect number of dimensions

sensitivity

function (data, ...)
{
 UseMethod("sensitivity")
}






specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
Error in predict_testing[1, 1] : incorrect number of dimensions

specificity

function (data, ...)
{
 UseMethod("specificity")
}



On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey 
wrote:


Rather hard to know without seeing what output you expected and what
error message you got if any but did you mean to summarise your variable
predict before doing anything with it?

Michael

On 24/10/2022 16:17, greg holly wrote:

Hi all R-Help ,

After partitioning my data to testing and training (please see below), I
need to estimate the Sensitivity and Specificity. I failed. It would be
appropriate to get your help.

Best regards,
Greg


inTrain <- createDataPartition(y=data$case,
 p=0.7,
 list=FALSE)
training <- data[ inTrain,]
testing  <- data[-inTrain,]

attach(training)
#model training and prediction
data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
training, family = binomial(link="logit"))

predict <- predict(data_training, data_predict = testing, type =

"response")


predict_testing <- ifelse(predict > 0.5,1,0)

# Sensitivity and Specificity



  
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100

   sensitivity



  
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100

   specificity

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



--
Michael
http://www.dewey.myzen.co.uk/home.html



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


Hello,

Instead of computing by hand, why not use package caret?


tbl <- table(predict_testing, testing$case)
caret::sensitivity(tbl)
caret::specificity(tbl)


Hope this helps,

Rui Barradas

__
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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Kelly Thompson
Andrew,
Thanks. I reviewed the code for "require" and saw:
"if (!character.only)
package <- as.character(substitute(package))"

#This helps me better understand what is going on. I am sharing this
here because I think it might help others understand.
as.character( substitute("this_pac_does_not_exist") ) #quoted

as.character( substitute( this_pac_does_not_exist ) ) #not quoted

as.character( substitute("this_pac_does_not_exist") ) == as.character(
substitute( this_pac_does_not_exist ) )

#

packages_i_want_to_use <- c("this_pac_does_not_exist", "abcz")
as.character( substitute(packages_i_want_to_use[1] ) )
packages_i_want_to_use[1]
as.character( substitute(packages_i_want_to_use[1] ) ) ==
packages_i_want_to_use[1]

#To prevent substitute(packages_i_want_to_use[1] from getting changed
to as.character( substitute(packages_i_want_to_use[1] ) ), we need to
set character.only = TRUE

On Mon, Oct 24, 2022 at 12:53 PM Andrew Simmons  wrote:
>
> In the first one, the argument is a character vector of length 1, so the code 
> works perfectly fine.
>
> The second is a call, and when coerced to a character vector should look like
>
> c("[", "packages_i_want_to_use", "1")
>
> You can try this yourself with quote(packages_i_want_to_use[1]) which returns 
> its first argument, unevaluated.
>
> On Mon, Oct 24, 2022, 12:46 Kelly Thompson  wrote:
>>
>> Thanks!
>>
>> # Please, can you help me understand why
>> require( 'base' ) # works, but
>> require( packages_i_want_to_use[1] ) # does not work?
>>
>> # In require( 'base' ), what is the "first argument"?
>>
>> On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons  wrote:
>> >
>> > require(), similarly to library(), does not evaluate its first argument 
>> > UNLESS you add character.only = TRUE
>> >
>> > require( packages_i_want_to_use[1], character.only = TRUE)
>> >
>> >
>> > On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:
>> >>
>> >> # Below, when using require(), why do I get the error message "Error
>> >> in if (!loaded) { : the condition has length > 1" ?
>> >>
>> >> # This is my reproducible code:
>> >>
>> >> #create a vector with the names of the packages I want to use
>> >> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>> >>
>> >> # Here I get error messages:
>> >> require( packages_i_want_to_use[1] )
>> >> #Error in if (!loaded) { : the condition has length > 1
>> >>
>> >> require( packages_i_want_to_use[2] )
>> >> #Error in if (!loaded) { : the condition has length > 1
>> >>
>> >> # Here I get what I expect:
>> >> require('base')
>> >>
>> >> require('this_pac_does_not_exist')
>> >> #Loading required package: this_pac_does_not_exist
>> >> #Warning message:
>> >> #In library(package, lib.loc = lib.loc, character.only = TRUE,
>> >> logical.return = TRUE,  :
>> >> #  there is no package called ‘this_pac_does_not_exist’
>> >>
>> >> __
>> >> 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] Sensitivity and Specificity

2022-10-24 Thread greg holly
THanks Michael for this.This is much appreciated. So, how can I estimate
the sensitivity and  specificity after having the prediction on testing
data. Any thoughts?

Kind regards,
Greg



On Mon, Oct 24, 2022 at 12:10 PM Michael Dewey 
wrote:

> So predict is a one-dimensional vector of predictions but you are
> treating it as a two-dimensional matrix (presumably you think those are
> the totals).
>
> Michael
>
> On 24/10/2022 16:50, greg holly wrote:
> > Hi Michael,
> >
> > I appreciate your writing. Here are what I have after;
> >
> >  > predict_testing <- ifelse(predict > 0.5,1,0)
> >  >
> >  > head(predict)
> >   1  2  3  5  7  8
> > 0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920
> >  >
> >  > # Sensitivity and Specificity
> >  >
> >  >
> >
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> > Error in predict_testing[2, 2] : incorrect number of dimensions
> >  > sensitivity
> > function (data, ...)
> > {
> >  UseMethod("sensitivity")
> > }
> > 
> > 
> >  >
> >  >
> >
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> > Error in predict_testing[1, 1] : incorrect number of dimensions
> >  > specificity
> > function (data, ...)
> > {
> >  UseMethod("specificity")
> > }
> > 
> > 
> >
> > On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey  > > wrote:
> >
> > Rather hard to know without seeing what output you expected and what
> > error message you got if any but did you mean to summarise your
> > variable
> > predict before doing anything with it?
> >
> > Michael
> >
> > On 24/10/2022 16:17, greg holly wrote:
> >  > Hi all R-Help ,
> >  >
> >  > After partitioning my data to testing and training (please see
> > below), I
> >  > need to estimate the Sensitivity and Specificity. I failed. It
> > would be
> >  > appropriate to get your help.
> >  >
> >  > Best regards,
> >  > Greg
> >  >
> >  >
> >  > inTrain <- createDataPartition(y=data$case,
> >  > p=0.7,
> >  > list=FALSE)
> >  > training <- data[ inTrain,]
> >  > testing  <- data[-inTrain,]
> >  >
> >  > attach(training)
> >  > #model training and prediction
> >  > data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
> >  > training, family = binomial(link="logit"))
> >  >
> >  > predict <- predict(data_training, data_predict = testing, type =
> > "response")
> >  >
> >  > predict_testing <- ifelse(predict > 0.5,1,0)
> >  >
> >  > # Sensitivity and Specificity
> >  >
> >  >
> >
>  
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> >  >   sensitivity
> >  >
> >  >
> >
>  
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> >  >   specificity
> >  >
> >  >   [[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.
> >  >
> >
> > --
> > Michael
> > http://www.dewey.myzen.co.uk/home.html
> > 
> >
> >
> > <
> http://www.avg.com/email-signature?utm_medium=email_source=link_campaign=sig-email_content=emailclient>
> Virus-free.www.avg.com <
> http://www.avg.com/email-signature?utm_medium=email_source=link_campaign=sig-email_content=emailclient
> >
> >
> > <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>

[[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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
In the first one, the argument is a character vector of length 1, so the
code works perfectly fine.

The second is a call, and when coerced to a character vector should look
like

c("[", "packages_i_want_to_use", "1")

You can try this yourself with quote(packages_i_want_to_use[1]) which
returns its first argument, unevaluated.

On Mon, Oct 24, 2022, 12:46 Kelly Thompson  wrote:

> Thanks!
>
> # Please, can you help me understand why
> require( 'base' ) # works, but
> require( packages_i_want_to_use[1] ) # does not work?
>
> # In require( 'base' ), what is the "first argument"?
>
> On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons 
> wrote:
> >
> > require(), similarly to library(), does not evaluate its first argument
> UNLESS you add character.only = TRUE
> >
> > require( packages_i_want_to_use[1], character.only = TRUE)
> >
> >
> > On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:
> >>
> >> # Below, when using require(), why do I get the error message "Error
> >> in if (!loaded) { : the condition has length > 1" ?
> >>
> >> # This is my reproducible code:
> >>
> >> #create a vector with the names of the packages I want to use
> >> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
> >>
> >> # Here I get error messages:
> >> require( packages_i_want_to_use[1] )
> >> #Error in if (!loaded) { : the condition has length > 1
> >>
> >> require( packages_i_want_to_use[2] )
> >> #Error in if (!loaded) { : the condition has length > 1
> >>
> >> # Here I get what I expect:
> >> require('base')
> >>
> >> require('this_pac_does_not_exist')
> >> #Loading required package: this_pac_does_not_exist
> >> #Warning message:
> >> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> >> logical.return = TRUE,  :
> >> #  there is no package called ‘this_pac_does_not_exist’
> >>
> >> __
> >> 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.
>

[[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] [EXTERNAL] Re: unexpected 'else' in " else" (Ebert, Timothy Aaron)

2022-10-24 Thread Jorgen Harmse via R-help
I agree that the documentation should be clarified. Moreover, my last example 
shows that the class can be different even when no mode coercion is required. I 
don't know enough about S3 & S4 to comment on your last point.

Regards,
Jorgen Harmse.


From: Bert Gunter 
Date: Monday, 24October, 2022 at 11:31
To: Jorgen Harmse 
Cc: r-help@r-project.org 
Subject: [EXTERNAL] Re: [R] unexpected 'else' in " else" (Ebert,Timothy Aaron)
...

So it would appear that the ifelse() documentation needs to be
clarified. For example, if the above asterisked phrase were "The S3
*class* of the answer will be inferred from the mode, where the mode
of the answer will be coerced ..." that might resolve at least that
bit of confusion However, that might also be incorrect -- what about
S4 vs S3 vs Reference classes, for example (are such cases even
possible?)? I leave resolution of these matters -- or at least their
accurate and complete documentation -- to wiser heads.

Cheers,
Bert

...

> > ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> > missing.
>
> logical(0)


[[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] Sensitivity and Specificity

2022-10-24 Thread Michael Dewey
So predict is a one-dimensional vector of predictions but you are 
treating it as a two-dimensional matrix (presumably you think those are 
the totals).


Michael

On 24/10/2022 16:50, greg holly wrote:

Hi Michael,

I appreciate your writing. Here are what I have after;

 > predict_testing <- ifelse(predict > 0.5,1,0)
 >
 > head(predict)
          1          2          3          5          7          8
0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920
 >
 > # Sensitivity and Specificity
 >
 > 
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100

Error in predict_testing[2, 2] : incorrect number of dimensions
 > sensitivity
function (data, ...)
{
     UseMethod("sensitivity")
}


 >
 > 
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100

Error in predict_testing[1, 1] : incorrect number of dimensions
 > specificity
function (data, ...)
{
     UseMethod("specificity")
}



On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey > wrote:


Rather hard to know without seeing what output you expected and what
error message you got if any but did you mean to summarise your
variable
predict before doing anything with it?

Michael

On 24/10/2022 16:17, greg holly wrote:
 > Hi all R-Help ,
 >
 > After partitioning my data to testing and training (please see
below), I
 > need to estimate the Sensitivity and Specificity. I failed. It
would be
 > appropriate to get your help.
 >
 > Best regards,
 > Greg
 >
 >
 > inTrain <- createDataPartition(y=data$case,
 >                                 p=0.7,
 >                                 list=FALSE)
 > training <- data[ inTrain,]
 > testing  <- data[-inTrain,]
 >
 > attach(training)
 > #model training and prediction
 > data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
 > training, family = binomial(link="logit"))
 >
 > predict <- predict(data_training, data_predict = testing, type =
"response")
 >
 > predict_testing <- ifelse(predict > 0.5,1,0)
 >
 > # Sensitivity and Specificity
 >
 > 
  sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100

 >   sensitivity
 >
 > 
  specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100

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

-- 
Michael

http://www.dewey.myzen.co.uk/home.html




  Virus-free.www.avg.com 


<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>


--
Michael
http://www.dewey.myzen.co.uk/home.html

__
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] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
I wanted to follow up.
A more careful reading of the following:
"A vector of the same length and attributes (including dimensions and
"class") as test..."

So the above **refers only to a "class" attribute that appears among
the attributes of test and result**. Using my previous example, note
that:

 z <- c(TRUE,TRUE,FALSE)
> attributes(z)
NULL ## so no 'class' among attributes(z)
## However
> class(z)  ## S3 class
[1] "logical"
## Similarly
> w <- ifelse(z,5,'a')
> attributes(w)
NULL ## so no 'class' among attributes(w)
> class(w)   ##S3 class
[1] "character"

So my (anyway) confusion stems from conflating the S3 'class' of the
object with a "class" attribute, of which there is none.

Nevertheless, I believe that the phrase I suggested (or something
along those lines) might clarify how the S3 class is determined and
perhaps better distinguish it from a "class" attribute among the
attributes, if there there is such. Or maybe that part of the doc
should just be removed.

My guess is that this documentation has been around for a long time
and no one has gotten around to revising it once S3 classes came into
wider use. ... or saw the need to revise it, anyway.

-- Bert





"

On Mon, Oct 24, 2022 at 9:30 AM Bert Gunter  wrote:
>
> "...but 'same length and attributes (including dimensions and
> ‘"class"’) as ‘test’' looks wrong. The output seems to be `logical` or
> something related to the classes of `yes` & `no`."
>
> The documentation in fact says:
> "A vector of the same length and attributes (including dimensions and
> "class") as test and data values from the values of yes or no. **The
> mode of the answer will be coerced from logical to accommodate first
> any values taken from yes and then any values taken from no.**
>
> So the values are taken from 'yes' and 'no' (with coercion if they are
> of different classes), and the class of the result will presumably be
> inferred from the mode of those values. e.g.
>
> > z <- c(TRUE,TRUE,FALSE)
> > class(z)
> [1] "logical"
> > w <- ifelse(z,5,'a')
> > class(w)
> [1] "character"  ## note coercion
>
> So it would appear that the ifelse() documentation needs to be
> clarified. For example, if the above asterisked phrase were "The S3
> *class* of the answer will be inferred from the mode, where the mode
> of the answer will be coerced ..." that might resolve at least that
> bit of confusion However, that might also be incorrect -- what about
> S4 vs S3 vs Reference classes, for example (are such cases even
> possible?)? I leave resolution of these matters -- or at least their
> accurate and complete documentation -- to wiser heads.
>
> Cheers,
> Bert
>
>
>
>
> On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help
>  wrote:
> >
> > There were several interesting points about `ifelse`. The usual behaviour 
> > seems to be that all three inputs are evaluated, and the entries of `yes` 
> > corresponding to `TRUE` in `test` are combined with the entries of `no` 
> > corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be 
> > recycled as necessary in case `test` is longer. On top of that, there seems 
> > to be some sugar that suppresses evaluations in case `all(test)` and/or 
> > `all(!test)`, and the return type can be `logical` even if `yes` & `no` are 
> > not. I agreed with the other responses already, but my experiments further 
> > confirmed that `ifelse` is not interchangeable with `if()  else 
> > `.
> >
> >
> >
> > The documentation confirms most of this, but 'same length and attributes 
> > (including dimensions and ‘"class"’) as ‘test’' looks wrong. The output 
> > seems to be `logical` or something related to the classes of `yes` & `no`.
> >
> >
> >
> > Regards,
> >
> > Jorgen Harmse.
> >
> >
> >
> > > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 0
> >
> > > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 0 1 2 3 4
> >
> > > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > > {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > [1] 1 2 3
> >
> > > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 
> > > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 1 2 2
> >
> > > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 
> > > 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
> >
> > Evaluating the vector for 'if'.
> >
> > Evaluating the vector for 'else'.
> >
> > [1] 1 2 2 1
> >
> > > args(ifelse)
> >
> > function (test, yes, no)
> >
> > NULL
> >
> > > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the 
> > > vector for 'if'.\n"); 

Re: [R] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Kelly Thompson
Thanks!

# Please, can you help me understand why
require( 'base' ) # works, but
require( packages_i_want_to_use[1] ) # does not work?

# In require( 'base' ), what is the "first argument"?

On Mon, Oct 24, 2022 at 12:29 PM Andrew Simmons  wrote:
>
> require(), similarly to library(), does not evaluate its first argument 
> UNLESS you add character.only = TRUE
>
> require( packages_i_want_to_use[1], character.only = TRUE)
>
>
> On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:
>>
>> # Below, when using require(), why do I get the error message "Error
>> in if (!loaded) { : the condition has length > 1" ?
>>
>> # This is my reproducible code:
>>
>> #create a vector with the names of the packages I want to use
>> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>>
>> # Here I get error messages:
>> require( packages_i_want_to_use[1] )
>> #Error in if (!loaded) { : the condition has length > 1
>>
>> require( packages_i_want_to_use[2] )
>> #Error in if (!loaded) { : the condition has length > 1
>>
>> # Here I get what I expect:
>> require('base')
>>
>> require('this_pac_does_not_exist')
>> #Loading required package: this_pac_does_not_exist
>> #Warning message:
>> #In library(package, lib.loc = lib.loc, character.only = TRUE,
>> logical.return = TRUE,  :
>> #  there is no package called ‘this_pac_does_not_exist’
>>
>> __
>> 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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Jeff Newmiller
Don't load base. It is already loaded.

On October 24, 2022 9:07:44 AM PDT, Kelly Thompson  wrote:
># Below, when using require(), why do I get the error message "Error
>in if (!loaded) { : the condition has length > 1" ?
>
># This is my reproducible code:
>
>#create a vector with the names of the packages I want to use
>packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>
># Here I get error messages:
>require( packages_i_want_to_use[1] )
>#Error in if (!loaded) { : the condition has length > 1
>
>require( packages_i_want_to_use[2] )
>#Error in if (!loaded) { : the condition has length > 1
>
># Here I get what I expect:
>require('base')
>
>require('this_pac_does_not_exist')
>#Loading required package: this_pac_does_not_exist
>#Warning message:
>#In library(package, lib.loc = lib.loc, character.only = TRUE,
>logical.return = TRUE,  :
>#  there is no package called ‘this_pac_does_not_exist’
>
>__
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Bert Gunter
Reread ?require more carefully. Especially for the  'package' argument.

Incidentally, you don't need to require base. It's always available.

-- Bert

On Mon, Oct 24, 2022 at 9:25 AM Kelly Thompson  wrote:
>
> # Below, when using require(), why do I get the error message "Error
> in if (!loaded) { : the condition has length > 1" ?
>
> # This is my reproducible code:
>
> #create a vector with the names of the packages I want to use
> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>
> # Here I get error messages:
> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1
>
> require( packages_i_want_to_use[2] )
> #Error in if (!loaded) { : the condition has length > 1
>
> # Here I get what I expect:
> require('base')
>
> require('this_pac_does_not_exist')
> #Loading required package: this_pac_does_not_exist
> #Warning message:
> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> logical.return = TRUE,  :
> #  there is no package called ‘this_pac_does_not_exist’
>
> __
> 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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Henrik Bengtsson
You need to pass character.only = TRUE to require() whenever you
specify the package using a character variable.

I agree, the error message is confusing.

/Henrik

On Mon, Oct 24, 2022 at 9:26 AM Kelly Thompson  wrote:
>
> # Below, when using require(), why do I get the error message "Error
> in if (!loaded) { : the condition has length > 1" ?
>
> # This is my reproducible code:
>
> #create a vector with the names of the packages I want to use
> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>
> # Here I get error messages:
> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1
>
> require( packages_i_want_to_use[2] )
> #Error in if (!loaded) { : the condition has length > 1
>
> # Here I get what I expect:
> require('base')
>
> require('this_pac_does_not_exist')
> #Loading required package: this_pac_does_not_exist
> #Warning message:
> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> logical.return = TRUE,  :
> #  there is no package called ‘this_pac_does_not_exist’
>
> __
> 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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Andrew Simmons
require(), similarly to library(), does not evaluate its first argument
UNLESS you add character.only = TRUE

require( packages_i_want_to_use[1], character.only = TRUE)


On Mon, Oct 24, 2022, 12:26 Kelly Thompson  wrote:

> # Below, when using require(), why do I get the error message "Error
> in if (!loaded) { : the condition has length > 1" ?
>
> # This is my reproducible code:
>
> #create a vector with the names of the packages I want to use
> packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')
>
> # Here I get error messages:
> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1
>
> require( packages_i_want_to_use[2] )
> #Error in if (!loaded) { : the condition has length > 1
>
> # Here I get what I expect:
> require('base')
>
> require('this_pac_does_not_exist')
> #Loading required package: this_pac_does_not_exist
> #Warning message:
> #In library(package, lib.loc = lib.loc, character.only = TRUE,
> logical.return = TRUE,  :
> #  there is no package called ‘this_pac_does_not_exist’
>
> __
> 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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Ivan Krylov
В Mon, 24 Oct 2022 12:07:44 -0400
Kelly Thompson  пишет:

> require( packages_i_want_to_use[1] )
> #Error in if (!loaded) { : the condition has length > 1

This seems to be a bug in require(). In addition to understanding
character strings as arguments, require() can load packages named by
unquoted barewords:

require(base)

This uses non-standard evaluation, and the part that transforms an
unquoted symbol into a package name doesn't expect to see what is
effectively `[`(packages_i_want_to_use, 1) instead of a single symbol.

If you pass the character.only = TRUE argument to require(), this
additional handling is disabled, making your examples work again. It's
a good idea to always pass this argument when calling require() with a
variable as the first argument.

-- 
Best regards,
Ivan

__
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] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Bert Gunter
"...but 'same length and attributes (including dimensions and
‘"class"’) as ‘test’' looks wrong. The output seems to be `logical` or
something related to the classes of `yes` & `no`."

The documentation in fact says:
"A vector of the same length and attributes (including dimensions and
"class") as test and data values from the values of yes or no. **The
mode of the answer will be coerced from logical to accommodate first
any values taken from yes and then any values taken from no.**

So the values are taken from 'yes' and 'no' (with coercion if they are
of different classes), and the class of the result will presumably be
inferred from the mode of those values. e.g.

> z <- c(TRUE,TRUE,FALSE)
> class(z)
[1] "logical"
> w <- ifelse(z,5,'a')
> class(w)
[1] "character"  ## note coercion

So it would appear that the ifelse() documentation needs to be
clarified. For example, if the above asterisked phrase were "The S3
*class* of the answer will be inferred from the mode, where the mode
of the answer will be coerced ..." that might resolve at least that
bit of confusion However, that might also be incorrect -- what about
S4 vs S3 vs Reference classes, for example (are such cases even
possible?)? I leave resolution of these matters -- or at least their
accurate and complete documentation -- to wiser heads.

Cheers,
Bert




On Mon, Oct 24, 2022 at 8:45 AM Jorgen Harmse via R-help
 wrote:
>
> There were several interesting points about `ifelse`. The usual behaviour 
> seems to be that all three inputs are evaluated, and the entries of `yes` 
> corresponding to `TRUE` in `test` are combined with the entries of `no` 
> corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be 
> recycled as necessary in case `test` is longer. On top of that, there seems 
> to be some sugar that suppresses evaluations in case `all(test)` and/or 
> `all(!test)`, and the return type can be `logical` even if `yes` & `no` are 
> not. I agreed with the other responses already, but my experiments further 
> confirmed that `ifelse` is not interchangeable with `if()  else `.
>
>
>
> The documentation confirms most of this, but 'same length and attributes 
> (including dimensions and ‘"class"’) as ‘test’' looks wrong. The output seems 
> to be `logical` or something related to the classes of `yes` & `no`.
>
>
>
> Regards,
>
> Jorgen Harmse.
>
>
>
> > ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'else'.
>
> [1] 0
>
> > ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'else'.
>
> [1] 0 1 2 3 4
>
> > ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> [1] 1 2 3
>
> > ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2
>
> > ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); 
> > 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2 1
>
> > args(ifelse)
>
> function (test, yes, no)
>
> NULL
>
> > ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector 
> > for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> Evaluating the vector for 'if'.
>
> Evaluating the vector for 'else'.
>
> [1] 1 2 2 1 2 0 1
>
> > ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> > {cat("Evaluating the vector for 'else'.\n"); 0:4})
>
> logical(0)
>
> > ifelse(TRUE, integer(0L), numeric(0L))
>
> [1] NA
>
> > class(ifelse(TRUE, integer(0L), numeric(0L)))
>
> [1] "integer"
>
> > ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> > missing.
>
> logical(0)
>
>
>
>
>
>
>
>
> [[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] When using require(), why do I get the error message "Error in if (!loaded) { : the condition has length > 1" ?

2022-10-24 Thread Kelly Thompson
# Below, when using require(), why do I get the error message "Error
in if (!loaded) { : the condition has length > 1" ?

# This is my reproducible code:

#create a vector with the names of the packages I want to use
packages_i_want_to_use <- c('base', 'this_pac_does_not_exist')

# Here I get error messages:
require( packages_i_want_to_use[1] )
#Error in if (!loaded) { : the condition has length > 1

require( packages_i_want_to_use[2] )
#Error in if (!loaded) { : the condition has length > 1

# Here I get what I expect:
require('base')

require('this_pac_does_not_exist')
#Loading required package: this_pac_does_not_exist
#Warning message:
#In library(package, lib.loc = lib.loc, character.only = TRUE,
logical.return = TRUE,  :
#  there is no package called ‘this_pac_does_not_exist’

__
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] Sensitivity and Specificity

2022-10-24 Thread Michael Dewey
Rather hard to know without seeing what output you expected and what 
error message you got if any but did you mean to summarise your variable 
predict before doing anything with it?


Michael

On 24/10/2022 16:17, greg holly wrote:

Hi all R-Help ,

After partitioning my data to testing and training (please see below), I
need to estimate the Sensitivity and Specificity. I failed. It would be
appropriate to get your help.

Best regards,
Greg


inTrain <- createDataPartition(y=data$case,
p=0.7,
list=FALSE)
training <- data[ inTrain,]
testing  <- data[-inTrain,]

attach(training)
#model training and prediction
data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
training, family = binomial(link="logit"))

predict <- predict(data_training, data_predict = testing, type = "response")

predict_testing <- ifelse(predict > 0.5,1,0)

# Sensitivity and Specificity

  
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
  sensitivity

  
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
  specificity

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



--
Michael
http://www.dewey.myzen.co.uk/home.html

__
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] Sensitivity and Specificity

2022-10-24 Thread greg holly
Hi Michael,

I appreciate your writing. Here are what I have after;

> predict_testing <- ifelse(predict > 0.5,1,0)
>
> head(predict)
 1  2  3  5  7  8
0.29006984 0.28370507 0.10761993 0.02204224 0.12873872 0.08127920
>
> # Sensitivity and Specificity
>
>
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
Error in predict_testing[2, 2] : incorrect number of dimensions
> sensitivity
function (data, ...)
{
UseMethod("sensitivity")
}


>
>
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
Error in predict_testing[1, 1] : incorrect number of dimensions
> specificity
function (data, ...)
{
UseMethod("specificity")
}



On Mon, Oct 24, 2022 at 10:45 AM Michael Dewey 
wrote:

> Rather hard to know without seeing what output you expected and what
> error message you got if any but did you mean to summarise your variable
> predict before doing anything with it?
>
> Michael
>
> On 24/10/2022 16:17, greg holly wrote:
> > Hi all R-Help ,
> >
> > After partitioning my data to testing and training (please see below), I
> > need to estimate the Sensitivity and Specificity. I failed. It would be
> > appropriate to get your help.
> >
> > Best regards,
> > Greg
> >
> >
> > inTrain <- createDataPartition(y=data$case,
> > p=0.7,
> > list=FALSE)
> > training <- data[ inTrain,]
> > testing  <- data[-inTrain,]
> >
> > attach(training)
> > #model training and prediction
> > data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
> > training, family = binomial(link="logit"))
> >
> > predict <- predict(data_training, data_predict = testing, type =
> "response")
> >
> > predict_testing <- ifelse(predict > 0.5,1,0)
> >
> > # Sensitivity and Specificity
> >
> >
>  
> sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
> >   sensitivity
> >
> >
>  
> specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
> >   specificity
> >
> >   [[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.
> >
>
> --
> Michael
> http://www.dewey.myzen.co.uk/home.html
>

[[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] cannot print a list with cat

2022-10-24 Thread Bert Gunter
"collapse the lines" means ??

If you mean that you want to control the precision (# of decimals
places to show) then that is exactly what sprintf does. ?sprintf tells
you how. If you mean something else, please specify more clearly -- or
await a reply from someone with greater insight than I.

-- Bert

On Mon, Oct 24, 2022 at 8:28 AM Steven T. Yen  wrote:
>
> Thanks to everyone. I read ? sprint and the following is best I came up
> with. If there are ways to collapse the lines I'd be glad to know.
> Otherwise, I will live with this. Thanks again.
>
> cat(sprintf("\ntol = %e",mycontrol$tol),
>  sprintf("\nreltol  = %e",mycontrol$reltol),
>  sprintf("\nsteptol = %e",mycontrol$steptol),
>  sprintf("\ngradtol = %e",mycontrol$gradtol))
>
> tol = 0.00e+00
> reltol  = 0.00e+00
> steptol = 1.00e-08
> gradtol = 1.00e-10
>
> On 10/24/2022 10:02 PM, Rui Barradas wrote:
> > Hello,
> >
> > There's also ?message.
> >
> >
> > msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",
> >
> > mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
> > message(msg)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Às 14:25 de 24/10/2022, Steven T. Yen escreveu:
> >> Thank, Boris and Ivan.
> >>
> >> The simple command suggested by Ivan ( print(t(mycontrol)) ) worked.
> >> I went along with Boris' suggestion and do/get the following:
> >>
> >> cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
> >> mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))
> >>
> >> (tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 1.00E-08
> >> 1.00E-12
> >>
> >> This works great. Thanks.
> >>
> >> Steven
> >>
> >> On 10/24/2022 9:05 PM, Boris Steipe wrote:
> >>
> >>> ???  t() is the transpose function. It just happens to return your
> >>> list unchanged. The return value is then printed to console if it is
> >>> not assigned, or returned invisibly. Transposing your list is
> >>> probably not what you wanted to do.
> >>>
> >>> Returned values do not get printed from within a loop or from a
> >>> source()'d script. That's why it "works" interactively, but not from
> >>> a script file.
> >>>
> >>> If you want to print the contents of your list, just use:
> >>>print(mycontrol)
> >>>
> >>> Or use some incantation with sprintf() if you want more control
> >>> about the format of what gets printed. Eg:
> >>>
> >>>   cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol,
> >>> mycontrol$reltol))
> >>>
> >>> etc.
> >>>
> >>>
> >>> B.
> >>>
> >>>
> >>>
>  On 2022-10-24, at 08:47, Ivan Krylov  wrote:
> 
>  В Mon, 24 Oct 2022 20:39:33 +0800
>  "Steven T. Yen"  пишет:
> 
> > Printing this in a main program causes no problem (as shown above).
> > But, using the command t(mycontrol) the line gets ignored.
>  t() doesn't print, it returns a value. In R, there's auto-printing in
>  the toplevel context (see ?withAutoprint), but not when you move away
>  from the interactive prompt. I think that it should be possible to use
>  an explicit print(t(mycontrol)) to get the behaviour you desire.
> 
>  --
>  Best regards,
>  Ivan
> 
>  __
>  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] unexpected 'else' in " else" (Ebert,Timothy Aaron)

2022-10-24 Thread Jorgen Harmse via R-help
There were several interesting points about `ifelse`. The usual behaviour seems 
to be that all three inputs are evaluated, and the entries of `yes` 
corresponding to `TRUE` in `test` are combined with the entries of `no` 
corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be recycled 
as necessary in case `test` is longer. On top of that, there seems to be some 
sugar that suppresses evaluations in case `all(test)` and/or `all(!test)`, and 
the return type can be `logical` even if `yes` & `no` are not. I agreed with 
the other responses already, but my experiments further confirmed that `ifelse` 
is not interchangeable with `if()  else `.



The documentation confirms most of this, but 'same length and attributes 
(including dimensions and �"class"�) as �test�' looks wrong. The output seems 
to be `logical` or something related to the classes of `yes` & `no`.



Regards,

Jorgen Harmse.



> ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0

> ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0 1 2 3 4

> ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

[1] 1 2 3

> ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2

> ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); 
> 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1

> args(ifelse)

function (test, yes, no)

NULL

> ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector 
> for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1 2 0 1

> ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

logical(0)

> ifelse(TRUE, integer(0L), numeric(0L))

[1] NA

> class(ifelse(TRUE, integer(0L), numeric(0L)))

[1] "integer"

> ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> missing.

logical(0)








[[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] cannot print a list with cat

2022-10-24 Thread Steven T. Yen
Thanks to everyone. I read ? sprint and the following is best I came up 
with. If there are ways to collapse the lines I'd be glad to know. 
Otherwise, I will live with this. Thanks again.


cat(sprintf("\ntol = %e",mycontrol$tol),
    sprintf("\nreltol  = %e",mycontrol$reltol),
    sprintf("\nsteptol = %e",mycontrol$steptol),
    sprintf("\ngradtol = %e",mycontrol$gradtol))

tol = 0.00e+00
reltol  = 0.00e+00
steptol = 1.00e-08
gradtol = 1.00e-10

On 10/24/2022 10:02 PM, Rui Barradas wrote:

Hello,

There's also ?message.


msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",

mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
message(msg)


Hope this helps,

Rui Barradas

Às 14:25 de 24/10/2022, Steven T. Yen escreveu:

Thank, Boris and Ivan.

The simple command suggested by Ivan ( print(t(mycontrol)) ) worked. 
I went along with Boris' suggestion and do/get the following:


cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))

(tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 1.00E-08 
1.00E-12


This works great. Thanks.

Steven

On 10/24/2022 9:05 PM, Boris Steipe wrote:

???  t() is the transpose function. It just happens to return your 
list unchanged. The return value is then printed to console if it is 
not assigned, or returned invisibly. Transposing your list is 
probably not what you wanted to do.


Returned values do not get printed from within a loop or from a 
source()'d script. That's why it "works" interactively, but not from 
a script file.


If you want to print the contents of your list, just use:
   print(mycontrol)

Or use some incantation with sprintf() if you want more control 
about the format of what gets printed. Eg:


  cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, 
mycontrol$reltol))


etc.


B.




On 2022-10-24, at 08:47, Ivan Krylov  wrote:

В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:


Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored.

t() doesn't print, it returns a value. In R, there's auto-printing in
the toplevel context (see ?withAutoprint), but not when you move away
from the interactive prompt. I think that it should be possible to use
an explicit print(t(mycontrol)) to get the behaviour you desire.

--
Best regards,
Ivan

__
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] Sensitivity and Specificity

2022-10-24 Thread greg holly
Hi all R-Help ,

After partitioning my data to testing and training (please see below), I
need to estimate the Sensitivity and Specificity. I failed. It would be
appropriate to get your help.

Best regards,
Greg


inTrain <- createDataPartition(y=data$case,
   p=0.7,
   list=FALSE)
training <- data[ inTrain,]
testing  <- data[-inTrain,]

attach(training)
#model training and prediction
data_training <- glm(case ~ age+BMI+Calcium+Albumin+meno_1, data =
training, family = binomial(link="logit"))

predict <- predict(data_training, data_predict = testing, type = "response")

predict_testing <- ifelse(predict > 0.5,1,0)

# Sensitivity and Specificity

 
sensitivity<-(predict_testing[2,2]/(predict_testing[2,2]+predict_testing[2,1]))*100
 sensitivity

 
specificity<-(predict_testing[1,1]/(predict_testing[1,1]+predict_testing[1,2]))*100
 specificity

[[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] cannot print a list with cat

2022-10-24 Thread Rui Barradas

Hello,

There's also ?message.


msg <- sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",

mycontrol$tol,mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol)
message(msg)


Hope this helps,

Rui Barradas

Às 14:25 de 24/10/2022, Steven T. Yen escreveu:

Thank, Boris and Ivan.

The simple command suggested by Ivan ( print(t(mycontrol)) ) worked. I 
went along with Boris' suggestion and do/get the following:


cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))

(tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 1.00E-08 
1.00E-12


This works great. Thanks.

Steven

On 10/24/2022 9:05 PM, Boris Steipe wrote:

???  t() is the transpose function. It just happens to return your 
list unchanged. The return value is then printed to console if it is 
not assigned, or returned invisibly. Transposing your list is probably 
not what you wanted to do.


Returned values do not get printed from within a loop or from a 
source()'d script. That's why it "works" interactively, but not from a 
script file.


If you want to print the contents of your list, just use:
   print(mycontrol)

Or use some incantation with sprintf() if you want more control about 
the format of what gets printed. Eg:


  cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, mycontrol$reltol))

etc.


B.




On 2022-10-24, at 08:47, Ivan Krylov  wrote:

В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:


Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored.

t() doesn't print, it returns a value. In R, there's auto-printing in
the toplevel context (see ?withAutoprint), but not when you move away
from the interactive prompt. I think that it should be possible to use
an explicit print(t(mycontrol)) to get the behaviour you desire.

--
Best regards,
Ivan

__
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] cannot print a list with cat

2022-10-24 Thread Spencer Graves




On 10/24/22 7:39 AM, Steven T. Yen wrote:

I have a "list" containing four elements, as shown below:

 > t(mycontrol)

  tol reltol steptol gradtol
[1,] 0   0  1e-08   1e-12

Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored. Any idea? 



I'm confused.  I get:


> (mycontrol <- list(tol=0, reltol=0,
+ steptol=1e-8, gradtol=1e-12))
$tol
[1] 0

$reltol
[1] 0

$steptol
[1] 1e-08

$gradtol
[1] 1e-12

>
> t(mycontrol)
 tol reltol steptol gradtol
[1,] 0   0  1e-08   1e-12


	  I don't know what you mean by "main program" vs. "the command 
t(mycontrol)".



  ???
  Spencer Graves


> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.7

Matrix products: default
LAPACK: 
/Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib


locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

loaded via a namespace (and not attached):
[1] compiler_4.2.1 tools_4.2.1



Thanks.
Steven Yen

__
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] cannot print a list with cat

2022-10-24 Thread Steven T. Yen

Thank, Boris and Ivan.

The simple command suggested by Ivan ( print(t(mycontrol)) ) worked. I 
went along with Boris' suggestion and do/get the following:


cat(sprintf("(tol,reltol,steptol,gradtol): %E %E %E %E",mycontrol$tol,
mycontrol$reltol,mycontrol$steptol,mycontrol$gradtol))

(tol,reltol,steptol,gradtol): 0.00E+00 0.00E+00 1.00E-08 
1.00E-12


This works great. Thanks.

Steven

On 10/24/2022 9:05 PM, Boris Steipe wrote:


???  t() is the transpose function. It just happens to return your list 
unchanged. The return value is then printed to console if it is not assigned, 
or returned invisibly. Transposing your list is probably not what you wanted to 
do.

Returned values do not get printed from within a loop or from a source()'d script. That's 
why it "works" interactively, but not from a script file.

If you want to print the contents of your list, just use:
   print(mycontrol)

Or use some incantation with sprintf() if you want more control about the 
format of what gets printed. Eg:

  cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, mycontrol$reltol))

etc.


B.




On 2022-10-24, at 08:47, Ivan Krylov  wrote:

В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:


Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored.

t() doesn't print, it returns a value. In R, there's auto-printing in
the toplevel context (see ?withAutoprint), but not when you move away
from the interactive prompt. I think that it should be possible to use
an explicit print(t(mycontrol)) to get the behaviour you desire.

--
Best regards,
Ivan

__
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] cannot print a list with cat

2022-10-24 Thread Boris Steipe
???  t() is the transpose function. It just happens to return your list 
unchanged. The return value is then printed to console if it is not assigned, 
or returned invisibly. Transposing your list is probably not what you wanted to 
do.

Returned values do not get printed from within a loop or from a source()'d 
script. That's why it "works" interactively, but not from a script file.

If you want to print the contents of your list, just use:
  print(mycontrol)

Or use some incantation with sprintf() if you want more control about the 
format of what gets printed. Eg:

 cat(sprintf("Tolerance: %f (%f %%)", mycontrol$tol, mycontrol$reltol))

etc.


B.



> On 2022-10-24, at 08:47, Ivan Krylov  wrote:
> 
> В Mon, 24 Oct 2022 20:39:33 +0800
> "Steven T. Yen"  пишет:
> 
>> Printing this in a main program causes no problem (as shown above).
>> But, using the command t(mycontrol) the line gets ignored.
> 
> t() doesn't print, it returns a value. In R, there's auto-printing in
> the toplevel context (see ?withAutoprint), but not when you move away
> from the interactive prompt. I think that it should be possible to use
> an explicit print(t(mycontrol)) to get the behaviour you desire.
> 
> -- 
> Best regards,
> Ivan
> 
> __
> 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] cannot print a list with cat

2022-10-24 Thread Ivan Krylov
В Mon, 24 Oct 2022 20:39:33 +0800
"Steven T. Yen"  пишет:

> Printing this in a main program causes no problem (as shown above).
> But, using the command t(mycontrol) the line gets ignored.

t() doesn't print, it returns a value. In R, there's auto-printing in
the toplevel context (see ?withAutoprint), but not when you move away
from the interactive prompt. I think that it should be possible to use
an explicit print(t(mycontrol)) to get the behaviour you desire.

-- 
Best regards,
Ivan

__
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] cannot print a list with cat

2022-10-24 Thread Steven T. Yen

I have a "list" containing four elements, as shown below:

> t(mycontrol)

 tol reltol steptol gradtol
[1,] 0   0  1e-08   1e-12

Printing this in a main program causes no problem (as shown above).
But, using the command t(mycontrol) the line gets ignored. Any idea? Thanks.
Steven Yen

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