Re: [R] refresh.console() function?

2024-03-13 Thread Deepayan Sarkar
Perhaps you mean

https://search.r-project.org/R/refmans/utils/html/flush.console.html

Best,
Deepayan


On Wed, 13 Mar, 2024, 8:59 pm Christofer Bogaso, <
bogaso.christo...@gmail.com> wrote:

> Hi,
>
> I run a lengthy for loop and I want to display loop status for each
> step in my loop.
>
> I previously heard of a R function namely refresh.console() which
> would print the status within the loop as it progresses.
>
> However I see this
>
> > help.search("refresh.console")
>
> No vignettes or demos or help files found with alias or concept or
>
> title matching ‘refresh.console’ using regular expression matching.
>
> Could you please help me find the correct function name?
>
> __
> 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] refresh.console() function?

2024-03-13 Thread Enrico Schumann
On Wed, 13 Mar 2024, Christofer Bogaso writes:

> Hi,
>
> I run a lengthy for loop and I want to display loop status for each
> step in my loop.
>
> I previously heard of a R function namely refresh.console() which
> would print the status within the loop as it progresses.
>
> However I see this
>
>> help.search("refresh.console")
>
> No vignettes or demos or help files found with alias or concept or
>
> title matching ‘refresh.console’ using regular expression matching.
>
> Could you please help me find the correct function name?
>

?flush.console

... but you'll need to print/message/cat/... explicitly in the
loop, or the output won't be shown.  [Also, options(warn=1) might
be useful.]


-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net

__
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] Function environments serialize to a lot of data until they don't

2024-03-11 Thread Ivan Krylov via R-help
Dear Luke,

Thank you for the detailed explanation of the power of force()! It does
solve my problem in a much more reliable manner than setting function
environments manually.

On Fri, 8 Mar 2024 15:46:52 -0600 (CST)
luke-tier...@uiowa.edu wrote:

> Having a reference to a large environment is not much of an issue
> within a single process, but can be in a distributed memory parallel
> computing context.  To avoid this you can force evaluation of the
> promises:
> 
>  mkLL1 <- function(m, s) {
>   force(m)
>   force(s)
>   function(x) sum(dnorm(x, m, s, log = TRUE))
>  }
>  ll <- f(1e7)
>  length(serialize(ll, NULL))
>  ## [1] 2146

I think this also illustrates the danger of letting side effects come
near function arguments. A promise to read a file could survive on a
cluster node and result in a lot of head-scratching. A promise to write
to the connection number N, which coincides with a connection open on
the cluster node, could even do some damage. This is definitely
something to remember when creating closures.

> A very simple tool available in the snow package for snow clusters is
> snow.time(), which can produce some summary times and a Gantt chart
> (patterned after ones produced by xpvm and xmpi).

I can see the snow.time() plot being useful. Thank you for letting me
know about it!

-- 
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] [External] Function environments serialize to a lot of data until they don't

2024-03-08 Thread luke-tierney--- via R-help

On Fri, 8 Mar 2024, Ivan Krylov via R-help wrote:


Hello R-help,

I've noticed that my 'parallel' jobs take too much memory to store and
transfer to the cluster workers. I've managed to trace it to the
following:

# `payload` is being written to the cluster worker.
# The function FUN had been created as a closure inside my package:
payload$data$args$FUN
# function (l, ...)
# withCallingHandlers(fun(l$x, ...), error = .wraperr(l$name))
# 
# 

# The function seems to bring a lot of captured data with it.
e <- environment(payload$data$args$FUN)
length(serialize(e, NULL))
# [1] 738202878
parent.env(e)
# 

# The parent environment has a name, so it all must be right here.
# What is it?

ls(e, all.names = TRUE)
# [1] "fun"
length(serialize(e$fun, NULL))
# [1] 317

# The only object in the environment is small!
# Where is the 700 megabytes of data?

length(serialize(e, NULL))
# [1] 536
length(serialize(payload$data$args$FUN, NULL))
# [1] 1722

And once I've observed `fun`, the environment becomes very small and
now can be serialized in a very compact manner.

I managed to work around it by forcing the promise and explicitly
putting `fun` in a small environment when constructing the closure:

.wrapfun <- function(fun) {
e <- new.env(parent = loadNamespace('mypackage'))
e$fun <- fun
# NOTE: a naive return(function(...)) could serialize to 700
# megabytes due to `fun` seemingly being a promise (?). Once the
# promise is resolved, suddenly `fun` is much more compact.
ret <- function(l, ...) withCallingHandlers(
 fun(l$x, ...),
 error = .wraperr(l$name)
)
environment(ret) <- e
ret
}


Creating and setting environments is brittle and easy to get wrong. I
prefer to use a combination of proper lexical scoping, regular
assignments, and force() as I do below.


Is this analysis correct? Could a simple f <- force(fun) have sufficed?
Where can I read more about this type of problems?


Just force(fun), without the assignment, should be enough, or even
just fun, as in

   function(fun) { fun;  }

Using force() make the intent clearer.

Closures or formulas capturing large amount of data is something you
have to be careful about with serialization in general and distributed
memory computing in R in particular. There is a little on it in the
parallel vignette. I know I have talked and written about it in
various places but can't remember a specific reference right now.

I usually define a top level function to create any closures I want to
transmit and make sure they only capture what they need. A common
pattern is provided by a simple function for creating a normal
log-likelihood:

mkLL <- function(x) {
m <- mean(x)
s <- sd(x)
function(y) sum(dnorm(y, m, s, log = TRUE))
}

This avoids recomputing the mean and sd on every call. It is fine for
use within a single process, and the fact that the original data is
available in the environment might even be useful for debugging:

ll <- mkLL(rnorm(10))
environment(ll)$x
##  [1] -0.09202453  0.78901912 -0.66744232  1.36061149  1.50768816
##  [6] -2.60754997  0.68727212  0.31557476  2.02027688 -1.42361769

But it does prevent the data from being garbage-collected until
the returned result is no longer reachable. A more GC-friendly, and
serialization-friendly definition is

mkLL <- function(x) {
m <- mean(x)
s <- sd(x)
x <- NULL  ## not needed anymore; remove from the result's enclosing env
function(y) sum(dnorm(y, m, s, log = TRUE))
}

ll <- mkLL(rnorm(1e7))
length(serialize(ll, NULL))
## [1] 734

If you prefer to calculate the mean and sd yourself you could use

mkLL1 <- function(m, s) function(x) sum(dnorm(x, m, s, log = TRUE))

Until the result is called for the first time the evaluation of the
arguments will be delayed, i.e. encoded in promises that record the
expression to evaluate and the environment in which to evaluate
it:

f <- function(n) {
x <- rnorm(n)
mkLL1(mean(x), sd(x))
}
ll <- f(1e7)
length(serialize(ll, NULL))
## [1] 80002223

Once the arguments are evaluated, the expressions are still needed for
substitute() and such, but the environment is not, so it is dropped,
and if the promise environment can no longer be reached it can be
garbage-collected, It will also no longer appear in a serialization:

ll(1)
## [1] -1.419588
length(serialize(ll, NULL))
## [1] 3537

Having a reference to a large environment is not much of an issue
within a single process, but can be in a distributed memory parallel
computing context.  To avoid this you can force evaluation of the
promises:

mkLL1 <- function(m, s) {
force(m)
force(s)
function(x) sum(dnorm(x, m, s, log = TRUE))
}
ll <- f(1e7)
length(serialize(ll, NULL))
## [1] 2146

The possibility of inadvertently transferring too much data is an
issue in distributed memory computing in general, so there are various
tools that help. A very 

Re: [R] Capturing Function Arguments

2024-02-19 Thread Reed A. Cartwright
Thanks Ivan and Iris for your solutions, I'll look over them.

The solution that I came up with last night involves creating a
function that has the same formals signature as the wrapped function
and relying on `environment()` and `list(...)` to return a function's
variables at the beginning of the call. Since some default variables
can't be successfully created at the beginning of the function, I
manually clean up the formals as needed so that
`as.list(environment())` won't fail. I'm thinking about returning a
copy of the environment instead of a list to avoid having to clean up
the formals, as I can selectively ignore problematic arguments later
instead of modifying the signature ahead of time.

Thanks again for the suggestions,
Reed

```
capture <- function(fun, env = parent.frame()) {
fun_name <- deparse(substitute(fun))
# extract formal arguments and append ... if needed
fmls <- formals(fun)
if(! "..." %in% names(fmls)) {
fmls <- c(fmls, alist("..." = ))
}

# construct wrapped function
f <- function() TRUE
formals(f) <- fmls
body(f) <- bquote({
args <- as.list(environment())
args <- c(args, list(...))
mc <- match.call()
mc[[1L]] <- quote(.(substitute(fun)))
res <- eval.parent(mc)
out <- list(fun = .(fun_name),
args = args,
result = res
)
invisible(structure(out, class="function_call"))
})
environment(f) <- env
return(f)
}

# example usage
hist <- capture(graphics::hist.default)
formals(hist) <- c(formals(hist), list(xname = NA_character_))
```

On Sun, Feb 18, 2024 at 4:17 AM Ivan Krylov  wrote:
>
> В Sat, 17 Feb 2024 11:15:43 -0700
> "Reed A. Cartwright"  пишет:
>
> > I'm wrapping a function in R and I want to record all the arguments
> > passed to it, including default values and missing values.
>
> This is hard if not impossible to implement for the general case
> because the default arguments are evaluated in the environment of the
> function as it is running:
>
> f0 <- function(arg = frobnicate()) {
>  frobnicate <- switch(
>   sample.int(3, 1),
>   function() environment(),
>   function(n=1) runif(n),
>   function() alist(a=)$a
>  )
>  arg
> }
>
> (And some arguments aren't meant to be evaluated at all.)
>
> Even starting with rlang::call_match(call = NULL, defaults = TRUE) is
> doomed to a certain extent because it gives you f(x = a, a = NULL) for
> both function(x, a = NULL), f(a) (where `a` passed as an argument `x`
> and `a` should be taken from the parent frame) and function(x = a, a =
> NULL), f() (in which case `x` defaults to `a`, which in turn defaults
> to NULL).
>
> I think the key here is evaluating the arguments first, then matching.
> This makes a lot of assumptions about the function being inspected: no
> NSE, no ellipsis, formals don't depend on the body, nothing weird about
> the environment of the function...
>
> f <- function(...) {
>  .makemissing <- function() alist(a=)$a
>  .ismissing <- function(x) identical(x, .makemissing())
>
>  # prepare to evaluate formals
>  params <- formals(f0)
>  e <- new.env(parent = environment(f0))
>  # assign non-missing formals
>  for (n in names(params)) if (!.ismissing(params[[n]])) eval(
>   # work around delayedAssign quoting its second argument
>   call('delayedAssign', n, params[[n]], e, e)
>  )
>
>  # match the evaluated arguments against the names of the formals
>  args <- as.list(match.call(f0, as.call(c('f0', list(...)[-1]
>  for (n in names(args)) assign(n, args[[n]], envir = e)
>
>  # evaluate everything, default argument or not
>  mget(names(params), e, ifnotfound = list(.makemissing()))
> }
>
> f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
> a <- 1
> identical(
>  f(a, z = 1 + 100),
>  list(x = 1, y = 202, z = 101, a = NULL, b = rlang::missing_arg())
> )
> # [1] TRUE
>
> --
> 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] Capturing Function Arguments

2024-02-18 Thread Ivan Krylov via R-help
В Sat, 17 Feb 2024 11:15:43 -0700
"Reed A. Cartwright"  пишет:

> I'm wrapping a function in R and I want to record all the arguments
> passed to it, including default values and missing values.

This is hard if not impossible to implement for the general case
because the default arguments are evaluated in the environment of the
function as it is running:

f0 <- function(arg = frobnicate()) {
 frobnicate <- switch(
  sample.int(3, 1),
  function() environment(),
  function(n=1) runif(n),
  function() alist(a=)$a
 )
 arg
}

(And some arguments aren't meant to be evaluated at all.)

Even starting with rlang::call_match(call = NULL, defaults = TRUE) is
doomed to a certain extent because it gives you f(x = a, a = NULL) for
both function(x, a = NULL), f(a) (where `a` passed as an argument `x`
and `a` should be taken from the parent frame) and function(x = a, a =
NULL), f() (in which case `x` defaults to `a`, which in turn defaults
to NULL).

I think the key here is evaluating the arguments first, then matching.
This makes a lot of assumptions about the function being inspected: no
NSE, no ellipsis, formals don't depend on the body, nothing weird about
the environment of the function...

f <- function(...) {
 .makemissing <- function() alist(a=)$a
 .ismissing <- function(x) identical(x, .makemissing())

 # prepare to evaluate formals
 params <- formals(f0)
 e <- new.env(parent = environment(f0))
 # assign non-missing formals
 for (n in names(params)) if (!.ismissing(params[[n]])) eval(
  # work around delayedAssign quoting its second argument
  call('delayedAssign', n, params[[n]], e, e)
 )

 # match the evaluated arguments against the names of the formals
 args <- as.list(match.call(f0, as.call(c('f0', list(...)[-1]
 for (n in names(args)) assign(n, args[[n]], envir = e)

 # evaluate everything, default argument or not
 mget(names(params), e, ifnotfound = list(.makemissing()))
}

f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
a <- 1
identical(
 f(a, z = 1 + 100),
 list(x = 1, y = 202, z = 101, a = NULL, b = rlang::missing_arg())
)
# [1] TRUE

-- 
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] Capturing Function Arguments

2024-02-18 Thread Iris Simmons
Hi Reed,


I need to stress before giving my answer that no solution can handle
everything. These scenarios will always lead to problems:

* if any of the formal arguments rely on the current state of the call stack

* if any of the formal arguments rely on a variable that is only
defined later in the body of the function

That being said, this function does well to handle other scenarios:

```R
f <- function (...)
{
## replace f0 as needed
wrapped_function <- f0
call <- match.call(wrapped_function, expand.dots = FALSE)
args <- as.list(call)[-1L]
e <- new.env(parent = environment(wrapped_function))
parent_frame <- parent.frame()
formal_args <- formals(wrapped_function)
for (sym in names(formal_args)) {
## if the formal argument is one of the provided arguments
if (i <- match(sym, names(args), 0L)) {
## if the argument is missing, assign as is
if (identical(args[[i]], quote(expr = )))
e[[sym]] <- quote(expr = )
## if the argument is not ..., assign as a promise
else if (sym != "...")
eval(call("delayedAssign", quote(sym), args[[i]],
eval.env = quote(parent_frame), assign.env = quote(e)))
else {
## handle ... separately
## create a variable corresponding to each dot argument,
## then capture them with get("...")
dots <- args[[i]]
for (i in seq_along(dots)) {
sym <- paste0("dd", i)
eval(call("delayedAssign", quote(sym), dots[[i]],
eval.env = quote(parent_frame)))
dots[[i]] <- as.symbol(sym)
}
e[["..."]] <- eval(as.call(c(function(...) get("..."), dots)))
}
}
else {
## similar to above, but this time evaluate in e not parent_frame
i <- match(sym, names(formal_args), 0L)
if (identical(formal_args[[i]], quote(expr = )))
e[[sym]] <- quote(expr = )
else eval(call("delayedAssign", quote(sym),
formal_args[[i]], eval.env = quote(e), assign.env = quote(e)))
}
}
## you don't need to turn into a list, but you can
args2 <- as.list(e, all.names = TRUE)
list(call = call, args = args, args2 = args2, e = e)
## do whatever else you want to here
}
```

in the test scenario you described, it works:

```R
f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
a <- 1
x <- f(a, z = 1 + 100)
x$args2
```

produces:

```
> f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
> a <- 1
> x <- f(a, z = 1 + 100)
> x$args2
$x
[1] 1

$y
[1] 202

$z
[1] 101

$a
NULL

$b


>
```

Regards,
Iris

On Sun, Feb 18, 2024 at 3:51 AM Reed A. Cartwright
 wrote:
>
> I'm wrapping a function in R and I want to record all the arguments
> passed to it, including default values and missing values. I want to
> be able to snoop on function calls in sourced scripts as part of a
> unit testing framework.
>
> I can capture the values fine, but I'm having trouble evaluating them
> as if `force()` had been applied to each of them.
>
> Here is a minimal example:
>
> f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL
>
> f <- function(...) {
>   call <- rlang::call_match(fn = f0, defaults = TRUE)
>   args <- rlang::call_args(call)
>   # do something here to evaluate args as if force() had been called
>   # I've tried many things but haven't found a solution that handled 
> everything
>   args
> }
>
> # In the below example args1 and args2 should be the same
> a <- 1
> args1 <- f(a, z = 1 + 100)
>
> args2 <- list( a = 1, y = 202, z = 101, a = NULL, b = rlang::missing_arg() )
>
> If anyone knows how to get this to work, I would appreciate the help.
>
> Thanks,
> Reed
>
> --
> Reed A. Cartwright, PhD
> Associate Professor of Genomics, Evolution, and Bioinformatics
> School of Life Sciences and The Biodesign Institute
> Arizona State University
> ==
> Address: The Biodesign Institute, PO Box 876401, Tempe, AZ 85287-6401 USA
> Packages: The Biodesign Institute, 1001 S. McAllister Ave, Tempe, AZ
> 85287-6401 USA
> Office: Biodesign B-220C, 1-480-965-9949
> Website: http://cartwrig.ht/
>
> __
> 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] by function does not separate output from function with mulliple parts

2023-10-25 Thread Rui Barradas

Às 00:22 de 25/10/2023, Sorkin, John escreveu:

Colleagues,

I have written an R function (see fully annotated code below), with which I 
want to process a dataframe within levels of the variable StepType. My program 
works, it processes the data within levels of StepType, but the usual headers 
that separate the output by levels of StepType are at the end of the listing 
rather than being used as separators, i.e. I get

Regression results StepType First
Contrast results StepType First
Regression results StepType Second
Contrast results StepType Second

and only after the results are displayed do I get the usual separators:
mydata$StepType: First
NULL
--
mydata$StepType: Second
NULL


What I want to get is output that includes the separators i.e.,

mydata$StepType: First
Regression results StepType First
Contrast results StepType First
--
mydata$StepType: Second
Regression results StepType Second
Contrast results StepType Second

Can you help me get the separators included in the printed otput?
Thank you,
John


# Create Dataframe #

mydata <- structure(list(HipFlex = c(19.44, 4.44, 3.71, 1.95, 2.07, 1.55,
   0.44, 0.23, 2.15, 0.41, 2.3, 0.22, 2.08, 4.61, 4.19, 5.65, 2.73,
   1.46, 10.02, 7.41, 6.91, 5.28, 9.56, 2.46, 6, 3.85, 6.43, 3.73,
   1.08, 1.43, 1.82, 2.22, 0.34, 5.11, 0.94, 0.98, 2.04, 1.73, 0.94,
   18.41, 0.77, 2.31, 0.22, 1.06, 0.13, 0.36, 2.84, 5.2, 2.39, 2.99),
jSex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
   1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
   2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class = 
"factor")),
   row.names = c(NA, 50L), class = "data.frame")

mydata[,"StepType"] <- rep(c("First","Second"),25)
mydata

# END Create Dataframe #



# Define function to be run#

DoReg <- function(x){
 fit0<-lm(as.numeric(HipFlex) ~ jSex,data=x)
   print(summary(fit0))
   
   cat("\nMale\n")

   print(contrast(fit0,
  list(jSex="Male")))
   
   cat("\nFemale\n")

   print(contrast(fit0,
  list(jSex="Female")))
   
   cat("\nDifference\n")

   print(contrast(fit0,
  a=list(jSex="Male"),
  b=list(jSex="Female")))
}

# END Define function to be run#


#
# Run function within levels of Steptype#
#
by(mydata,mydata$StepType,DoReg)
#
# END Run function within levels of Steptype#
#




John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;
Senior Statistician University of Maryland Center for Vascular Research;
Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382



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

What you are seeing is the prints and cats in the function DoReg, not 
its output. The code below works as expected.

Also, you didn't load the package where function contrast() is found.


library(contrast)


# Define function to be run#

DoReg <- function(x){
  fit0 <- lm(as.numeric(HipFlex) ~ jSex,data=x)

  contrast(
fit0,
a=list(jSex="Male"),
b=list(jSex="Female")
  )
}

# END Define function to be run#


#
# Run function within levels of Steptype#
#
by(mydata,mydata$StepType,DoReg)
#> mydata$StepType: First
#> lm model parameter contrast
#>
#>   Contrast S.E.LowerUppert df Pr(>|t|)
#> 1  2.99114 1.956013 -1.05518 7.037461 1.53 23   0.1399
#> 
#> mydata$StepType: Second
#> lm model parameter contrast
#>
#>   Contrast S.E. LowerUpper t df Pr(>|t|)
#> 1   -2.435 1.819421 -6.198759 1.328759 -1.34 23   

Re: [R] by function does not separate output from function with, mulliple parts

2023-10-25 Thread Leonard Mada via R-help

Dear John,

Printing inside the function is problematic. Your function itself does 
NOT print the labels.


Just as a clarification:

F = factor(rep(1:2, 2))
by(data.frame(V = 1:4, F = F), F, function(x) { print(x); return(NULL); } )
#   V F
# 1 1 1
# 3 3 1
#   V F
# 2 2 2
# 4 4 2
# F: 1 <- this is NOT printed inside the function
# NULL
# -
# F: 2
# NULL

### Return Results
by(data.frame(V = 1:4, F = F), F, function(x) { return(x); } )
# F: 1
#   V F
# 1 1 1
# 3 3 1
# --
# F: 2
#   V F
# 2 2 2
# 4 4 2

Maybe others on the list can offer further assistance.

Sincerely,

Leonard

__
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] predict function type class vs. prob

2023-09-23 Thread Rolf Turner


On Fri, 22 Sep 2023 10:12:51 +
"Milbert, Sabine (LGL)"  wrote:

> Dear R Help Team,



In addition to other misapprehensions that others have pointed out, you
seem to have a fundamental misunderstanding of R-help (and perhaps of
R).  There is no such thing as the "R Help Team".  This is a *mailing
list*, to which some R users subscribe, and from time to time
contribute.

All advice given is the personal opinion of the contributor.  It has no
official status, and may or may not be sound advice, depending on the
contributor.  (Those contributors who have responded to your enquiry so
far may be relied upon to give sound advice.)

cheers,

Rolf Turner

-- 
Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:
 +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619

__
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] predict function type class vs. prob

2023-09-23 Thread David Winsemius
That's embarrassing. Apologies for the garbles HTML posting. I'll see if 
this is more readable:


On 9/23/23 05:30, Rui Barradas wrote:

Às 11:12 de 22/09/2023, Milbert, Sabine (LGL) escreveu:

Dear R Help Team,

My research group and I use R scripts for our multivariate data 
screening routines. During routine use, we encountered some 
inconsistencies within the predict() function of the R Stats Package. 





On 9/23/23 05:30, Rui Barradas wrote:
> Às 11:12 de 22/09/2023, Milbert, Sabine (LGL) escreveu:
>> Dear R Help Team,
>>
>> My research group and I use R scripts for our multivariate data 
screening routines. During routine use, we encountered some 
inconsistencies within the predict() function of the R Stats Package.


In addition to Rui's correction to this misstatement, the caret package 
is really a meta package that attempts to implement an umbrella 
framework for a vast array of tools from a wide variety of sources. It 
is an immense effort but not really a part of the core R project. The 
correct place to file issues is found in the DESCRIPTION file:



URL: https://github.com/topepo/caret/
BugReports: https://github.com/topepo/caret/issues

 If you use `str` on an object constructed with caret, you discover 
that the `predict` function is actually not in the main workspace but 
rather embedded in the fit-object itself. I think this is a rather 
general statement regarding the caret universe, and so I expect that 
your fit -objects can be examined for the code that predict.train will 
use with this approach. Your description of your analysis methods was 
rather incompletely specified, and I will put an appendix of "svm" 
methods that might be specified after my demonstration using code. (Note 
that I do not see a caret "weights" hyper-parameter for the "svmLinear" 
method which is actually using code from pkg:kernlab.)



library(caret)
svmFit <- train(Species ~ ., data = iris, method = "svmLinear",
 trControl = trainControl(method = "cv"))

 class(svmFit)
#[1] "train" "train.formula"
str(predict(svmFit))
 Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
str(svmFit)
#---screen output-
List of 24
 $ method  : chr "svmLinear"
 $ modelInfo   :List of 13
  ..$ label : chr "Support Vector Machines with Linear Kernel"
  ..$ library   : chr "kernlab"
  ..$ type  : chr [1:2] "Regression" "Classification"
  ..$ parameters:'data.frame':    1 obs. of  3 variables:
  .. ..$ parameter: chr "C"
  .. ..$ class    : chr "numeric"
  .. ..$ label    : chr "Cost"
  ..$ grid  :function (x, y, len = NULL, search = "grid")
  ..$ loop  : NULL
  ..$ fit   :function (x, y, wts, param, lev, last, classProbs, ...)
  ..$ predict   :function (modelFit, newdata, submodels = NULL)
  ..$ prob  :function (modelFit, newdata, submodels = NULL)
  ..$ predictors:function (x, ...)
  ..$ tags  : chr [1:5] "Kernel Method" "Support Vector Machines" 
"Linear Regression" "Linear Classifier" ...

  ..$ levels    :function (x)
  ..$ sort  :function (x)
 $ modelType   : chr "Classification"
#   large amount of screen output omitted--

# note that the class of svmFit$modelInfo$predict is 'function'
# and its code at least to this particular svm method of which there are 
about 10!



svmFit$modelInfo$predict

# screen output --
function (modelFit, newdata, submodels = NULL)
{
    svmPred <- function(obj, x) {
    hasPM <- !is.null(unlist(obj@prob.model))
    if (hasPM) {
    pred <- kernlab::lev(obj)[apply(kernlab::predict(obj,
    x, type = "probabilities"), 1, which.max)]
    }
    else pred <- kernlab::predict(obj, x)
    pred
    }
    out <- try(svmPred(modelFit, newdata), silent = TRUE)
    if (is.character(kernlab::lev(modelFit))) {
    if (class(out)[1] == "try-error") {
    warning("kernlab class prediction calculations failed; 
returning NAs")

    out <- rep("", nrow(newdata))
    out[seq(along = out)] <- NA
    }
    }
    else {
    if (class(out)[1] == "try-error") {
    warning("kernlab prediction calculations failed; returning 
NAs")

    out <- rep(NA, nrow(newdata))
    }
    }
    if (is.matrix(out))
    out <- out[, 1]
    out
}


--
David


>> Through internal research, we were unable to find the reason for 
this and have decided to contact your help team with the following issue:

>>
>> The predict() function is used once to predict the class membership 
of a new sample (type = "class") on a trained linear SVM model for 
distinguishing two classes (using the caret package). It is then used to 
also examine the probability of class membership (type = "prob"). Both 
are then presented in an R shiny output. Within the routine, we noticed 
two samples (out of 100+) where the class prediction and probability 
prediction did not match. The prediction probabilities of one class 
(52%) did not match the class membership 

Re: [R] predict function type class vs. prob

2023-09-23 Thread Ivan Krylov
В Fri, 22 Sep 2023 10:12:51 +
"Milbert, Sabine (LGL)"  пишет:

> PS: If this is an issue based on the model training function of the
> caret package and therefore not your responsibility, please let us
> know.

Indeed, as Rui Barradas said, predict() is a generic function. Calling
it with your model as an argument resolves to a function in the caret
package.

It's hard to say without looking at your code and data (the R-help
posting guide has some hints on how to prepare a reproducible example),
but I think that the caret package fits your linear SVM models using
kernlab::ksvm, and then predict() resolves to a combination of
kernlab::predict (potentially with the argument type = "probabilities")
and kernlab::lev.

Try replicating your results using just the kernlab package.

-- 
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] predict function type class vs. prob

2023-09-23 Thread David Winsemius


On 9/23/23 05:30, Rui Barradas wrote:
> Às 11:12 de 22/09/2023, Milbert, Sabine (LGL) escreveu:
>> Dear R Help Team,
>>
>> My research group and I use R scripts for our multivariate data 
>> screening routines. During routine use, we encountered some 
>> inconsistencies within the predict() function of the R Stats Package.

In addition to Rui's correction to this misstatement, the caret package 
is really a meta package that attempts to implement an umbrella 
framework for a vast array of tools from a wide variety of sources. It 
is an immense effort but not really a part of the core R project. The 
correct place to file issues is found in the DESCRIPTION file:


URL: https://github.com/topepo/caret/ BugReports: 
https://github.com/topepo/caret/issues

  If you use `str` on an object constructed with caret, you discover 
that the `predict` function is actually not in the main workspace but 
rather embedded in the fit-object itself. I think this is a rather 
general statement regarding the caret universe, and so I expect that 
your fit -objects can be examined for the code that predict.train will 
use with this approach. Your description of your analysis methods was 
rather incompletely specified, and I will put an appendix of "svm" 
methods that might be specified after my demonstration using code. (Note 
that I do not see a caret "weights" hyper-parameter for the "svmLinear" 
method which is actually using code from pkg:kernlab.)


library(caret) svmFit <- train(Species ~ ., data = iris, method = 
"svmLinear", trControl = trainControl(method = "cv")) class(svmFit) #[1] 
"train" "train.formula" str(predict(svmFit)) Factor w/ 3 levels 
"setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... str(svmFit) #---screen 
output- List of 24 $ method : chr "svmLinear" $ modelInfo 
:List of 13 ..$ label : chr "Support Vector Machines with Linear Kernel" 
..$ library : chr "kernlab" ..$ type : chr [1:2] "Regression" 
"Classification" ..$ parameters:'data.frame': 1 obs. of 3 variables: .. 
..$ parameter: chr "C" .. ..$ class : chr "numeric" .. ..$ label : chr 
"Cost" ..$ grid :function (x, y, len = NULL, search = "grid") ..$ loop : 
NULL ..$ fit :function (x, y, wts, param, lev, last, classProbs, ...) 
..$ predict :function (modelFit, newdata, submodels = NULL) ..$ prob 
:function (modelFit, newdata, submodels = NULL) ..$ predictors:function 
(x, ...) ..$ tags : chr [1:5] "Kernel Method" "Support Vector Machines" 
"Linear Regression" "Linear Classifier" ... ..$ levels :function (x) ..$ 
sort :function (x) $ modelType : chr "Classification" #  large 
amount of screen output omitted-- # note that the class of 
svmFit$modelInfo$predict is 'function' # and its code at least to this 
particular svm method of which there are about 10!
svmFit$modelInfo$predict # screen output -- function (modelFit, 
newdata, submodels = NULL) { svmPred <- function(obj, x) { hasPM <- 
!is.null(unlist(obj@prob.model)) if (hasPM) { pred <- 
kernlab::lev(obj)[apply(kernlab::predict(obj, x, type = 
"probabilities"), 1, which.max)] } else pred <- kernlab::predict(obj, x) 
pred } out <- try(svmPred(modelFit, newdata), silent = TRUE) if 
(is.character(kernlab::lev(modelFit))) { if (class(out)[1] == 
"try-error") { warning("kernlab class prediction calculations failed; 
returning NAs") out <- rep("", nrow(newdata)) out[seq(along = out)] <- 
NA } } else { if (class(out)[1] == "try-error") { warning("kernlab 
prediction calculations failed; returning NAs") out <- rep(NA, 
nrow(newdata)) } } if (is.matrix(out)) out <- out[, 1] out }  -- David


>> Through internal research, we were unable to find the reason for this 
>> and have decided to contact your help team with the following issue:
>>
>> The predict() function is used once to predict the class membership 
>> of a new sample (type = "class") on a trained linear SVM model for 
>> distinguishing two classes (using the caret package). It is then used 
>> to also examine the probability of class membership (type = "prob"). 
>> Both are then presented in an R shiny output. Within the routine, we 
>> noticed two samples (out of 100+) where the class prediction and 
>> probability prediction did not match. The prediction probabilities of 
>> one class (52%) did not match the class membership within the predict 
>> function. We use the same seed and the discrepancy is reproducible in 
>> this sample. The same problem did not occur in other trained models 
>> (lda, random forest, radial SVM...).

*Support Vector Machines with Boundrange String Kernel*(|method = 
'svmBoundrangeString'|)

For classification and regression using packagekernlabwith tuning 
parameters:

  *

length (|length|, numeric)

  *

Cost (|C|, numeric)

*Support Vector Machines with Class Weights*(|method = 'svmRadialWeights'|)

For classification using packagekernlabwith tuning parameters:

  *

Sigma (|sigma|, numeric)

  *

Cost (|C|, numeric)

  *

Weight (|Weight|, numeric)

*Support 

Re: [R] predict function type class vs. prob

2023-09-23 Thread Rui Barradas

Às 11:12 de 22/09/2023, Milbert, Sabine (LGL) escreveu:

Dear R Help Team,

My research group and I use R scripts for our multivariate data screening 
routines. During routine use, we encountered some inconsistencies within the 
predict() function of the R Stats Package. Through internal research, we were 
unable to find the reason for this and have decided to contact your help team 
with the following issue:

The predict() function is used once to predict the class membership of a new sample (type = 
"class") on a trained linear SVM model for distinguishing two classes (using the caret 
package). It is then used to also examine the probability of class membership (type = 
"prob"). Both are then presented in an R shiny output. Within the routine, we noticed two 
samples (out of 100+) where the class prediction and probability prediction did not match. The 
prediction probabilities of one class (52%) did not match the class membership within the predict 
function. We use the same seed and the discrepancy is reproducible in this sample. The same problem 
did not occur in other trained models (lda, random forest, radial SVM...).

Is there a weighing of classes within the prediction function or is the 
classification limit not at 50%/a majority vote? Or do you have another 
explanation for this discrepancy, please let us know.

PS: If this is an issue based on the model training function of the caret 
package and therefore not your responsibility, please let us know.

Thank you in advance for your support!

Yours sincerely,
Sabine Milbert

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

I cannot tell what is going on but I would like to make a correction to 
your post.


predict() is a generic function with methods for objects of several 
classes in many packages. In base package stats you will find methods 
for objects (fits) of class lm, glm and others, see ?predict.


The method you are asking about is predict.train, defined in package 
caret, not in package stats.

to see what predict method is being called, check


class(your_fit)


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] col2rgb() function

2023-07-23 Thread Ben Bolker
  You could also adjustcolor for this approach (levels of red, green, 
blue, alpha can all be adjusted proportionally)


On 2023-07-23 5:35 p.m., David Stevens via R-help wrote:

Nick,

I've also made colors transparent by pasting the hex equivalent of, say,
0.3*256 = 76.9 to the hex color code. e.q. for black it might be
"#004d" and the 4d is 77 in hex. That way you don't need to convert
back and forth so much. If col is "#00" the transparent version is

tcol <- paste0(col,"4d")

This would work in one step on a whole palette.

David

David K Stevens, PhD, PE, Professor
Civil and Environmental Engineering
Utah Water Research Laboratory
Utah State University
8200 Old Main Hill
Logan, UT 84322-8200
david.stev...@usu.edu
(435) 797-3229 (office)

On 7/23/2023 1:00 PM, Nick Wray wrote:

Thanks That works nicely  Nick

On Sun, 23 Jul 2023 at 19:26, Ben Bolker  wrote:


 Does adjustcolor() help?

cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
  "#D55E00", "#CC79A7")
plot(0,0,xlim=c(1,8),ylim=c(0,1))
points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3), pch=19,cex=2)

On 2023-07-23 2:15 p.m., Nick Wray wrote:

Hello  I have a palette vector of colour blind colours (in hexadecimal)
which I’m using for plots, but they are not see-through, and as I wanted

to

overlay some histograms I wanted to convert these colours to rgb, when

you

can set the opacity.

I have found the function col2rgb(), which works in the sense that it

gives

a vector of numbers but these don’t work directly in rgb because they are
too big.  If I divide through to make them all less than 1 I don’t get

the

corresponding colour-blind hue, but something somewhat off.

Here is the colour-blind palette in a plot:


*cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7")*

*plot(0,0,xlim=c(1,8),ylim=c(0,1))*

*points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*



so if I try to convert the red dot ("#D55E00") (number 7) I get

*col2rgb("#D55E00"*

 [,1]

red213

green   94

blue 0

*points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*

gives me an error message and although if  I divide through

*points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*

gives me a reddish dot, but not the same as in the colour-blind palette



Somewhat mystified.  Can anyone help?? Thanks Nick Wray

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


[[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] col2rgb() function

2023-07-23 Thread David Stevens via R-help
Nick,

I've also made colors transparent by pasting the hex equivalent of, say, 
0.3*256 = 76.9 to the hex color code. e.q. for black it might be 
"#004d" and the 4d is 77 in hex. That way you don't need to convert 
back and forth so much. If col is "#00" the transparent version is

tcol <- paste0(col,"4d")

This would work in one step on a whole palette.

David

David K Stevens, PhD, PE, Professor
Civil and Environmental Engineering
Utah Water Research Laboratory
Utah State University
8200 Old Main Hill
Logan, UT 84322-8200
david.stev...@usu.edu
(435) 797-3229 (office)

On 7/23/2023 1:00 PM, Nick Wray wrote:
> Thanks That works nicely  Nick
>
> On Sun, 23 Jul 2023 at 19:26, Ben Bolker  wrote:
>
>> Does adjustcolor() help?
>>
>> cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
>>  "#D55E00", "#CC79A7")
>> plot(0,0,xlim=c(1,8),ylim=c(0,1))
>> points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
>> points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3), pch=19,cex=2)
>>
>> On 2023-07-23 2:15 p.m., Nick Wray wrote:
>>> Hello  I have a palette vector of colour blind colours (in hexadecimal)
>>> which I’m using for plots, but they are not see-through, and as I wanted
>> to
>>> overlay some histograms I wanted to convert these colours to rgb, when
>> you
>>> can set the opacity.
>>>
>>> I have found the function col2rgb(), which works in the sense that it
>> gives
>>> a vector of numbers but these don’t work directly in rgb because they are
>>> too big.  If I divide through to make them all less than 1 I don’t get
>> the
>>> corresponding colour-blind hue, but something somewhat off.
>>>
>>> Here is the colour-blind palette in a plot:
>>>
>>>
>>> *cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
>>> "#D55E00", "#CC79A7")*
>>>
>>> *plot(0,0,xlim=c(1,8),ylim=c(0,1))*
>>>
>>> *points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*
>>>
>>>
>>>
>>> so if I try to convert the red dot ("#D55E00") (number 7) I get
>>>
>>> *col2rgb("#D55E00"*
>>>
>>> [,1]
>>>
>>> red213
>>>
>>> green   94
>>>
>>> blue 0
>>>
>>> *points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*
>>>
>>> gives me an error message and although if  I divide through
>>>
>>> *points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*
>>>
>>> gives me a reddish dot, but not the same as in the colour-blind palette
>>>
>>>
>>>
>>> Somewhat mystified.  Can anyone help?? Thanks Nick Wray
>>>
>>>[[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.
>>
>   [[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] col2rgb() function

2023-07-23 Thread Nick Wray
Thanks v useful to know Nick

On Sun, 23 Jul 2023 at 21:13, Achim Zeileis 
wrote:

> Just one addition which may or may not be useful: The color palette you
> use is also known as "Okabe-Ito" and it is the default set of colors in
> the palette.colors() function. This function also has an optional alpha
> argument. So if you want to generate these colors with an alpha of 0.3 you
> can also do:
>
> palette.colors(8, alpha = 0.3)
>
> or more explicitly
>
> palette.colors(8, palette = "Okabe-Ito", alpha = 0.3)
>
> On Sun, 23 Jul 2023, Nick Wray wrote:
>
> > Thanks That works nicely  Nick
> >
> > On Sun, 23 Jul 2023 at 19:26, Ben Bolker  wrote:
> >
> >>Does adjustcolor() help?
> >>
> >> cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
> >> "#D55E00", "#CC79A7")
> >> plot(0,0,xlim=c(1,8),ylim=c(0,1))
> >> points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
> >> points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3),
> pch=19,cex=2)
> >>
> >> On 2023-07-23 2:15 p.m., Nick Wray wrote:
> >>> Hello  I have a palette vector of colour blind colours (in hexadecimal)
> >>> which I’m using for plots, but they are not see-through, and as I
> wanted
> >> to
> >>> overlay some histograms I wanted to convert these colours to rgb, when
> >> you
> >>> can set the opacity.
> >>>
> >>> I have found the function col2rgb(), which works in the sense that it
> >> gives
> >>> a vector of numbers but these don’t work directly in rgb because they
> are
> >>> too big.  If I divide through to make them all less than 1 I don’t get
> >> the
> >>> corresponding colour-blind hue, but something somewhat off.
> >>>
> >>> Here is the colour-blind palette in a plot:
> >>>
> >>>
> >>> *cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442",
> "#0072B2",
> >>> "#D55E00", "#CC79A7")*
> >>>
> >>> *plot(0,0,xlim=c(1,8),ylim=c(0,1))*
> >>>
> >>> *points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*
> >>>
> >>>
> >>>
> >>> so if I try to convert the red dot ("#D55E00") (number 7) I get
> >>>
> >>> *col2rgb("#D55E00"*
> >>>
> >>>[,1]
> >>>
> >>> red213
> >>>
> >>> green   94
> >>>
> >>> blue 0
> >>>
> >>> *points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*
> >>>
> >>> gives me an error message and although if  I divide through
> >>>
> >>> *points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*
> >>>
> >>> gives me a reddish dot, but not the same as in the colour-blind palette
> >>>
> >>>
> >>>
> >>> Somewhat mystified.  Can anyone help?? Thanks Nick Wray
> >>>
> >>>   [[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.
> >>
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >

[[alternative HTML version deleted]]

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


Re: [R] col2rgb() function

2023-07-23 Thread Achim Zeileis
Just one addition which may or may not be useful: The color palette you 
use is also known as "Okabe-Ito" and it is the default set of colors in 
the palette.colors() function. This function also has an optional alpha 
argument. So if you want to generate these colors with an alpha of 0.3 you 
can also do:


palette.colors(8, alpha = 0.3)

or more explicitly

palette.colors(8, palette = "Okabe-Ito", alpha = 0.3)

On Sun, 23 Jul 2023, Nick Wray wrote:


Thanks That works nicely  Nick

On Sun, 23 Jul 2023 at 19:26, Ben Bolker  wrote:


   Does adjustcolor() help?

cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
plot(0,0,xlim=c(1,8),ylim=c(0,1))
points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3), pch=19,cex=2)

On 2023-07-23 2:15 p.m., Nick Wray wrote:

Hello  I have a palette vector of colour blind colours (in hexadecimal)
which I’m using for plots, but they are not see-through, and as I wanted

to

overlay some histograms I wanted to convert these colours to rgb, when

you

can set the opacity.

I have found the function col2rgb(), which works in the sense that it

gives

a vector of numbers but these don’t work directly in rgb because they are
too big.  If I divide through to make them all less than 1 I don’t get

the

corresponding colour-blind hue, but something somewhat off.

Here is the colour-blind palette in a plot:


*cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7")*

*plot(0,0,xlim=c(1,8),ylim=c(0,1))*

*points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*



so if I try to convert the red dot ("#D55E00") (number 7) I get

*col2rgb("#D55E00"*

   [,1]

red213

green   94

blue 0

*points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*

gives me an error message and although if  I divide through

*points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*

gives me a reddish dot, but not the same as in the colour-blind palette



Somewhat mystified.  Can anyone help?? Thanks Nick Wray

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



[[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] col2rgb() function

2023-07-23 Thread Nick Wray
Thanks That works nicely  Nick

On Sun, 23 Jul 2023 at 19:26, Ben Bolker  wrote:

>Does adjustcolor() help?
>
> cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
> "#D55E00", "#CC79A7")
> plot(0,0,xlim=c(1,8),ylim=c(0,1))
> points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
> points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3), pch=19,cex=2)
>
> On 2023-07-23 2:15 p.m., Nick Wray wrote:
> > Hello  I have a palette vector of colour blind colours (in hexadecimal)
> > which I’m using for plots, but they are not see-through, and as I wanted
> to
> > overlay some histograms I wanted to convert these colours to rgb, when
> you
> > can set the opacity.
> >
> > I have found the function col2rgb(), which works in the sense that it
> gives
> > a vector of numbers but these don’t work directly in rgb because they are
> > too big.  If I divide through to make them all less than 1 I don’t get
> the
> > corresponding colour-blind hue, but something somewhat off.
> >
> > Here is the colour-blind palette in a plot:
> >
> >
> > *cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
> > "#D55E00", "#CC79A7")*
> >
> > *plot(0,0,xlim=c(1,8),ylim=c(0,1))*
> >
> > *points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*
> >
> >
> >
> > so if I try to convert the red dot ("#D55E00") (number 7) I get
> >
> > *col2rgb("#D55E00"*
> >
> >[,1]
> >
> > red213
> >
> > green   94
> >
> > blue 0
> >
> > *points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*
> >
> > gives me an error message and although if  I divide through
> >
> > *points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*
> >
> > gives me a reddish dot, but not the same as in the colour-blind palette
> >
> >
> >
> > Somewhat mystified.  Can anyone help?? Thanks Nick Wray
> >
> >   [[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.
>

[[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] col2rgb() function

2023-07-23 Thread Duncan Murdoch

On 23/07/2023 2:15 p.m., Nick Wray wrote:

Hello  I have a palette vector of colour blind colours (in hexadecimal)
which I’m using for plots, but they are not see-through, and as I wanted to
overlay some histograms I wanted to convert these colours to rgb, when you
can set the opacity.

I have found the function col2rgb(), which works in the sense that it gives
a vector of numbers but these don’t work directly in rgb because they are
too big.  If I divide through to make them all less than 1 I don’t get the
corresponding colour-blind hue, but something somewhat off.

Here is the colour-blind palette in a plot:


*cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7")*

*plot(0,0,xlim=c(1,8),ylim=c(0,1))*

*points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*



so if I try to convert the red dot ("#D55E00") (number 7) I get

*col2rgb("#D55E00"*

   [,1]

red213

green   94

blue 0

*points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*

gives me an error message and although if  I divide through

*points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*

gives me a reddish dot, but not the same as in the colour-blind palette


Why are you dividing by 307?  You should divide by 255.

Duncan Murdoch

__
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] col2rgb() function

2023-07-23 Thread Ben Bolker

  Does adjustcolor() help?

cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
   "#D55E00", "#CC79A7")
plot(0,0,xlim=c(1,8),ylim=c(0,1))
points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)
points(1:8,rep(0.75,8),col=adjustcolor(cb8, alpha.f = 0.3), pch=19,cex=2)

On 2023-07-23 2:15 p.m., Nick Wray wrote:

Hello  I have a palette vector of colour blind colours (in hexadecimal)
which I’m using for plots, but they are not see-through, and as I wanted to
overlay some histograms I wanted to convert these colours to rgb, when you
can set the opacity.

I have found the function col2rgb(), which works in the sense that it gives
a vector of numbers but these don’t work directly in rgb because they are
too big.  If I divide through to make them all less than 1 I don’t get the
corresponding colour-blind hue, but something somewhat off.

Here is the colour-blind palette in a plot:


*cb8<- c("#00", "#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2",
"#D55E00", "#CC79A7")*

*plot(0,0,xlim=c(1,8),ylim=c(0,1))*

*points(1:8,rep(0.5,8),col=cb8,pch=19,cex=2)*



so if I try to convert the red dot ("#D55E00") (number 7) I get

*col2rgb("#D55E00"*

   [,1]

red213

green   94

blue 0

*points(7,0.25,col=rgb(rgb(213,94,0)),pch=19,cex=2)*

gives me an error message and although if  I divide through

*points(7,0.25,col=rgb(213/307,94/307,0),pch=19,cex=2)*

gives me a reddish dot, but not the same as in the colour-blind palette



Somewhat mystified.  Can anyone help?? Thanks Nick Wray

[[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] PROP_TEST function from INFER package

2023-05-15 Thread Marc Schwartz via R-help
Hi,

You are using the prop_test() function from the infer CRAN package, with an 
argument set for an example using the prop_test() function from the rstatix 
CRAN package.

They are **not** the same function, and do not take the same arguments, even 
though they have the same name.

Read the help page for the prop_test() function in the infer package, where 'x' 
is supposed to be a data frame, not a vector, and there is no 'n' argument.

Regards,

Marc Schwartz


On May 15, 2023 at 7:26:16 AM, alfredo.rocc...@fastwebnet.it 
(alfredo.rocc...@fastwebnet.it (mailto:alfredo.rocc...@fastwebnet.it)) wrote:

> Dear all,
>
> I'm trying to replicate the same example you can find in:
> https://rpkgs.datanovia.com/rstatix/reference/prop_test.html
>
> but I get the following get this error message:
>
>
>
> > library(infer)
>
> > prop_test(x = 95, n = 160, p = 0.5, detailed = TRUE)
>
> Error: Please supply a response variable that is not `NULL`.
>
> >
>
>
>
> > strsplit(version[['version.string']], ' ')[[1]][3]
>
> [1] "4.3.0"
>
> >
>
> > packageVersion("infer")
>
> [1] '1.0.4'
>
>
>
> Anybody can give any help?
>
> Thank you a lot,
>
> Alfredo
>
>

__
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] seqMK function

2023-04-03 Thread Rasmus Liland
Dear Nick,

Looking at dput(pheno::seqMK), 
dput(pheno::tau), and [1], I think you 
need to change the function pheno::tau 
to change the confidence level ... R

[1] 
https://mannkendall.github.io/about.html#application-of-the-seasonal-mann-kendall-test

__
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] Generic Function read?

2023-02-28 Thread John Kane
Have a look at the {rio} package.

On Tue, 28 Feb 2023 at 15:00, Leonard Mada via R-help 
wrote:

> Dear R-Users,
>
> I noticed that *read* is not a generic function. Although it could
> benefit from the functionality available for generic functions:
>
> read = function(file, ...) UseMethod("read")
>
> methods(read)
>   # [1] read.csv read.csv2read.dcf read.delim read.delim2
> read.DIF read.fortran
>   # [8] read.ftable  read.fwf read.socket  read.table
>
> The users would still need to call the full function name. But it seems
> useful to be able to find rapidly what formats can be read; including
> with other packages (e.g. for Excel, SAS, ... - although most packages
> do not adhere to the generic naming convention, but maybe they will
> change in the future).
>
> Note:
> This should be possible (even though impractical), but actually does NOT
> work:
> read = function(file, ...) UseMethod("read")
> file = "file.csv"
> class(file) = c("csv", class(file));
> read(file)
>
> Should it not work?
>
> Sincerely,
>
> Leonard
>
> __
> 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.
>


-- 
John Kane
Kingston ON Canada

[[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] Information function in CRM model

2022-01-06 Thread Bert Gunter
If you haven't already done so, you should probably look here:
https://cran.r-project.org/web/views/Psychometrics.html

Further detailed discussion of the statistics is generally off topic here.
Per the posting guide (linked below):

"Questions about statistics: The R mailing lists are primarily intended for
questions and discussion about the R software. However, questions about
statistical methodology are sometimes posted. If the question is well-asked
and of interest to someone on the list, it may elicit an informative
up-to-date answer."


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jan 6, 2022 at 2:03 AM Cecilia Reyna  wrote:

> Dear,
>
> I am trying to model the information function of items (and full) based on
> the continuous response model.
>
> I am using the EstCRM (1.4) package, but it doesn't compute the information
> function.
>
> Another person noticed me of Wang & Zeng's paper (Item parameter estimation
> for a continuous response model using an EM algorithm, 1998, Applied
> Psychological Measurement). But I am not sure how to calculate it in R.
>
> Please, If someone has calculated that function could you share the script?
>
> Thanks in advance,
> Cecilia
>
> --
> Cecilia Reyna
> Instituto de Investigaciones Psicológicas (IIPsi) - CONICET - UNC
> Facultad de Psicología - Universidad Nacional de Córdoba
> (5000) Córdoba - ARGENTINA
> Tel. 54-351-5353890 int. 60201
>
> RACC: http://www.revistas.unc.edu.ar/index.php/racc
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] Information function in CRM model

2022-01-06 Thread Cecilia Reyna
Please, forget and delete the previous post!

Thanks!



On Thu, Jan 6, 2022 at 7:02 AM Cecilia Reyna  wrote:

> Dear,
>
> I am trying to model the information function of items (and full) based on
> the continuous response model.
>
> I am using the EstCRM (1.4) package, but it doesn't compute the
> information function.
>
> Another person noticed me of Wang & Zeng's paper (Item parameter
> estimation for a continuous response model using an EM algorithm, 1998,
> Applied Psychological Measurement). But I am not sure how to calculate it
> in R.
>
> Please, If someone has calculated that function could you share the script?
>
> Thanks in advance,
> Cecilia
>
> --
> Cecilia Reyna
> Instituto de Investigaciones Psicológicas (IIPsi) - CONICET - UNC
> Facultad de Psicología - Universidad Nacional de Córdoba
> (5000) Córdoba - ARGENTINA
> Tel. 54-351-5353890 int. 60201
>
> RACC: http://www.revistas.unc.edu.ar/index.php/racc
>


-- 
Cecilia Reyna
Instituto de Investigaciones Psicológicas (IIPsi) - CONICET - UNC
Facultad de Psicología - Universidad Nacional de Córdoba
(5000) Córdoba - ARGENTINA
Tel. 54-351-5353890 int. 60201

RACC: http://www.revistas.unc.edu.ar/index.php/racc

[[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] Improvement: function cut

2021-09-18 Thread David Winsemius



On 9/18/21 5:28 AM, Leonard Mada via R-help wrote:

Hello Andrew,


I add this info as a completion (so other users can get a better
understanding):

If we want to perform a survival analysis, than the interval should be
closed to the right, but we should include also the first time point (as
per Intention-to-Treat):

[0, 4](4, 8](8, 12](12, 16]

[0, 4](4, 8](8, 12](12, 16](16, 20]


So the series is extendible to the right without any errors!

But the 1st interval (which is the same in both series) is different
from the other intervals: [0, 4].


I feel that this should have been the default behaviour for cut().


To Leonard;

If you do not like the behavior of `cut`, then you should "roll your 
own". It's very unlikely that R Core will modify a base cunction like 
cut. You might want to look at Hmisc::cut2. Frank Harrell didn't like 
that default behavior and thought he could make a better cut, so he just 
put it in his package. I did like his version better and often used it 
when I was actively programming. I suspect there is also a tidyverse 
cut-like function, but I'm not terribly familiar with that fork of R. 
(It's really not the same language IMHO.)


But it's a waste of time and energy to try propose modifications of core 
R functions unless *you* can show that it is stable across 20,000 
packages and will not offend long-time users. The likelihood  of that 
happening for your proposal is vanishing small in my estimation. You 
shouldn't ask R Core to do that for you. They are busy fixing real bugs.



If you want to persist despite my negativity, then you should make a 
complete proposal by submitting a proper diff file that incorporates 
your tested efforts to the Rdevel mailing list.



--

David



Note:

I was induced to think about a different situation in my previous
message, as you constructed open intervals on the right, and also
extended to the right. But survival analysis should be as described in
this mail and should probably be the default.


Sincerely,


Leonard


On 9/18/2021 1:29 AM, Andrew Simmons wrote:

I disagree, I don't really think it's too long or ugly, but if you
think it is, you could abbreviate it as 'i'.


x <- 0:20
breaks1 <- seq.int (0, 16, 4)
breaks2 <- seq.int (0, 20, 4)
data.frame(
     cut(x, breaks1, right = FALSE, i = TRUE),
     cut(x, breaks2, right = FALSE, i = TRUE),
     check.names = FALSE
)


I hope this helps.

On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada mailto:leo.m...@syonic.eu>> wrote:

 Hello Andrew,


 But "cut" generates factors. In most cases with real data one
 expects to have also the ends of the interval: the argument
 "include.lowest" is both ugly and too long.

 [The test-code on the ftable thread contains this error! I have
 run through this error a couple of times.]


 The only real situation that I can imagine to be problematic:

 - if the interval goes to +Inf (or -Inf): I do not know if there
 would be any effects when including +Inf (or -Inf).


 Leonard


 On 9/18/2021 1:14 AM, Andrew Simmons wrote:

 While it is not explicitly mentioned anywhere in the
 documentation for .bincode, I suspect 'include.lowest = FALSE' is
 the default to keep the definitions of the bins consistent. For
 example:


 x <- 0:20
 breaks1 <- seq.int (0, 16, 4)
 breaks2 <- seq.int (0, 20, 4)
 cbind(
     .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
     .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
 )


 by having 'include.lowest = TRUE' with different ends, you can
 get inconsistent behaviour. While this probably wouldn't be an
 issue with 'real' data, this would seem like something you'd want
 to avoid by default. The definitions of the bins are


 [0, 4)
 [4, 8)
 [8, 12)
 [12, 16]


 and


 [0, 4)
 [4, 8)
 [8, 12)
 [12, 16)
 [16, 20]


 so you can see where the inconsistent behaviour comes from. You
 might be able to get R-core to add argument 'warn', but probably
 not to change the default of 'include.lowest'. I hope this helps


 On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada mailto:leo.m...@syonic.eu>> wrote:

 Thank you Andrew.


 Is there any reason not to make: include.lowest = TRUE the
 default?


 Regarding the NA:

 The user still has to suspect that some values were not
 included and run that test.


 Leonard


 On 9/18/2021 12:53 AM, Andrew Simmons wrote:

 Regarding your first point, argument 'include.lowest'
 already handles this specific case, see ?.bincode

 Your second point, maybe it could be helpful, but since both
 'cut.default' and '.bincode' return NA if a value isn't
 within a bin, you could make something like this on your own.
 Might be worth pitching to R-bugs on the 

Re: [R] Improvement: function cut

2021-09-18 Thread Leonard Mada via R-help
Hello Andrew,


I add this info as a completion (so other users can get a better 
understanding):

If we want to perform a survival analysis, than the interval should be 
closed to the right, but we should include also the first time point (as 
per Intention-to-Treat):

[0, 4](4, 8](8, 12](12, 16]

[0, 4](4, 8](8, 12](12, 16](16, 20]


So the series is extendible to the right without any errors!

But the 1st interval (which is the same in both series) is different 
from the other intervals: [0, 4].


I feel that this should have been the default behaviour for cut().

Note:

I was induced to think about a different situation in my previous 
message, as you constructed open intervals on the right, and also 
extended to the right. But survival analysis should be as described in 
this mail and should probably be the default.


Sincerely,


Leonard


On 9/18/2021 1:29 AM, Andrew Simmons wrote:
> I disagree, I don't really think it's too long or ugly, but if you 
> think it is, you could abbreviate it as 'i'.
>
>
> x <- 0:20
> breaks1 <- seq.int (0, 16, 4)
> breaks2 <- seq.int (0, 20, 4)
> data.frame(
>     cut(x, breaks1, right = FALSE, i = TRUE),
>     cut(x, breaks2, right = FALSE, i = TRUE),
>     check.names = FALSE
> )
>
>
> I hope this helps.
>
> On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada  > wrote:
>
> Hello Andrew,
>
>
> But "cut" generates factors. In most cases with real data one
> expects to have also the ends of the interval: the argument
> "include.lowest" is both ugly and too long.
>
> [The test-code on the ftable thread contains this error! I have
> run through this error a couple of times.]
>
>
> The only real situation that I can imagine to be problematic:
>
> - if the interval goes to +Inf (or -Inf): I do not know if there
> would be any effects when including +Inf (or -Inf).
>
>
> Leonard
>
>
> On 9/18/2021 1:14 AM, Andrew Simmons wrote:
>> While it is not explicitly mentioned anywhere in the
>> documentation for .bincode, I suspect 'include.lowest = FALSE' is
>> the default to keep the definitions of the bins consistent. For
>> example:
>>
>>
>> x <- 0:20
>> breaks1 <- seq.int (0, 16, 4)
>> breaks2 <- seq.int (0, 20, 4)
>> cbind(
>>     .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
>>     .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
>> )
>>
>>
>> by having 'include.lowest = TRUE' with different ends, you can
>> get inconsistent behaviour. While this probably wouldn't be an
>> issue with 'real' data, this would seem like something you'd want
>> to avoid by default. The definitions of the bins are
>>
>>
>> [0, 4)
>> [4, 8)
>> [8, 12)
>> [12, 16]
>>
>>
>> and
>>
>>
>> [0, 4)
>> [4, 8)
>> [8, 12)
>> [12, 16)
>> [16, 20]
>>
>>
>> so you can see where the inconsistent behaviour comes from. You
>> might be able to get R-core to add argument 'warn', but probably
>> not to change the default of 'include.lowest'. I hope this helps
>>
>>
>> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada > > wrote:
>>
>> Thank you Andrew.
>>
>>
>> Is there any reason not to make: include.lowest = TRUE the
>> default?
>>
>>
>> Regarding the NA:
>>
>> The user still has to suspect that some values were not
>> included and run that test.
>>
>>
>> Leonard
>>
>>
>> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>>> Regarding your first point, argument 'include.lowest'
>>> already handles this specific case, see ?.bincode
>>>
>>> Your second point, maybe it could be helpful, but since both
>>> 'cut.default' and '.bincode' return NA if a value isn't
>>> within a bin, you could make something like this on your own.
>>> Might be worth pitching to R-bugs on the wishlist.
>>>
>>>
>>>
>>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help
>>> mailto:r-help@r-project.org>> wrote:
>>>
>>> Hello List members,
>>>
>>>
>>> the following improvements would be useful for function
>>> cut (and .bincode):
>>>
>>>
>>> 1.) Argument: Include extremes
>>> extremes = TRUE
>>> if(right == FALSE) {
>>>     # include also right for last interval;
>>> } else {
>>>     # include also left for first interval;
>>> }
>>>
>>>
>>> 2.) Argument: warn = TRUE
>>>
>>> Warn if any values are not included in the intervals.
>>>
>>>
>>> Motivation:
>>> - reduce risk of errors when using function cut();
>>>
>>>
>>> Sincerely,
>>>
>>>
>>> Leonard
>>>
>>> __
>>> R-help@r-project.org 

Re: [R] Improvement: function cut

2021-09-17 Thread Bert Gunter
Perhaps you and Andrew should take this discussion off list...

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Fri, Sep 17, 2021 at 3:45 PM Leonard Mada via R-help
 wrote:
>
> Why would you want to merge different factors?
>
> It makes no sense on real data. Even if some names are the same, the
> factors are not the same!
>
>
> The only real-data application that springs to mind is censoring (right
> or left, depending on the choice): but here we have both open and closed
> intervals, e.g. to the right (in the same data-set).
>
>
> Leonard
>
>
> On 9/18/2021 1:29 AM, Andrew Simmons wrote:
> > I disagree, I don't really think it's too long or ugly, but if you
> > think it is, you could abbreviate it as 'i'.
> >
> >
> > x <- 0:20
> > breaks1 <- seq.int (0, 16, 4)
> > breaks2 <- seq.int (0, 20, 4)
> > data.frame(
> > cut(x, breaks1, right = FALSE, i = TRUE),
> > cut(x, breaks2, right = FALSE, i = TRUE),
> > check.names = FALSE
> > )
> >
> >
> > I hope this helps.
> >
> > On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada  > > wrote:
> >
> > Hello Andrew,
> >
> >
> > But "cut" generates factors. In most cases with real data one
> > expects to have also the ends of the interval: the argument
> > "include.lowest" is both ugly and too long.
> >
> > [The test-code on the ftable thread contains this error! I have
> > run through this error a couple of times.]
> >
> >
> > The only real situation that I can imagine to be problematic:
> >
> > - if the interval goes to +Inf (or -Inf): I do not know if there
> > would be any effects when including +Inf (or -Inf).
> >
> >
> > Leonard
> >
> >
> > On 9/18/2021 1:14 AM, Andrew Simmons wrote:
> >> While it is not explicitly mentioned anywhere in the
> >> documentation for .bincode, I suspect 'include.lowest = FALSE' is
> >> the default to keep the definitions of the bins consistent. For
> >> example:
> >>
> >>
> >> x <- 0:20
> >> breaks1 <- seq.int (0, 16, 4)
> >> breaks2 <- seq.int (0, 20, 4)
> >> cbind(
> >> .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
> >> .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
> >> )
> >>
> >>
> >> by having 'include.lowest = TRUE' with different ends, you can
> >> get inconsistent behaviour. While this probably wouldn't be an
> >> issue with 'real' data, this would seem like something you'd want
> >> to avoid by default. The definitions of the bins are
> >>
> >>
> >> [0, 4)
> >> [4, 8)
> >> [8, 12)
> >> [12, 16]
> >>
> >>
> >> and
> >>
> >>
> >> [0, 4)
> >> [4, 8)
> >> [8, 12)
> >> [12, 16)
> >> [16, 20]
> >>
> >>
> >> so you can see where the inconsistent behaviour comes from. You
> >> might be able to get R-core to add argument 'warn', but probably
> >> not to change the default of 'include.lowest'. I hope this helps
> >>
> >>
> >> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  >> > wrote:
> >>
> >> Thank you Andrew.
> >>
> >>
> >> Is there any reason not to make: include.lowest = TRUE the
> >> default?
> >>
> >>
> >> Regarding the NA:
> >>
> >> The user still has to suspect that some values were not
> >> included and run that test.
> >>
> >>
> >> Leonard
> >>
> >>
> >> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
> >>> Regarding your first point, argument 'include.lowest'
> >>> already handles this specific case, see ?.bincode
> >>>
> >>> Your second point, maybe it could be helpful, but since both
> >>> 'cut.default' and '.bincode' return NA if a value isn't
> >>> within a bin, you could make something like this on your own.
> >>> Might be worth pitching to R-bugs on the wishlist.
> >>>
> >>>
> >>>
> >>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help
> >>> mailto:r-help@r-project.org>> wrote:
> >>>
> >>> Hello List members,
> >>>
> >>>
> >>> the following improvements would be useful for function
> >>> cut (and .bincode):
> >>>
> >>>
> >>> 1.) Argument: Include extremes
> >>> extremes = TRUE
> >>> if(right == FALSE) {
> >>> # include also right for last interval;
> >>> } else {
> >>> # include also left for first interval;
> >>> }
> >>>
> >>>
> >>> 2.) Argument: warn = TRUE
> >>>
> >>> Warn if any values are not included in the intervals.
> >>>
> >>>
> >>> Motivation:
> >>> - reduce risk of errors when using function cut();
> >>>
> >>>
> >>> Sincerely,
> >>>
> >>>
> >>>  

Re: [R] Improvement: function cut

2021-09-17 Thread Leonard Mada via R-help

The warn should be in cut() => .bincode().

It should be generated whenever a real value (excludes NA or NAN or +/- 
Inf) is not included in any of the bins.



If the user writes a script and doesn't want any warnings: he can select 
warn = FALSE. But otherwise it would be very helpful to catch 
immediately the error (and not after a number of steps or miss the error 
altogether).



Leonard


On 9/18/2021 1:28 AM, Jeff Newmiller wrote:

Re your objection that "the user has to suspect that some values were not 
included" applies equally to your proposed warn option. There are a lot of ways to 
introduce NAs... in real projects all analysts should be suspecting this problem.

On September 17, 2021 3:01:35 PM PDT, Leonard Mada via R-help 
 wrote:

Thank you Andrew.


Is there any reason not to make: include.lowest = TRUE the default?


Regarding the NA:

The user still has to suspect that some values were not included and run
that test.


Leonard


On 9/18/2021 12:53 AM, Andrew Simmons wrote:

Regarding your first point, argument 'include.lowest' already handles
this specific case, see ?.bincode

Your second point, maybe it could be helpful, but since both
'cut.default' and '.bincode' return NA if a value isn't within a bin,
you could make something like this on your own.
Might be worth pitching to R-bugs on the wishlist.



On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help
mailto:r-help@r-project.org>> wrote:

 Hello List members,


 the following improvements would be useful for function cut (and
 .bincode):


 1.) Argument: Include extremes
 extremes = TRUE
 if(right == FALSE) {
     # include also right for last interval;
 } else {
     # include also left for first interval;
 }


 2.) Argument: warn = TRUE

 Warn if any values are not included in the intervals.


 Motivation:
 - reduce risk of errors when using function cut();


 Sincerely,


 Leonard

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


__
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] Improvement: function cut

2021-09-17 Thread Leonard Mada via R-help
Why would you want to merge different factors?

It makes no sense on real data. Even if some names are the same, the 
factors are not the same!


The only real-data application that springs to mind is censoring (right 
or left, depending on the choice): but here we have both open and closed 
intervals, e.g. to the right (in the same data-set).


Leonard


On 9/18/2021 1:29 AM, Andrew Simmons wrote:
> I disagree, I don't really think it's too long or ugly, but if you 
> think it is, you could abbreviate it as 'i'.
>
>
> x <- 0:20
> breaks1 <- seq.int (0, 16, 4)
> breaks2 <- seq.int (0, 20, 4)
> data.frame(
>     cut(x, breaks1, right = FALSE, i = TRUE),
>     cut(x, breaks2, right = FALSE, i = TRUE),
>     check.names = FALSE
> )
>
>
> I hope this helps.
>
> On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada  > wrote:
>
> Hello Andrew,
>
>
> But "cut" generates factors. In most cases with real data one
> expects to have also the ends of the interval: the argument
> "include.lowest" is both ugly and too long.
>
> [The test-code on the ftable thread contains this error! I have
> run through this error a couple of times.]
>
>
> The only real situation that I can imagine to be problematic:
>
> - if the interval goes to +Inf (or -Inf): I do not know if there
> would be any effects when including +Inf (or -Inf).
>
>
> Leonard
>
>
> On 9/18/2021 1:14 AM, Andrew Simmons wrote:
>> While it is not explicitly mentioned anywhere in the
>> documentation for .bincode, I suspect 'include.lowest = FALSE' is
>> the default to keep the definitions of the bins consistent. For
>> example:
>>
>>
>> x <- 0:20
>> breaks1 <- seq.int (0, 16, 4)
>> breaks2 <- seq.int (0, 20, 4)
>> cbind(
>>     .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
>>     .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
>> )
>>
>>
>> by having 'include.lowest = TRUE' with different ends, you can
>> get inconsistent behaviour. While this probably wouldn't be an
>> issue with 'real' data, this would seem like something you'd want
>> to avoid by default. The definitions of the bins are
>>
>>
>> [0, 4)
>> [4, 8)
>> [8, 12)
>> [12, 16]
>>
>>
>> and
>>
>>
>> [0, 4)
>> [4, 8)
>> [8, 12)
>> [12, 16)
>> [16, 20]
>>
>>
>> so you can see where the inconsistent behaviour comes from. You
>> might be able to get R-core to add argument 'warn', but probably
>> not to change the default of 'include.lowest'. I hope this helps
>>
>>
>> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada > > wrote:
>>
>> Thank you Andrew.
>>
>>
>> Is there any reason not to make: include.lowest = TRUE the
>> default?
>>
>>
>> Regarding the NA:
>>
>> The user still has to suspect that some values were not
>> included and run that test.
>>
>>
>> Leonard
>>
>>
>> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>>> Regarding your first point, argument 'include.lowest'
>>> already handles this specific case, see ?.bincode
>>>
>>> Your second point, maybe it could be helpful, but since both
>>> 'cut.default' and '.bincode' return NA if a value isn't
>>> within a bin, you could make something like this on your own.
>>> Might be worth pitching to R-bugs on the wishlist.
>>>
>>>
>>>
>>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help
>>> mailto:r-help@r-project.org>> wrote:
>>>
>>> Hello List members,
>>>
>>>
>>> the following improvements would be useful for function
>>> cut (and .bincode):
>>>
>>>
>>> 1.) Argument: Include extremes
>>> extremes = TRUE
>>> if(right == FALSE) {
>>>     # include also right for last interval;
>>> } else {
>>>     # include also left for first interval;
>>> }
>>>
>>>
>>> 2.) Argument: warn = TRUE
>>>
>>> Warn if any values are not included in the intervals.
>>>
>>>
>>> Motivation:
>>> - reduce risk of errors when using function cut();
>>>
>>>
>>> Sincerely,
>>>
>>>
>>> Leonard
>>>
>>> __
>>> 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] Improvement: function cut

2021-09-17 Thread Jeff Newmiller
Re your objection that "the user has to suspect that some values were not 
included" applies equally to your proposed warn option. There are a lot of ways 
to introduce NAs... in real projects all analysts should be suspecting this 
problem.

On September 17, 2021 3:01:35 PM PDT, Leonard Mada via R-help 
 wrote:
>Thank you Andrew.
>
>
>Is there any reason not to make: include.lowest = TRUE the default?
>
>
>Regarding the NA:
>
>The user still has to suspect that some values were not included and run 
>that test.
>
>
>Leonard
>
>
>On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>> Regarding your first point, argument 'include.lowest' already handles 
>> this specific case, see ?.bincode
>>
>> Your second point, maybe it could be helpful, but since both 
>> 'cut.default' and '.bincode' return NA if a value isn't within a bin, 
>> you could make something like this on your own.
>> Might be worth pitching to R-bugs on the wishlist.
>>
>>
>>
>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
>> mailto:r-help@r-project.org>> wrote:
>>
>> Hello List members,
>>
>>
>> the following improvements would be useful for function cut (and
>> .bincode):
>>
>>
>> 1.) Argument: Include extremes
>> extremes = TRUE
>> if(right == FALSE) {
>>     # include also right for last interval;
>> } else {
>>     # include also left for first interval;
>> }
>>
>>
>> 2.) Argument: warn = TRUE
>>
>> Warn if any values are not included in the intervals.
>>
>>
>> Motivation:
>> - reduce risk of errors when using function cut();
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> __
>> 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.

-- 
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] Improvement: function cut

2021-09-17 Thread Leonard Mada via R-help
Hello Andrew,


But "cut" generates factors. In most cases with real data one expects to 
have also the ends of the interval: the argument "include.lowest" is 
both ugly and too long.

[The test-code on the ftable thread contains this error! I have run 
through this error a couple of times.]


The only real situation that I can imagine to be problematic:

- if the interval goes to +Inf (or -Inf): I do not know if there would 
be any effects when including +Inf (or -Inf).


Leonard


On 9/18/2021 1:14 AM, Andrew Simmons wrote:
> While it is not explicitly mentioned anywhere in the documentation for 
> .bincode, I suspect 'include.lowest = FALSE' is the default to keep 
> the definitions of the bins consistent. For example:
>
>
> x <- 0:20
> breaks1 <- seq.int (0, 16, 4)
> breaks2 <- seq.int (0, 20, 4)
> cbind(
>     .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
>     .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
> )
>
>
> by having 'include.lowest = TRUE' with different ends, you can get 
> inconsistent behaviour. While this probably wouldn't be an issue with 
> 'real' data, this would seem like something you'd want to avoid by 
> default. The definitions of the bins are
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16]
>
>
> and
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16)
> [16, 20]
>
>
> so you can see where the inconsistent behaviour comes from. You might 
> be able to get R-core to add argument 'warn', but probably not to 
> change the default of 'include.lowest'. I hope this helps
>
>
> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  > wrote:
>
> Thank you Andrew.
>
>
> Is there any reason not to make: include.lowest = TRUE the default?
>
>
> Regarding the NA:
>
> The user still has to suspect that some values were not included
> and run that test.
>
>
> Leonard
>
>
> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>> Regarding your first point, argument 'include.lowest' already
>> handles this specific case, see ?.bincode
>>
>> Your second point, maybe it could be helpful, but since both
>> 'cut.default' and '.bincode' return NA if a value isn't within a
>> bin, you could make something like this on your own.
>> Might be worth pitching to R-bugs on the wishlist.
>>
>>
>>
>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help
>> mailto:r-help@r-project.org>> wrote:
>>
>> Hello List members,
>>
>>
>> the following improvements would be useful for function cut
>> (and .bincode):
>>
>>
>> 1.) Argument: Include extremes
>> extremes = TRUE
>> if(right == FALSE) {
>>     # include also right for last interval;
>> } else {
>>     # include also left for first interval;
>> }
>>
>>
>> 2.) Argument: warn = TRUE
>>
>> Warn if any values are not included in the intervals.
>>
>>
>> Motivation:
>> - reduce risk of errors when using function cut();
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> __
>> 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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
I disagree, I don't really think it's too long or ugly, but if you think it
is, you could abbreviate it as 'i'.


x <- 0:20
breaks1 <- seq.int(0, 16, 4)
breaks2 <- seq.int(0, 20, 4)
data.frame(
cut(x, breaks1, right = FALSE, i = TRUE),
cut(x, breaks2, right = FALSE, i = TRUE),
check.names = FALSE
)


I hope this helps.

On Fri, Sep 17, 2021 at 6:26 PM Leonard Mada  wrote:

> Hello Andrew,
>
>
> But "cut" generates factors. In most cases with real data one expects to
> have also the ends of the interval: the argument "include.lowest" is both
> ugly and too long.
>
> [The test-code on the ftable thread contains this error! I have run
> through this error a couple of times.]
>
>
> The only real situation that I can imagine to be problematic:
>
> - if the interval goes to +Inf (or -Inf): I do not know if there would be
> any effects when including +Inf (or -Inf).
>
>
> Leonard
>
>
> On 9/18/2021 1:14 AM, Andrew Simmons wrote:
>
> While it is not explicitly mentioned anywhere in the documentation for
> .bincode, I suspect 'include.lowest = FALSE' is the default to keep the
> definitions of the bins consistent. For example:
>
>
> x <- 0:20
> breaks1 <- seq.int(0, 16, 4)
> breaks2 <- seq.int(0, 20, 4)
> cbind(
> .bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
> .bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
> )
>
>
> by having 'include.lowest = TRUE' with different ends, you can get
> inconsistent behaviour. While this probably wouldn't be an issue with
> 'real' data, this would seem like something you'd want to avoid by default.
> The definitions of the bins are
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16]
>
>
> and
>
>
> [0, 4)
> [4, 8)
> [8, 12)
> [12, 16)
> [16, 20]
>
>
> so you can see where the inconsistent behaviour comes from. You might be
> able to get R-core to add argument 'warn', but probably not to change the
> default of 'include.lowest'. I hope this helps
>
>
> On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  wrote:
>
>> Thank you Andrew.
>>
>>
>> Is there any reason not to make: include.lowest = TRUE the default?
>>
>>
>> Regarding the NA:
>>
>> The user still has to suspect that some values were not included and run
>> that test.
>>
>>
>> Leonard
>>
>>
>> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>>
>> Regarding your first point, argument 'include.lowest' already handles
>> this specific case, see ?.bincode
>>
>> Your second point, maybe it could be helpful, but since both
>> 'cut.default' and '.bincode' return NA if a value isn't within a bin, you
>> could make something like this on your own.
>> Might be worth pitching to R-bugs on the wishlist.
>>
>>
>>
>> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
>> wrote:
>>
>>> Hello List members,
>>>
>>>
>>> the following improvements would be useful for function cut (and
>>> .bincode):
>>>
>>>
>>> 1.) Argument: Include extremes
>>> extremes = TRUE
>>> if(right == FALSE) {
>>> # include also right for last interval;
>>> } else {
>>> # include also left for first interval;
>>> }
>>>
>>>
>>> 2.) Argument: warn = TRUE
>>>
>>> Warn if any values are not included in the intervals.
>>>
>>>
>>> Motivation:
>>> - reduce risk of errors when using function cut();
>>>
>>>
>>> Sincerely,
>>>
>>>
>>> Leonard
>>>
>>> __
>>> 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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
While it is not explicitly mentioned anywhere in the documentation for
.bincode, I suspect 'include.lowest = FALSE' is the default to keep the
definitions of the bins consistent. For example:


x <- 0:20
breaks1 <- seq.int(0, 16, 4)
breaks2 <- seq.int(0, 20, 4)
cbind(
.bincode(x, breaks1, right = FALSE, include.lowest = TRUE),
.bincode(x, breaks2, right = FALSE, include.lowest = TRUE)
)


by having 'include.lowest = TRUE' with different ends, you can get
inconsistent behaviour. While this probably wouldn't be an issue with
'real' data, this would seem like something you'd want to avoid by default.
The definitions of the bins are


[0, 4)
[4, 8)
[8, 12)
[12, 16]


and


[0, 4)
[4, 8)
[8, 12)
[12, 16)
[16, 20]


so you can see where the inconsistent behaviour comes from. You might be
able to get R-core to add argument 'warn', but probably not to change the
default of 'include.lowest'. I hope this helps


On Fri, Sep 17, 2021 at 6:01 PM Leonard Mada  wrote:

> Thank you Andrew.
>
>
> Is there any reason not to make: include.lowest = TRUE the default?
>
>
> Regarding the NA:
>
> The user still has to suspect that some values were not included and run
> that test.
>
>
> Leonard
>
>
> On 9/18/2021 12:53 AM, Andrew Simmons wrote:
>
> Regarding your first point, argument 'include.lowest' already handles this
> specific case, see ?.bincode
>
> Your second point, maybe it could be helpful, but since both 'cut.default'
> and '.bincode' return NA if a value isn't within a bin, you could make
> something like this on your own.
> Might be worth pitching to R-bugs on the wishlist.
>
>
>
> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
> wrote:
>
>> Hello List members,
>>
>>
>> the following improvements would be useful for function cut (and
>> .bincode):
>>
>>
>> 1.) Argument: Include extremes
>> extremes = TRUE
>> if(right == FALSE) {
>> # include also right for last interval;
>> } else {
>> # include also left for first interval;
>> }
>>
>>
>> 2.) Argument: warn = TRUE
>>
>> Warn if any values are not included in the intervals.
>>
>>
>> Motivation:
>> - reduce risk of errors when using function cut();
>>
>>
>> Sincerely,
>>
>>
>> Leonard
>>
>> __
>> 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] Improvement: function cut

2021-09-17 Thread Leonard Mada via R-help
Thank you Andrew.


Is there any reason not to make: include.lowest = TRUE the default?


Regarding the NA:

The user still has to suspect that some values were not included and run 
that test.


Leonard


On 9/18/2021 12:53 AM, Andrew Simmons wrote:
> Regarding your first point, argument 'include.lowest' already handles 
> this specific case, see ?.bincode
>
> Your second point, maybe it could be helpful, but since both 
> 'cut.default' and '.bincode' return NA if a value isn't within a bin, 
> you could make something like this on your own.
> Might be worth pitching to R-bugs on the wishlist.
>
>
>
> On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
> mailto:r-help@r-project.org>> wrote:
>
> Hello List members,
>
>
> the following improvements would be useful for function cut (and
> .bincode):
>
>
> 1.) Argument: Include extremes
> extremes = TRUE
> if(right == FALSE) {
>     # include also right for last interval;
> } else {
>     # include also left for first interval;
> }
>
>
> 2.) Argument: warn = TRUE
>
> Warn if any values are not included in the intervals.
>
>
> Motivation:
> - reduce risk of errors when using function cut();
>
>
> Sincerely,
>
>
> Leonard
>
> __
> 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] Improvement: function cut

2021-09-17 Thread Andrew Simmons
Regarding your first point, argument 'include.lowest' already handles this
specific case, see ?.bincode

Your second point, maybe it could be helpful, but since both 'cut.default'
and '.bincode' return NA if a value isn't within a bin, you could make
something like this on your own.
Might be worth pitching to R-bugs on the wishlist.



On Fri, Sep 17, 2021, 17:45 Leonard Mada via R-help 
wrote:

> Hello List members,
>
>
> the following improvements would be useful for function cut (and .bincode):
>
>
> 1.) Argument: Include extremes
> extremes = TRUE
> if(right == FALSE) {
> # include also right for last interval;
> } else {
> # include also left for first interval;
> }
>
>
> 2.) Argument: warn = TRUE
>
> Warn if any values are not included in the intervals.
>
>
> Motivation:
> - reduce risk of errors when using function cut();
>
>
> Sincerely,
>
>
> Leonard
>
> __
> 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] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Eric Berger
Definitely doable and you are on the right path and maybe even close.
The error message you got showed your query as having the wrong info after
the 'FROM'
keyword

 ' SELECT * FROM c("BIODBX.MECCUNIQUE2", "BIODBX.QDATA_HTML_DUMMY",
"BIODBX.SET_ITEMS", "BIODBX.SET_NAMES", "dbo.sysdiagrams",
"GEMD.ASSAY_DEFINITIONS",

etc

i.e you are passing the full vector of table names and not a single one
(the first being the one specified in your working example)

You have to figure out how to correctly set up your loop on this vector of
table names.
The debugger - browser() - is your friend! Learn how to use it, as I
suggested before.



...



On Fri, Jul 2, 2021 at 10:47 PM Kai Yang  wrote:

> Hi Eric,
>
> Thank you spent time to help me for this.
>
>
> Here is the thing: I was requested to manage a sql server for my group.
> the server has many schemas and the tables (>200). I use ODBC to connect
> the server and get the schema name + table name into a data frame.
>
>
> For each of schema + table on server, I need to run a summary report. So I
> wrote a summary script like this:
>
>
> res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
>
> view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
>
> rm(res)
>
>
> the script works well. but I don't want to write 200+ times of the script
> to summary each table. So, I'm trying to write the function to do this.
> this is my goal.
>
>
> First of all, I'm not sure if this is the right way to do the summary
> report, because I'm a new R user. So please correct me if my idea is doable.
>
>
> Second, would you please tell me what is "more detail" information do you
> need?
>
>
> Thank you,
>
> Kai
>
>
>
> On Friday, July 2, 2021, 12:31:17 PM PDT, Eric Berger <
> ericjber...@gmail.com> wrote:
>
>
> Hard for me to tell without more details but it looks like the following
> has several bugs
>
> for (i in dbtable$Tot_table)
> {
>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
> i)))
>   summ(Tabname)
> }
>
> Your sprintf() statement seems to use 'i' but actually does not.
> You probably want to rewrite/rearrange this code. More like
>
> x <- sqldf("SELECT Tot_table FROM dbtable")
> for ( Tabname in x )
> summ(Tabname)
>
> no doubt this is wrong but put a browser() call after the x <- sqldf(...)
> line and inspect x and go from there
>
>
>
>
> On Fri, Jul 2, 2021 at 10:20 PM Kai Yang  wrote:
>
> Hello Eric,
>
> Following your suggestion, I modified the code as:
>
> summ <- function(Tabname){
>
>   query <- sprintf(" SELECT * FROM %s",Tabname)
>
>   res <- dbGetQuery(con, query)
>
>   view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
>
>   rm(res)
>
> }
>
>
> for (i in dbtable$Tot_table)
>
> {
>
>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
> i)))
>
>   summ(Tabname)
>
> }
>
> after submitted the work, I got the error message below:
>
>
>  Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for
> SQL Server][SQL Server]Invalid object name 'c'.  [Microsoft][ODBC Driver 17
> for SQL Server][SQL Server]Statement(s) could not be prepared.
>  ' SELECT * FROM c("BIODBX.MECCUNIQUE2", "BIODBX.QDATA_HTML_DUMMY",
> "BIODBX.SET_ITEMS", "BIODBX.SET_NAMES", "dbo.sysdiagrams",
> "GEMD.ASSAY_DEFINITIONS", "GEMD.ASSAY_DISCRETE_VALUES",
> "GEMD.ASSAY_QUESTIONS", "GEMD.ASSAY_RUNS", "GEMD.BIODBX_DATABASE_SEED",
> "GEMD.BIODBX_USER_SEEDS", "GEMD.BIODBX_USERS", "GEMD.DATA_ENTRY_PAGES",
> "GEMD.DISC_SESSION_QID", "GEMD.DISC_SESSION_STATUS",
> "GEMD.DISC_SESSION_TYPE", "GEMD.DISCREPANCIES",
> "GEMD.DISCREPANCY_QUERY_TEMP", "GEMD.DISCRETE_VALUES",
> "GEMD.ENTERED_DATA_ENTRY_PAGES", "GEMD.ENTRY_GROUPS",
> "GEMD.ExportSampleListNames", "GEMD.FORM_STATUS_BY_SUBJECT",
> "GEMD.GEMD_CODELIST_GROUPS", "GEMD.GEMD_CODELIST_VALUES",
> "GEMD.GEMD_LOT_DEFINITIONS", "GEMD.GEMD_SAMPLES", "GEMD.GEMD_STUDIES",
> "GEMD.MECCUNIQUE", "GEMD.MECCUNIQUE2", "GEMD.MISSING_DI
>
>
> One more question,  in the code of "*view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")*",
>
> can Tabname part be replacted automatic also?
>
> Thank you,
>
> Kai
> On Friday, July 2, 2021, 12:06:12 PM PDT, Eric Berger <
> ericjber...@gmail.com> wrote:
>
>
> Modify the summ() function to start like this
>
> summ <- function(Tabname){
>query <- sprintf(" SELECT * FROM %s",Tabname)
>   res <- dbGetQuery(con, query)
>
> etc
>
> HTH,
> Eric
>
> On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help 
> wrote:
>
> Hello List,
>
> The previous post look massy. I repost my question. Sorry,
>
>
> I need to generate summary report for many tables (>200 tables). For each
> table, I can use the script to generate report:
> res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
> view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
> rm(res)
> BIODBX.MECCUNIQUE2 is the name of table.
>
> I have all of tables' name in a data 

Re: [R] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Kai Yang via R-help
 Hi Eric,
Thank you spent time to help me for this.

Here is the thing: I was requested to manage a sql server for my group. the 
server has many schemas and the tables (>200). I use ODBC to connect the server 
and get the schema name + table name into a data frame.

For each of schema + table on server, I need to run a summary report. So I 
wrote a summary script like this:

res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
rm(res)

the script works well. but I don't want to write 200+ times of the script to 
summary each table. So, I'm trying to write the function to do this. this is my 
goal.

First of all, I'm not sure if this is the right way to do the summary report, 
because I'm a new R user. So please correct me if my idea is doable.

Second, would you please tell me what is "more detail" information do you need?

Thank you,
Kai


On Friday, July 2, 2021, 12:31:17 PM PDT, Eric Berger 
 wrote:  
 
 Hard for me to tell without more details but it looks like the following has 
several bugs
for (i in dbtable$Tot_table){
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable", i)))
  summ(Tabname)
}

Your sprintf() statement seems to use 'i' but actually does not.You probably 
want to rewrite/rearrange this code. More like
x <- sqldf("SELECT Tot_table FROM dbtable")for ( Tabname in x )summ(Tabname)
no doubt this is wrong but put a browser() call after the x <- sqldf(...)line 
and inspect x and go from there



On Fri, Jul 2, 2021 at 10:20 PM Kai Yang  wrote:

 Hello Eric,
Following your suggestion, I modified the code as:
summ <- function(Tabname){
  query <- sprintf(" SELECT * FROM %s",Tabname)
  res <- dbGetQuery(con, query)
  view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
  rm(res)
}

for (i in dbtable$Tot_table)
{
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable", i)))
  summ(Tabname)
}
after submitted the work, I got the error message below:

 Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Invalid object name 'c'.  [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Statement(s) could not be prepared.  ' SELECT * FROM 
c("BIODBX.MECCUNIQUE2", "BIODBX.QDATA_HTML_DUMMY", "BIODBX.SET_ITEMS", 
"BIODBX.SET_NAMES", "dbo.sysdiagrams", "GEMD.ASSAY_DEFINITIONS", 
"GEMD.ASSAY_DISCRETE_VALUES", "GEMD.ASSAY_QUESTIONS", "GEMD.ASSAY_RUNS", 
"GEMD.BIODBX_DATABASE_SEED", "GEMD.BIODBX_USER_SEEDS", "GEMD.BIODBX_USERS", 
"GEMD.DATA_ENTRY_PAGES", "GEMD.DISC_SESSION_QID", "GEMD.DISC_SESSION_STATUS", 
"GEMD.DISC_SESSION_TYPE", "GEMD.DISCREPANCIES", "GEMD.DISCREPANCY_QUERY_TEMP", 
"GEMD.DISCRETE_VALUES", "GEMD.ENTERED_DATA_ENTRY_PAGES", "GEMD.ENTRY_GROUPS", 
"GEMD.ExportSampleListNames", "GEMD.FORM_STATUS_BY_SUBJECT", 
"GEMD.GEMD_CODELIST_GROUPS", "GEMD.GEMD_CODELIST_VALUES", 
"GEMD.GEMD_LOT_DEFINITIONS", "GEMD.GEMD_SAMPLES", "GEMD.GEMD_STUDIES", 
"GEMD.MECCUNIQUE", "GEMD.MECCUNIQUE2", "GEMD.MISSING_DI 

One more question,  in the code of "view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")",
can Tabname part be replacted automatic also? 
Thank you,
KaiOn Friday, July 2, 2021, 12:06:12 PM PDT, Eric Berger 
 wrote:  
 
 Modify the summ() function to start like this 
summ <- function(Tabname){   query <- sprintf("SELECT * FROM %s",Tabname)
  res <- dbGetQuery(con, query)
 
etc
HTH,Eric
On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help  wrote:

Hello List,

The previous post look massy. I repost my question. Sorry,


I need to generate summary report for many tables (>200 tables). For each 
table, I can use the script to generate report:
res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
rm(res)
BIODBX.MECCUNIQUE2 is the name of table.

I have all of tables' name in a data frame. So, I'm trying to write a function 
to do this:
summ <- function(Tabname){
  res <- dbGetQuery(con, "SELECT * FROM Tabname")
  view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
  rm(res)
}
for (i in dbtable$Tot_table)
{
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable", i)))
  summ(Tabname)
}

1. I created  a function summ, the argument is Tabname. I put the Tabname in 
the function. I hope it can be replaced one by one
2. the table dbtable contents all tables' name (>200 rows), the field name is 
Tot_table
3. I want use "for" to establish a loop, which can automatic generate a summary 
report for each table

but I got error message below:
 Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Invalid object name 'Tabname'.  
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be 
prepared. 

 'SELECT * FROM Tabname' 
10. stop(structure(list(message = 

Re: [R] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Jeff Newmiller
Not all advice received on the Internet is safe.

https://xkcd.com/327

https://db.rstudio.com/best-practices/run-queries-safely

It is not that much more difficult to do it right.

On July 2, 2021 12:05:43 PM PDT, Eric Berger  wrote:
>Modify the summ() function to start like this
>
>summ <- function(Tabname){
>   query <- sprintf(" SELECT * FROM %s",Tabname)
>  res <- dbGetQuery(con, query)
>
>etc
>
>HTH,
>Eric
>
>On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help
>
>wrote:
>
>> Hello List,
>>
>> The previous post look massy. I repost my question. Sorry,
>>
>>
>> I need to generate summary report for many tables (>200 tables). For
>each
>> table, I can use the script to generate report:
>> res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
>> view(dfSummary(res), file =
>> "W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
>> rm(res)
>> BIODBX.MECCUNIQUE2 is the name of table.
>>
>> I have all of tables' name in a data frame. So, I'm trying to write a
>> function to do this:
>> summ <- function(Tabname){
>>   res <- dbGetQuery(con, "SELECT * FROM Tabname")
>>   view(dfSummary(res), file =
>> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
>>   rm(res)
>> }
>> for (i in dbtable$Tot_table)
>> {
>>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM
>dbtable",
>> i)))
>>   summ(Tabname)
>> }
>>
>> 1. I created  a function summ, the argument is Tabname. I put the
>Tabname
>> in the function. I hope it can be replaced one by one
>> 2. the table dbtable contents all tables' name (>200 rows), the field
>name
>> is Tot_table
>> 3. I want use "for" to establish a loop, which can automatic generate
>a
>> summary report for each table
>>
>> but I got error message below:
>>  Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17
>for
>> SQL Server][SQL Server]Invalid object name 'Tabname'.
>> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s)
>could
>> not be prepared.
>>
>>  'SELECT * FROM Tabname'
>> 10. stop(structure(list(message = "nanodbc/nanodbc.cpp:1655: 42000:
>> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid
>> object name 'Tabname'.  [Microsoft][ODBC Driver 17 for SQL
>Server][SQL
>> Server]Statement(s) could not be prepared. \n
>> 'SELECT * FROM Tabname'",
>> call = NULL, cppstack = NULL), class = c("odbc::odbc_error",
>> "C++Error", "error", "condition")))
>> 9.new_result(connection@ptr, statement, immediate)
>> 8.OdbcResult(connection = conn, statement = statement, params =
>params,
>>  immediate = immediate)
>> 7..local(conn, statement, ...)
>> 6.dbSendQuery(conn, statement, params = params, ...)
>> 5.dbSendQuery(conn, statement, params = params, ...)
>> 4..local(conn, statement, ...)
>> 3.dbGetQuery(con, "SELECT * FROM Tabname")
>> 2.dbGetQuery(con, "SELECT * FROM Tabname")
>> 1.summ(Tabname)
>>
>> it seems the tables' name is not successfully pass into query. can
>someone
>> give me an instruction for this?
>> many thanks,
>> 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.
>>
>
>   [[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.

-- 
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] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Eric Berger
Hard for me to tell without more details but it looks like the following
has several bugs

for (i in dbtable$Tot_table)
{
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
i)))
  summ(Tabname)
}

Your sprintf() statement seems to use 'i' but actually does not.
You probably want to rewrite/rearrange this code. More like

x <- sqldf("SELECT Tot_table FROM dbtable")
for ( Tabname in x )
summ(Tabname)

no doubt this is wrong but put a browser() call after the x <- sqldf(...)
line and inspect x and go from there




On Fri, Jul 2, 2021 at 10:20 PM Kai Yang  wrote:

> Hello Eric,
>
> Following your suggestion, I modified the code as:
>
> summ <- function(Tabname){
>
>   query <- sprintf(" SELECT * FROM %s",Tabname)
>
>   res <- dbGetQuery(con, query)
>
>   view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
>
>   rm(res)
>
> }
>
>
> for (i in dbtable$Tot_table)
>
> {
>
>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
> i)))
>
>   summ(Tabname)
>
> }
>
> after submitted the work, I got the error message below:
>
>
>  Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for
> SQL Server][SQL Server]Invalid object name 'c'.  [Microsoft][ODBC Driver 17
> for SQL Server][SQL Server]Statement(s) could not be prepared.
>  ' SELECT * FROM c("BIODBX.MECCUNIQUE2", "BIODBX.QDATA_HTML_DUMMY",
> "BIODBX.SET_ITEMS", "BIODBX.SET_NAMES", "dbo.sysdiagrams",
> "GEMD.ASSAY_DEFINITIONS", "GEMD.ASSAY_DISCRETE_VALUES",
> "GEMD.ASSAY_QUESTIONS", "GEMD.ASSAY_RUNS", "GEMD.BIODBX_DATABASE_SEED",
> "GEMD.BIODBX_USER_SEEDS", "GEMD.BIODBX_USERS", "GEMD.DATA_ENTRY_PAGES",
> "GEMD.DISC_SESSION_QID", "GEMD.DISC_SESSION_STATUS",
> "GEMD.DISC_SESSION_TYPE", "GEMD.DISCREPANCIES",
> "GEMD.DISCREPANCY_QUERY_TEMP", "GEMD.DISCRETE_VALUES",
> "GEMD.ENTERED_DATA_ENTRY_PAGES", "GEMD.ENTRY_GROUPS",
> "GEMD.ExportSampleListNames", "GEMD.FORM_STATUS_BY_SUBJECT",
> "GEMD.GEMD_CODELIST_GROUPS", "GEMD.GEMD_CODELIST_VALUES",
> "GEMD.GEMD_LOT_DEFINITIONS", "GEMD.GEMD_SAMPLES", "GEMD.GEMD_STUDIES",
> "GEMD.MECCUNIQUE", "GEMD.MECCUNIQUE2", "GEMD.MISSING_DI
>
>
> One more question,  in the code of "*view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")*",
>
> can Tabname part be replacted automatic also?
>
> Thank you,
>
> Kai
> On Friday, July 2, 2021, 12:06:12 PM PDT, Eric Berger <
> ericjber...@gmail.com> wrote:
>
>
> Modify the summ() function to start like this
>
> summ <- function(Tabname){
>query <- sprintf(" SELECT * FROM %s",Tabname)
>   res <- dbGetQuery(con, query)
>
> etc
>
> HTH,
> Eric
>
> On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help 
> wrote:
>
> Hello List,
>
> The previous post look massy. I repost my question. Sorry,
>
>
> I need to generate summary report for many tables (>200 tables). For each
> table, I can use the script to generate report:
> res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
> view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
> rm(res)
> BIODBX.MECCUNIQUE2 is the name of table.
>
> I have all of tables' name in a data frame. So, I'm trying to write a
> function to do this:
> summ <- function(Tabname){
>   res <- dbGetQuery(con, "SELECT * FROM Tabname")
>   view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
>   rm(res)
> }
> for (i in dbtable$Tot_table)
> {
>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
> i)))
>   summ(Tabname)
> }
>
> 1. I created  a function summ, the argument is Tabname. I put the Tabname
> in the function. I hope it can be replaced one by one
> 2. the table dbtable contents all tables' name (>200 rows), the field name
> is Tot_table
> 3. I want use "for" to establish a loop, which can automatic generate a
> summary report for each table
>
> but I got error message below:
>  Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for
> SQL Server][SQL Server]Invalid object name 'Tabname'.
> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could
> not be prepared.
>
>  'SELECT * FROM Tabname'
> 10. stop(structure(list(message = "nanodbc/nanodbc.cpp:1655: 42000:
> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid
> object name 'Tabname'.  [Microsoft][ODBC Driver 17 for SQL Server][SQL
> Server]Statement(s) could not be prepared. \n
> 'SELECT * FROM Tabname'",
> call = NULL, cppstack = NULL), class = c("odbc::odbc_error",
> "C++Error", "error", "condition")))
> 9.new_result(connection@ptr, statement, immediate)
> 8.OdbcResult(connection = conn, statement = statement, params = params,
>  immediate = immediate)
> 7..local(conn, statement, ...)
> 6.dbSendQuery(conn, statement, params = params, ...)
> 5.dbSendQuery(conn, statement, params = params, ...)
> 4..local(conn, statement, ...)
> 3.dbGetQuery(con, "SELECT * FROM Tabname")
> 2.dbGetQuery(con, "SELECT * FROM Tabname")
> 1.summ(Tabname)
>
> it seems the 

Re: [R] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Kai Yang via R-help
 Hello Eric,
Following your suggestion, I modified the code as:
summ <- function(Tabname){
  query <- sprintf(" SELECT * FROM %s",Tabname)
  res <- dbGetQuery(con, query)
  view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
  rm(res)
}

for (i in dbtable$Tot_table)
{
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable", i)))
  summ(Tabname)
}
after submitted the work, I got the error message below:

 Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Invalid object name 'c'.  [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Statement(s) could not be prepared.  ' SELECT * FROM 
c("BIODBX.MECCUNIQUE2", "BIODBX.QDATA_HTML_DUMMY", "BIODBX.SET_ITEMS", 
"BIODBX.SET_NAMES", "dbo.sysdiagrams", "GEMD.ASSAY_DEFINITIONS", 
"GEMD.ASSAY_DISCRETE_VALUES", "GEMD.ASSAY_QUESTIONS", "GEMD.ASSAY_RUNS", 
"GEMD.BIODBX_DATABASE_SEED", "GEMD.BIODBX_USER_SEEDS", "GEMD.BIODBX_USERS", 
"GEMD.DATA_ENTRY_PAGES", "GEMD.DISC_SESSION_QID", "GEMD.DISC_SESSION_STATUS", 
"GEMD.DISC_SESSION_TYPE", "GEMD.DISCREPANCIES", "GEMD.DISCREPANCY_QUERY_TEMP", 
"GEMD.DISCRETE_VALUES", "GEMD.ENTERED_DATA_ENTRY_PAGES", "GEMD.ENTRY_GROUPS", 
"GEMD.ExportSampleListNames", "GEMD.FORM_STATUS_BY_SUBJECT", 
"GEMD.GEMD_CODELIST_GROUPS", "GEMD.GEMD_CODELIST_VALUES", 
"GEMD.GEMD_LOT_DEFINITIONS", "GEMD.GEMD_SAMPLES", "GEMD.GEMD_STUDIES", 
"GEMD.MECCUNIQUE", "GEMD.MECCUNIQUE2", "GEMD.MISSING_DI 

One more question,  in the code of "view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")",
can Tabname part be replacted automatic also? 
Thank you,
KaiOn Friday, July 2, 2021, 12:06:12 PM PDT, Eric Berger 
 wrote:  
 
 Modify the summ() function to start like this 
summ <- function(Tabname){   query <- sprintf("SELECT * FROM %s",Tabname)
  res <- dbGetQuery(con, query)
 
etc
HTH,Eric
On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help  wrote:

Hello List,

The previous post look massy. I repost my question. Sorry,


I need to generate summary report for many tables (>200 tables). For each 
table, I can use the script to generate report:
res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
rm(res)
BIODBX.MECCUNIQUE2 is the name of table.

I have all of tables' name in a data frame. So, I'm trying to write a function 
to do this:
summ <- function(Tabname){
  res <- dbGetQuery(con, "SELECT * FROM Tabname")
  view(dfSummary(res), file = 
"W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
  rm(res)
}
for (i in dbtable$Tot_table)
{
  Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable", i)))
  summ(Tabname)
}

1. I created  a function summ, the argument is Tabname. I put the Tabname in 
the function. I hope it can be replaced one by one
2. the table dbtable contents all tables' name (>200 rows), the field name is 
Tot_table
3. I want use "for" to establish a loop, which can automatic generate a summary 
report for each table

but I got error message below:
 Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for SQL 
Server][SQL Server]Invalid object name 'Tabname'.  
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be 
prepared. 

 'SELECT * FROM Tabname' 
10. stop(structure(list(message = "nanodbc/nanodbc.cpp:1655: 42000: 
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid 
object name 'Tabname'.  [Microsoft][ODBC Driver 17 for SQL Server][SQL 
Server]Statement(s) could not be prepared. \n 
'SELECT * FROM Tabname'", 
    call = NULL, cppstack = NULL), class = c("odbc::odbc_error", 
"C++Error", "error", "condition"))) 
9.new_result(connection@ptr, statement, immediate) 
8.OdbcResult(connection = conn, statement = statement, params = params,     
immediate = immediate) 
7..local(conn, statement, ...) 
6.dbSendQuery(conn, statement, params = params, ...) 
5.dbSendQuery(conn, statement, params = params, ...) 
4..local(conn, statement, ...) 
3.dbGetQuery(con, "SELECT * FROM Tabname") 
2.dbGetQuery(con, "SELECT * FROM Tabname") 
1.summ(Tabname) 

it seems the tables' name is not successfully pass into query. can someone give 
me an instruction for this?
many thanks,
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.

  
[[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] R Function question, (repost to fix the messy work format)

2021-07-02 Thread Eric Berger
Modify the summ() function to start like this

summ <- function(Tabname){
   query <- sprintf(" SELECT * FROM %s",Tabname)
  res <- dbGetQuery(con, query)

etc

HTH,
Eric

On Fri, Jul 2, 2021 at 9:39 PM Kai Yang via R-help 
wrote:

> Hello List,
>
> The previous post look massy. I repost my question. Sorry,
>
>
> I need to generate summary report for many tables (>200 tables). For each
> table, I can use the script to generate report:
> res <- dbGetQuery(con, "SELECT * FROM BIODBX.MECCUNIQUE2")
> view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.BIODBX.MECCUNIQUE2.html")
> rm(res)
> BIODBX.MECCUNIQUE2 is the name of table.
>
> I have all of tables' name in a data frame. So, I'm trying to write a
> function to do this:
> summ <- function(Tabname){
>   res <- dbGetQuery(con, "SELECT * FROM Tabname")
>   view(dfSummary(res), file =
> "W:/project/_Joe.B/MSSQL/try/summarytools.Tabname.html")
>   rm(res)
> }
> for (i in dbtable$Tot_table)
> {
>   Tabname <- as.character(sqldf(sprintf("SELECT Tot_table FROM dbtable",
> i)))
>   summ(Tabname)
> }
>
> 1. I created  a function summ, the argument is Tabname. I put the Tabname
> in the function. I hope it can be replaced one by one
> 2. the table dbtable contents all tables' name (>200 rows), the field name
> is Tot_table
> 3. I want use "for" to establish a loop, which can automatic generate a
> summary report for each table
>
> but I got error message below:
>  Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 17 for
> SQL Server][SQL Server]Invalid object name 'Tabname'.
> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could
> not be prepared.
>
>  'SELECT * FROM Tabname'
> 10. stop(structure(list(message = "nanodbc/nanodbc.cpp:1655: 42000:
> [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid
> object name 'Tabname'.  [Microsoft][ODBC Driver 17 for SQL Server][SQL
> Server]Statement(s) could not be prepared. \n
> 'SELECT * FROM Tabname'",
> call = NULL, cppstack = NULL), class = c("odbc::odbc_error",
> "C++Error", "error", "condition")))
> 9.new_result(connection@ptr, statement, immediate)
> 8.OdbcResult(connection = conn, statement = statement, params = params,
>  immediate = immediate)
> 7..local(conn, statement, ...)
> 6.dbSendQuery(conn, statement, params = params, ...)
> 5.dbSendQuery(conn, statement, params = params, ...)
> 4..local(conn, statement, ...)
> 3.dbGetQuery(con, "SELECT * FROM Tabname")
> 2.dbGetQuery(con, "SELECT * FROM Tabname")
> 1.summ(Tabname)
>
> it seems the tables' name is not successfully pass into query. can someone
> give me an instruction for this?
> many thanks,
> 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.
>

[[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] .Last function in R 4.1.0

2021-06-07 Thread Duncan Murdoch
And to expand a bit:  only the site file tries to put objects into the 
base package.  The local .Rprofile file would put them into the global 
environment.  So there's still support for .First and .Last, but it has 
changed.


This abuses things a bit, but appears to work in an Rprofile.site file:

assign(".Last", function() if (interactive()) message("Goodbye!"), 
.AutoloadEnv)


The .AutoloadEnv environment is the place that autoload() writes in the 
base package, and it's generally on the search path, so the .Last 
function will be found.  I haven't checked whether it will be run if 
there's also a .Last defined in the global environment.


Duncan Murdoch

On 07/06/2021 10:24 a.m., Bert Gunter wrote:

According to the News file for 4.1.0 -- you should always check there first
for such things --

"The base environment and its namespace are now locked (so one can no
longer add bindings to these or remove from these)."

So the docs do seem to need updating.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Jun 7, 2021 at 12:15 AM Paul Louisell  wrote:


Here's my relevant OS / version info for background:
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)
Here's the problem: I've inserted the following lines *only *into my
Rprofile.site file in the etc folder:
if(interactive()){
.Last=function(){
timestamp(stamp=date(), prefix = "##-- ", suffix = " --##")
savehistory()
}
}
This is a basic .Last function to automatically insert a timestamp into the
history and save it at the end of an interactive R session. Prior to R
4.1.0, I'd never received an error from these lines. But in version 4.1.0
on startup, I get this error:
Error: cannot add binding of '.Last' to the base environment

And (as expected given the error) the .Last function is not in the
workspace:

.Last

Error: object '.Last' not found
The same thing happens when I start up the 32-bit version. Section 10.8
(pg. 54) of *R-intro.pdf *still has the documentation for .First and .Last,
and shows examples where you place the function definitions in the
Rprofile.site file, as well as other locations. *fullrefman.pdf *also has
documentation for these functions. So my question:

- Is this a bug? Or does R not support .First / .Last anymore, and the
documents just need updating?

Thanks for your help with this,
Paul Louisell

 [[alternative HTML version deleted]]

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



[[alternative HTML version deleted]]

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



__
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] .Last function in R 4.1.0

2021-06-07 Thread Bert Gunter
According to the News file for 4.1.0 -- you should always check there first
for such things --

"The base environment and its namespace are now locked (so one can no
longer add bindings to these or remove from these)."

So the docs do seem to need updating.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Mon, Jun 7, 2021 at 12:15 AM Paul Louisell  wrote:

> Here's my relevant OS / version info for background:
> R version 4.1.0 (2021-05-18)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 19043)
> Here's the problem: I've inserted the following lines *only *into my
> Rprofile.site file in the etc folder:
> if(interactive()){
> .Last=function(){
> timestamp(stamp=date(), prefix = "##-- ", suffix = " --##")
> savehistory()
> }
> }
> This is a basic .Last function to automatically insert a timestamp into the
> history and save it at the end of an interactive R session. Prior to R
> 4.1.0, I'd never received an error from these lines. But in version 4.1.0
> on startup, I get this error:
> Error: cannot add binding of '.Last' to the base environment
>
> And (as expected given the error) the .Last function is not in the
> workspace:
> > .Last
> Error: object '.Last' not found
> The same thing happens when I start up the 32-bit version. Section 10.8
> (pg. 54) of *R-intro.pdf *still has the documentation for .First and .Last,
> and shows examples where you place the function definitions in the
> Rprofile.site file, as well as other locations. *fullrefman.pdf *also has
> documentation for these functions. So my question:
>
>- Is this a bug? Or does R not support .First / .Last anymore, and the
>documents just need updating?
>
> Thanks for your help with this,
> Paul Louisell
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] Optimization function producing negative parameter values

2021-03-21 Thread J C Nash
This is likely because Hessian is being approximated.

Numerical approximation to Hessian will overstep the bounds because
the routines that are called don't respect the bounds (they likely
don't have the bounds available).

Writing numerical approximations that respect bounds and other constraints
is an interesting and very challenging problem. It's likely a lot easier to
do the work and get the derivatives analytically.

JN

On 2021-03-21 1:50 p.m., Bill Dunlap wrote:
> Does optim go out of bounds when you specify hessian=FALSE?
> hessian=TRUE causes some out-of-bounds evaluations of f.
> 
>> optim(c(X=1,Y=1), 
>> function(XY){print(unname(XY));(XY[["X"]]+1)^4+(XY[["Y"]]-2)^4}, method= 
>> "L-BFGS-B", lower=c(0.001,0.001), upper=c(1.5,1.5), hessian=TRUE)
> [1] 1 1
> [1] 1.001 1.000
> [1] 0.999 1.000
> [1] 1.000 1.001
> [1] 1.000 0.999
> [1] 0.001 1.500
> [1] 0.002 1.500
> [1] 0.001 1.500
> [1] 0.001 1.500
> [1] 0.001 1.499
> [1] 0.003 1.500
> [1] 0.001 1.500
> [1] 0.002 1.501
> [1] 0.002 1.499
> [1] 0.001 1.500
> [1] -0.001  1.500
> [1] 0.000 1.501
> [1] 0.000 1.499
> [1] 0.002 1.501
> [1] 0.000 1.501
> [1] 0.001 1.502
> [1] 0.001 1.500
> [1] 0.002 1.499
> [1] 0.000 1.499
> [1] 0.001 1.500
> [1] 0.001 1.498
> $par
> X Y
> 0.001 1.500
> 
> $value
> [1] 1.066506
> 
> On Sun, Mar 21, 2021 at 10:22 AM Shah Alam  wrote:
>>
>> Dear all,
>>
>> I am using optim() to estimate unknown parameters by minimizing the
>> residual sums of squares. I created a function with the model. The model is
>> working fine. The optim function is producing negative parameter values, even
>> I have introduced upper and lower bounds (given in code). Therefore,
>> the model produces *NAs*.
>>
>> Following is my code.
>>
>> param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial
>>> parameter values
>>> opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B",
>>> lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001),
>>
>> upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01),
>>> control=list(maxit=10), hessian = T)
>>
>>
>> Error:
>>
>> *"NAs producedError in if (rnd_1 < liferisk) { : missing value where
>> TRUE/FALSE needed "*
>>
>> The model function which produces NA due to negative parameter values
>>
>> liferisk <- rnorm(n = 1, mean =
>> (calib_para[which(names(calib_para)=="r_mu")]),sd =
>> (calib_para[which(names(calib_para)=="r_sd")]))
>>
>>   rnd_1 <- runif(1, 0, 1)
>>
>>   if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0}
>>
>>
>> How to design/ modify optim() function, and upper-lower bounds to stop
>> producing negative values during parameter search?
>> Thanks
>>
>> Best regards,
>> Shah
>>
>> [[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] Optimization function producing negative parameter values

2021-03-21 Thread Bill Dunlap
Does optim go out of bounds when you specify hessian=FALSE?
hessian=TRUE causes some out-of-bounds evaluations of f.

> optim(c(X=1,Y=1), 
> function(XY){print(unname(XY));(XY[["X"]]+1)^4+(XY[["Y"]]-2)^4}, method= 
> "L-BFGS-B", lower=c(0.001,0.001), upper=c(1.5,1.5), hessian=TRUE)
[1] 1 1
[1] 1.001 1.000
[1] 0.999 1.000
[1] 1.000 1.001
[1] 1.000 0.999
[1] 0.001 1.500
[1] 0.002 1.500
[1] 0.001 1.500
[1] 0.001 1.500
[1] 0.001 1.499
[1] 0.003 1.500
[1] 0.001 1.500
[1] 0.002 1.501
[1] 0.002 1.499
[1] 0.001 1.500
[1] -0.001  1.500
[1] 0.000 1.501
[1] 0.000 1.499
[1] 0.002 1.501
[1] 0.000 1.501
[1] 0.001 1.502
[1] 0.001 1.500
[1] 0.002 1.499
[1] 0.000 1.499
[1] 0.001 1.500
[1] 0.001 1.498
$par
X Y
0.001 1.500

$value
[1] 1.066506

On Sun, Mar 21, 2021 at 10:22 AM Shah Alam  wrote:
>
> Dear all,
>
> I am using optim() to estimate unknown parameters by minimizing the
> residual sums of squares. I created a function with the model. The model is
> working fine. The optim function is producing negative parameter values, even
> I have introduced upper and lower bounds (given in code). Therefore,
> the model produces *NAs*.
>
> Following is my code.
>
> param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial
> > parameter values
> > opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B",
> > lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001),
>
> upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01),
> > control=list(maxit=10), hessian = T)
>
>
> Error:
>
> *"NAs producedError in if (rnd_1 < liferisk) { : missing value where
> TRUE/FALSE needed "*
>
> The model function which produces NA due to negative parameter values
>
> liferisk <- rnorm(n = 1, mean =
> (calib_para[which(names(calib_para)=="r_mu")]),sd =
> (calib_para[which(names(calib_para)=="r_sd")]))
>
>   rnd_1 <- runif(1, 0, 1)
>
>   if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0}
>
>
> How to design/ modify optim() function, and upper-lower bounds to stop
> producing negative values during parameter search?
> Thanks
>
> Best regards,
> Shah
>
> [[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] Optimization function producing negative parameter values

2021-03-21 Thread J C Nash
Can you put together your example as a single runnable scipt?

If so, I'll try some other tools to see what is going on. There
have been rumours of some glitches in the L-BFGS-B R implementation,
but so far I've not been able to acquire any that I can reproduce.

John Nash (maintainer of optimx package and some other optimization tools)





On 2021-03-21 1:20 p.m., Shah Alam wrote:
> Dear all,
> 
> I am using optim() to estimate unknown parameters by minimizing the
> residual sums of squares. I created a function with the model. The model is
> working fine. The optim function is producing negative parameter values, even
> I have introduced upper and lower bounds (given in code). Therefore,
> the model produces *NAs*.
> 
> Following is my code.
> 
> param <<- c(0.002,0.002, 0.14,0.012,0.01,0.02, 0.03, 0.001)# initial
>> parameter values
>> opt <- optim(param, fn= f.opt, obsdata =obsdata_1, method= "L-BFGS-B",
>> lower = c(0.001, 0.001, 0.08,0.008, 0.009, 0.008, 0.009, 0.001),
> 
> upper = c(0.00375, 0.002, 0.2, 0.018, 0.08, 0.08, 0.08, 0.01),
>> control=list(maxit=10), hessian = T)
> 
> 
> Error:
> 
> *"NAs producedError in if (rnd_1 < liferisk) { : missing value where
> TRUE/FALSE needed "*
> 
> The model function which produces NA due to negative parameter values
> 
> liferisk <- rnorm(n = 1, mean =
> (calib_para[which(names(calib_para)=="r_mu")]),sd =
> (calib_para[which(names(calib_para)=="r_sd")]))
> 
>   rnd_1 <- runif(1, 0, 1)
> 
>   if (rnd_1 < liferisk) { ca_case <- 1} else {ca_case <- 0}
> 
> 
> How to design/ modify optim() function, and upper-lower bounds to stop
> producing negative values during parameter search?
> Thanks
> 
> Best regards,
> Shah
> 
>   [[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] mpfr function in Rmpfr crashes R

2021-03-07 Thread Duncan Murdoch

It works for me, on a slightly different system than yours:

> Rmpfr::mpfr(pi, 120)
1 'mpfr' number of precision  120   bits
[1] 3.1415926535897931159979634685441851616
> sessionInfo()
R version 4.0.3 Patched (2021-01-30 r79912)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

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


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

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

other attached packages:
[1] Rmpfr_0.8-2 gmp_0.6-1

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


On 06/03/2021 7:07 p.m., Roger Bos wrote:

All,

The following code crashes by R on my mac with a message "R session
aborted.  A fatal error occured".

```
library(Rmpfr)
Rmpfr::mpfr(pi, 120)
```

Does anyone have any suggestions?   My session info is below:

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK:
/Library/Frameworks/R.framework/Versions/4.0/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] datasets  utils stats graphics  grDevices methods   base

other attached packages:
  [1] alphavantager_0.1.2 googlesheets4_0.2.0 googledrive_1.0.1
clipr_0.7.1
  [5] jsonlite_1.7.2  stringi_1.5.3   dtplyr_1.0.1
  data.table_1.13.6
  [9] dplyr_1.0.4 plyr_1.8.6  testthat_3.0.1
  lubridate_1.7.9.2
[13] timeDate_3043.102   sendmailR_1.2-1 rmarkdown_2.6
devtools_2.3.2
[17] usethis_2.0.0   xts_0.12.1  zoo_1.8-8
MASS_7.3-53
[21] fortunes_1.5-4

loaded via a namespace (and not attached):
  [1] tinytex_0.29  tidyselect_1.1.0  xfun_0.20 remotes_2.2.0
   purrr_0.3.4
  [6] gargle_0.5.0  lattice_0.20-41   generics_0.1.0vctrs_0.3.6
   htmltools_0.5.1.1
[11] base64enc_0.1-3   rlang_0.4.10  pkgbuild_1.2.0pillar_1.4.7
  glue_1.4.2
[16] withr_2.4.1   DBI_1.1.1 sessioninfo_1.1.1 lifecycle_0.2.0
   cellranger_1.1.0
[21] evaluate_0.14 memoise_2.0.0 knitr_1.31callr_3.5.1
   fastmap_1.1.0
[26] ps_1.5.0  curl_4.3  Rcpp_1.0.6openssl_1.4.3
   cachem_1.0.1
[31] desc_1.2.0pkgload_1.1.0 fs_1.5.0  askpass_1.1
   digest_0.6.27
[36] processx_3.4.5grid_4.0.3rprojroot_2.0.2   cli_2.3.0
   tools_4.0.3
[41] magrittr_2.0.1tibble_3.0.6  crayon_1.4.0  pkgconfig_2.0.3
   ellipsis_0.3.1
[46] prettyunits_1.1.1 httr_1.4.2assertthat_0.2.1  R6_2.5.0
  compiler_4.0.3
19:05:52  >

Thanks,

Roger

[[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] mpfr function in Rmpfr crashes R

2021-03-07 Thread John Fox

Dear Roger,

This works perfectly fine for me on an apparently similar system, with 
the exceptions that I'm running R 4.0.4, have many fewer packages 
loaded, and am in a slightly different locale:


--- snip 

> Rmpfr::mpfr(pi, 120)
1 'mpfr' number of precision  120   bits
[1] 3.1415926535897931159979634685441851616

> sessionInfo()
R version 4.0.4 (2021-02-15)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

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


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

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

other attached packages:
[1] Rmpfr_0.8-2 gmp_0.6-2

loaded via a namespace (and not attached):
 [1] compiler_4.0.4htmltools_0.5.1.1 tools_4.0.4   yaml_2.2.1 
  rmarkdown_2.6
 [6] knitr_1.31xfun_0.21 digest_0.6.27 
packrat_0.5.0 rlang_0.4.10

[11] evaluate_0.14

--- snip 

You might try updating R or running Rmpfr in a cleaner session.

I hope this helps,
 John

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

On 2021-03-06 7:07 p.m., Roger Bos wrote:

All,

The following code crashes by R on my mac with a message "R session
aborted.  A fatal error occured".

```
library(Rmpfr)
Rmpfr::mpfr(pi, 120)
```

Does anyone have any suggestions?   My session info is below:

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK:
/Library/Frameworks/R.framework/Versions/4.0/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] datasets  utils stats graphics  grDevices methods   base

other attached packages:
  [1] alphavantager_0.1.2 googlesheets4_0.2.0 googledrive_1.0.1
clipr_0.7.1
  [5] jsonlite_1.7.2  stringi_1.5.3   dtplyr_1.0.1
  data.table_1.13.6
  [9] dplyr_1.0.4 plyr_1.8.6  testthat_3.0.1
  lubridate_1.7.9.2
[13] timeDate_3043.102   sendmailR_1.2-1 rmarkdown_2.6
devtools_2.3.2
[17] usethis_2.0.0   xts_0.12.1  zoo_1.8-8
MASS_7.3-53
[21] fortunes_1.5-4

loaded via a namespace (and not attached):
  [1] tinytex_0.29  tidyselect_1.1.0  xfun_0.20 remotes_2.2.0
   purrr_0.3.4
  [6] gargle_0.5.0  lattice_0.20-41   generics_0.1.0vctrs_0.3.6
   htmltools_0.5.1.1
[11] base64enc_0.1-3   rlang_0.4.10  pkgbuild_1.2.0pillar_1.4.7
  glue_1.4.2
[16] withr_2.4.1   DBI_1.1.1 sessioninfo_1.1.1 lifecycle_0.2.0
   cellranger_1.1.0
[21] evaluate_0.14 memoise_2.0.0 knitr_1.31callr_3.5.1
   fastmap_1.1.0
[26] ps_1.5.0  curl_4.3  Rcpp_1.0.6openssl_1.4.3
   cachem_1.0.1
[31] desc_1.2.0pkgload_1.1.0 fs_1.5.0  askpass_1.1
   digest_0.6.27
[36] processx_3.4.5grid_4.0.3rprojroot_2.0.2   cli_2.3.0
   tools_4.0.3
[41] magrittr_2.0.1tibble_3.0.6  crayon_1.4.0  pkgconfig_2.0.3
   ellipsis_0.3.1
[46] prettyunits_1.1.1 httr_1.4.2assertthat_0.2.1  R6_2.5.0
  compiler_4.0.3
19:05:52  >

Thanks,

Roger

[[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] Dalex function in mlr3

2021-02-02 Thread Bert Gunter
Please note per the posting guide linked below:

"For questions about functions in standard packages distributed with R (see
the FAQ Add-on packages in R
), ask
questions on R-help.
If the question relates to a *contributed package* , e.g., one downloaded
from CRAN, try contacting the package maintainer first. You can also use
find("functionname") and packageDescription("packagename") to find this
information. *Only* send such questions to R-help or R-devel if you get no
reply or need further assistance. This applies to both requests for help
and to bug reports."

As you do not appear to have done this -- if you have, please say so -- do
not be surprised if you do not receive (helpful) assistance here. You
might, but  For perspective, there are about 25,000 R packages "out
there", and this list cannot possibly support them all.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Feb 2, 2021 at 11:32 AM Neha gupta  wrote:

> Hi to everyone
>
> I just wanted to know if the dalex package of the mlr3 provides the same
> functions (i.e. variable importance) as provided by the scott-knott-esd
> test?
>
> 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.
>

[[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] pgmm function errors in R

2021-01-22 Thread Bert Gunter
1. Please read the posting guide. Statistical questions and questions on
non-standard (not shipped with standard R distro) packages are largely off
topic here.

2.  This CRAN task view might be of interest to you:
https://cran.r-project.org/web/views/Econometrics.html

3. But as a general comment, perhaps completely useless, singularity in
linear-type model fits are often due to overfitting. Have no idea what that
might mean in your context.


Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jan 22, 2021 at 1:08 PM Wadim iLchuk 
wrote:

> Hello everyone, My name is Vadym, I am a last-year student at Stockholm
> School of Economics in Riga, currently working on my Bachelor's Thesis (so
> still learning R). For my research, I need a GMM estimator (preferable
> Arellano and Bond, 1991). I have been searching for a solution to my issue
> for about 2 months already, yet still, no solutions have been found.
>
> The data set could be found here:
>
> https://drive.google.com/file/d/1uMmZTBDCaq1YoKO8KXU1L8EiqcKVe5V3/view?usp=sharing
> Dataset is firm-level data.
>
> The main model for my topic is:
> deltaTFPt+1   = lag(deltaTFPt) + deltadebtt + finfrictiont +
> deltadebt*finfriction + age + ta + deltasales.
>
> Where, deltaTFP - growth in productivity of the firm from t to t+1,
> deltadebt - growth in debt of the company, finfriction is my main variable
> of interest - namely financial friction for the company, age is the age of
> the company, ta is total assets, deltasales - growth in sales.
>
> I need to estimate coefficients by using GMM
>
> My code is the following for doing so by using pgmm function:
> ###
> PLVData_A <- pdata.frame(LVData_A, index = c("ID","Year")) z1 <-
> pgmm(domegaACF_A ~ lag(domegaACF_A, 1) + ddebt + ff1 + ff1:ddebt + Age + ta
> + dsales | lag(domegaACF_A, 2), data = PLVData_A, effect = "twoways", model
> = "onestep", transformation = "d")
> summary(z1, robust = TRUE)
> #
>
> Bearing in mind that I have been struggling with this for 2-3
> months already I have tried to change everything I could in the model, but
> constantly receive these types of errors:
>
> Error in solve.default(crossprod(WX, t(crossprod(WX, A1 :
>   Lapack routine dgesv: system is exactly singular: U[5,5] = 0
> In addition: Warning message:
> In pgmm(domegaACF_A ~ lag(domegaACF_A, 1) + lag(ddebt, 0:1) + ff1 +  :
>   the first-step matrix is singular, a general inverse is used
>
>
> Error in solve.default(crossprod(WX, t(crossprod(WX, A1 :
>   system is computationally singular: reciprocal condition number =
> 4.26304e-27
> In addition: Warning message:
> In pgmm(domegaACF_A ~ lag(domegaACF_A, 1) + lag(ddebt, 0:1) + ff1 +  :
>   the first-step matrix is singular, a general inverse is used
>
>
> Error in cbind(yX1[[i]], V1) :
>   number of rows of matrices must match (see arg 2)
>
>
> Error in cbind(W2[[i]], V2) :
>   number of rows of matrices must match (see arg 2)
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)
>
>
> I would really appreciate any of your support or ideas since I have no clue
> of how to solve the issue. Thank you in advance!
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] colMeans function

2020-11-04 Thread Rui Barradas

Hello,

No, flights[2] is *not* equal to flights$months. The former is a 
data.frame with only one column, therefore it has a dimension attribute. 
The latter is a column, a vector of the data.frame flights, it does not 
have the attribute dim set.


The difference is very important, see what class(), str() or dim() 
return when applied to both.


See also this StackOverflow post:

https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el


Hope this helps,

Rui Barradas

Às 13:26 de 04/11/20, Engin Yılmaz escreveu:

Dear
I use *flights* database library(nycflights13)

The following code is working as

colMeans(flights[2])

* 6.54851*

but other code is  not working as

colMeans(flights$month)

*Error in colMeans(flights$month) : *
*  'x' must be an array of at least two dimensions*

*flights[2]* is equal to the *month *column in database

*Sincerely*
Engin YILMAZ

[[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] colMeans function

2020-11-04 Thread Marc Schwartz via R-help
Hi,

You might want to become familiar with the ?str and ?dim functions, which can 
help you identify the structure of the objects that you are passing to 
colMeans().

In the first case, flights[2] is a data frame with a single column, so will 
have two dimensions with a row and column structure.

In the second case, flights$month is a single numeric vector within the data 
frame and does not have a row and column dimension structure.

If you used flights[[2]], that would be equivalent to flights$month, in 
referencing a single numeric vector.

So, if you used:

str(flights[2])
dim(flights[2])

str(flights$month)
dim(flights$month)

you would see the difference.

Regards,

Marc Schwartz


> On Nov 4, 2020, at 8:26 AM, Engin Yılmaz  wrote:
> 
> Dear
> I use *flights* database library(nycflights13)
> 
> The following code is working as
> 
> colMeans(flights[2])
> 
> * 6.54851*
> 
> but other code is  not working as
> 
> colMeans(flights$month)
> 
> *Error in colMeans(flights$month) : *
> *  'x' must be an array of at least two dimensions*
> 
> *flights[2]* is equal to the *month *column in database
> 
> *Sincerely*
> Engin YILMAZ

__
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] colMeans function

2020-11-04 Thread Duncan Murdoch

On 04/11/2020 8:26 a.m., Engin Yılmaz wrote:

Dear
I use *flights* database library(nycflights13)

The following code is working as

colMeans(flights[2])

* 6.54851*

but other code is  not working as

colMeans(flights$month)

*Error in colMeans(flights$month) : *
*  'x' must be an array of at least two dimensions*

*flights[2]* is equal to the *month *column in database


No, flights[2] is a dataframe with one column.  flights[[2]] would be 
the month column.


Duncan Murdoch

__
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] Function of "matrix"

2020-10-22 Thread Richard M. Heiberger
FAQ 7.31

> 10.1/.1
[1] 101
> print(10.1/.1, digits=17)
[1] 100.99
> floor(10.1/.1)
[1] 100
>  floor(10.1*10)
[1] 101
> matrix(0, 2.9, 3.9)
 [,1] [,2] [,3]
[1,]000
[2,]000
>

note that the dimension arguments are passed through floor() before
they are used.

On Thu, Oct 22, 2020 at 2:42 PM 奈良県奈良市  wrote:
>
> Dear R project team
>
> I used the function of "matrix" as follows:
> matrix(c(1:3030), 10.1/0.1)
> However, in the function, matrix, 10.1/0.1 was regarded as 100 not as 101.
> Therefore, a warning message appeared.
> On the other hand, matrix(c(1:3030), 101) or matrix(c(1:3030), 10.1*10) was
> OK. Of course, simply, 10.1/0.1 was successfully calculated. However,
> In the "matrix" environment, 10.1/0.1 was calculated as 100.
>
> Would you give me some answers?
>
> Sincerely
>
> Kazuki Sakura
>
> [[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] summarize_all Function

2020-10-02 Thread Jeff Reichman
Thanks Bill – got it

 

From: Bill Dunlap  
Sent: Thursday, October 1, 2020 1:56 PM
To: reichm...@sbcglobal.net
Cc: r-help@r-project.org
Subject: Re: [R] summarize_all Function

 

The warning gives some suggestions.  E.g., replace funs(sum,prod) with 
list(sum=sum,prod=prod).

 

% R CMD Rscript -e 'library(dplyr,warn.conflicts=FALSE); 
data.frame(X=1:3,Y=c(11,13,17)) %>% summarize_all(funs(sum,prod))'
  X_sum Y_sum X_prod Y_prod
1 641  6   2431
Warning message:
`funs()` is deprecated as of dplyr 0.8.0.
Please use a list of either functions or lambdas:

  # Simple named list:
  list(mean = mean, median = median)

  # Auto named with `tibble::lst()`:
  tibble::lst(mean, median)

  # Using lambdas
  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.

% R CMD Rscript -e 'library(dplyr,warn.conflicts=FALSE); 
data.frame(X=1:3,Y=c(11,13,17)) %>% summarize_all(list(sum=sum,prod=prod))'
  X_sum Y_sum X_prod Y_prod
1 641  6   2431

 

On Thu, Oct 1, 2020 at 10:29 AM Jeff Reichman mailto:reichm...@sbcglobal.net> > wrote:

r-help Forum



I'm using the dplyr:: summarize_all(funs(sum)) function and am receiving a
warning message that the `funs()` is deprecated as of dplyr 0.8.0. Ok what
should I be using to summarize  all columns by sum?



Jeff


[[alternative HTML version deleted]]

__
R-help@r-project.org <mailto: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] summarize_all Function

2020-10-02 Thread Jeff Reichman
Thanks Rui

-Original Message-
From: Rui Barradas  
Sent: Thursday, October 1, 2020 1:49 PM
To: reichm...@sbcglobal.net; r-help@r-project.org
Subject: Re: [R] summarize_all Function

Hello,

Any of the two will do, the first is now preferred.


library(dplyr)

mtcars %>%
   summarise(across(everything(), sum))

mtcars %>%
   summarise_all(sum)   # no need for `funs()`


Hope this helps,

Rui Barradas

Às 18:29 de 01/10/20, Jeff Reichman escreveu:
> r-help Forum
> 
>   
> 
> I'm using the dplyr:: summarize_all(funs(sum)) function and am 
> receiving a warning message that the `funs()` is deprecated as of 
> dplyr 0.8.0. Ok what should I be using to summarize  all columns by sum?
> 
>   
> 
> Jeff
> 
> 
>   [[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] summarize_all Function

2020-10-01 Thread Bill Dunlap
The warning gives some suggestions.  E.g., replace funs(sum,prod) with
list(sum=sum,prod=prod).

% R CMD Rscript -e 'library(dplyr,warn.conflicts=FALSE);
data.frame(X=1:3,Y=c(11,13,17)) %>% summarize_all(funs(sum,prod))'
  X_sum Y_sum X_prod Y_prod
1 641  6   2431
Warning message:
`funs()` is deprecated as of dplyr 0.8.0.
Please use a list of either functions or lambdas:

  # Simple named list:
  list(mean = mean, median = median)

  # Auto named with `tibble::lst()`:
  tibble::lst(mean, median)

  # Using lambdas
  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated.

% R CMD Rscript -e 'library(dplyr,warn.conflicts=FALSE);
data.frame(X=1:3,Y=c(11,13,17)) %>% summarize_all(list(sum=sum,prod=prod))'
  X_sum Y_sum X_prod Y_prod
1 641  6   2431

On Thu, Oct 1, 2020 at 10:29 AM Jeff Reichman 
wrote:

> r-help Forum
>
>
>
> I'm using the dplyr:: summarize_all(funs(sum)) function and am receiving a
> warning message that the `funs()` is deprecated as of dplyr 0.8.0. Ok what
> should I be using to summarize  all columns by sum?
>
>
>
> Jeff
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] summarize_all Function

2020-10-01 Thread Rui Barradas

Hello,

Any of the two will do, the first is now preferred.


library(dplyr)

mtcars %>%
  summarise(across(everything(), sum))

mtcars %>%
  summarise_all(sum)   # no need for `funs()`


Hope this helps,

Rui Barradas

Às 18:29 de 01/10/20, Jeff Reichman escreveu:

r-help Forum

  


I'm using the dplyr:: summarize_all(funs(sum)) function and am receiving a
warning message that the `funs()` is deprecated as of dplyr 0.8.0. Ok what
should I be using to summarize  all columns by sum?

  


Jeff


[[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] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rui Barradas

Hello,

These two gives the same results:


aggregate(values ~ sex + status, mydata, sum)
#  sex status values
#1   0  0    224
#2   1  0   5227
#3   0  1 11
#4   1  1    552


by(mydata$values, list(mydata$sex, mydata$status), sum)
#: 0
#: 0
#[1] 224
#
#: 1
#: 0
#[1] 5227
#
#: 0
#: 1
#[1] 11
#
#: 1
#: 1
#[1] 552


So Duncan is right, your expected output's 2nd sum is wrong, the right 
sum is


mydata rows 2 and 6: 4730 + 497 == 5227
^

Another option, returning a matrix,

 tapply(mydata$values, list(mydata$sex, mydata$status), sum)
#             0   1
#0     224  11
#1 5227 552


Hope this helps,

Rui Barradas


Às 23:15 de 23/07/2020, Sorkin, John escreveu:

Colleagues,
  
The by function in the R program below is not giving me the sums

I expect to see, viz.,
382+170=552
4730+170=4900
5+6=11
199+25=224
###
#full R program:
mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
sex=(rep(c(1,1,0,0),2)),
status=rep(c(1,0),2),
values=c(382,4730,5,199,170,497,6,25))
mydata
by(mydata,list(mydata$sex,mydata$status),sum)
by(mydata,list(mydata$sex,mydata$status),print)
###

More complete explanation of my question
  
I have created a simple dataframe having three factors:

  mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
  sex=(rep(c(1,1,0,0),2)),
  status=rep(c(1,0),2),
  values=c(382,4730,5,199,170,497,6,25))
  
  > mydata

   covid sex status values
1 0   1  1    382
2 0   1  0   4730
3 0   0  1  5
4 0   0  0    199
5 1   1  1    170
6 1   1  0    497
7 1   0  1  6
8 1   0  0 25
  
When I use the by function with a sum as an argument, I don’t

get the sums that I would expect to
receive based either on the listing of the dataframe above,
or from using by with print as an argument:
  

by(mydata,list(mydata$sex,mydata$status),sum)

: 0
: 0
[1] 225
---
: 1
: 0
[1] 5230
---
: 0
: 1
[1] 14
---
: 1
: 1
[1] 557
  
I expected to see the following sums:

382+170=552
4730+170=4900
5+6=11
199+25=224
Which as can be seen by the output above, I am not getting.
  
Using print as an argument to the by function, I get the values

grouped as I would expect, but for some reason I get a double
printing of the values!
  

by(mydata,list(mydata$sex,mydata$status),print)

   covid sex status values
4 0   0  0    199
8 1   0  0 25
   covid sex status values
2 0   1  0   4730
6 1   1  0    497
   covid sex status values
3 0   0  1      5
7 1   0  1  6
   covid sex status values
1 0   1  1    382
5 1   1  1    170
: 0
: 0
   covid sex status values
4 0   0  0    199
8 1   0  0 25
---
: 1
: 0
   covid sex status values
2 0   1  0   4730
6 1   1  0    497
---
: 0
: 1
   covid sex status values
3 0   0  1  5
7 1   0  1  6
---
: 1
: 1
   covid sex status values
1 0   1  1    382
5 1   1  1    170
  
What am I doing wrong, or what don’t I understand

About the by function?
  
Thank you

John
  
  


















John David Sorkin M.D., Ph.D.

Professor of Medicine

Chief, Biostatistics and Informatics

University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine

Baltimore VA Medical Center

10 North Greene Street

GRECC (BT/18/GR)

Baltimore, MD 21201-1524

(Phone) 410-605-7119

(Fax) 410-605-7913 (Please call phone number above prior to faxing)



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



--
Este e-mail foi verificado em termos de vírus pelo software antivírus Avast.
https://www.avast.com/antivirus

__
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] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rasmus Liland
On 2020-07-24 01:48 +0200, Rasmus Liland wrote:
>   aggregate(x=list("values"=mydata$values),
> by=list("sex"=mydata$sex,
> "status"=mydata$status),
> FUN=sum)
> 
> yields
> 
> sex status values
>   1   0  0224
>   2   1  0   5227
>   3   0  1 11
>   4   1  1552

After reading more in ?aggregate, I 
realized this does the same thing ...

aggregate(formula=formula("values~sex+status"),
  FUN=sum,
  data=mydata)


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


Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Rasmus Liland
On 2020-07-23 18:54 -0400, Duncan Murdoch wrote:
> On 23/07/2020 6:15 p.m., Sorkin, John wrote:
> > Colleagues,
> > The by function in the R program below is not giving me the sums
> > I expect to see, viz.,
> > 382+170=552
> > 4730+170=4900
> > 5+6=11
> > 199+25=224
> > ###
> > #full R program:
> > mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
> > sex=(rep(c(1,1,0,0),2)),
> > status=rep(c(1,0),2),
> > values=c(382,4730,5,199,170,497,6,25))
> > mydata
> > by(mydata,list(mydata$sex,mydata$status),sum)
> > by(mydata,list(mydata$sex,mydata$status),print)
> > ###
> 
> The problem is that you are summing the mydata values, not the mydata$values
> values.  That will include covid, sex and status in the sums.  I think
> you'll get what you should (though it doesn't match what you say you
> expected, which looks wrong to me) with this code:
> 
> by(mydata$values,list(mydata$sex,mydata$status),sum)
> 
> for 0,0, the sum is 224 = 199+25
> for 0,1, the sum is  11 = 5+6
> for 1,0, the sum is 5227 = 4730 + 497 (not 4730 + 170)
> for 1,1, the sum is 552 = 382 + 170

Dear John,

Aggregate also does this, but sex and 
status are columns in a data.frame and 
not attributes of the double.

aggregate(x=list("values"=mydata$values),
  by=list("sex"=mydata$sex,
  "status"=mydata$status),
  FUN=sum)

yields

  sex status values
1   0  0224
2   1  0   5227
3   0  1 11
4   1  1552

Best,
Rasmus


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


Re: [R] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Duncan Murdoch

On 23/07/2020 6:15 p.m., Sorkin, John wrote:

Colleagues,
  
The by function in the R program below is not giving me the sums

I expect to see, viz.,
382+170=552
4730+170=4900
5+6=11
199+25=224
###
#full R program:
mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
sex=(rep(c(1,1,0,0),2)),
status=rep(c(1,0),2),
values=c(382,4730,5,199,170,497,6,25))
mydata
by(mydata,list(mydata$sex,mydata$status),sum)
by(mydata,list(mydata$sex,mydata$status),print)
###


The problem is that you are summing the mydata values, not the 
mydata$values values.  That will include covid, sex and status in the 
sums.  I think you'll get what you should (though it doesn't match what 
you say you expected, which looks wrong to me) with this code:


by(mydata$values,list(mydata$sex,mydata$status),sum)

for 0,0, the sum is 224 = 199+25
for 0,1, the sum is  11 = 5+6
for 1,0, the sum is 5227 = 4730 + 497 (not 4730 + 170)
for 1,1, the sum is 552 = 382 + 170

Duncan Murdoch




More complete explanation of my question
  
I have created a simple dataframe having three factors:

  mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
  sex=(rep(c(1,1,0,0),2)),
  status=rep(c(1,0),2),
  values=c(382,4730,5,199,170,497,6,25))
  
  > mydata

   covid sex status values
1 0   1  1    382
2 0   1  0   4730
3 0   0  1  5
4 0   0  0    199
5 1   1  1    170
6 1   1  0    497
7 1   0  1  6
8 1   0  0 25
  
When I use the by function with a sum as an argument, I don’t

get the sums that I would expect to
receive based either on the listing of the dataframe above,
or from using by with print as an argument:
  

by(mydata,list(mydata$sex,mydata$status),sum)

: 0
: 0
[1] 225
---
: 1
: 0
[1] 5230
---
: 0
: 1
[1] 14
---
: 1
: 1
[1] 557
  
I expected to see the following sums:

382+170=552
4730+170=4900
5+6=11
199+25=224
Which as can be seen by the output above, I am not getting.
  
Using print as an argument to the by function, I get the values

grouped as I would expect, but for some reason I get a double
printing of the values!
  

by(mydata,list(mydata$sex,mydata$status),print)

   covid sex status values
4 0   0  0    199
8 1   0  0 25
   covid sex status values
2 0   1  0   4730
6 1   1  0    497
   covid sex status values
3 0   0  1      5
7 1   0  1  6
   covid sex status values
1 0   1  1    382
5 1   1  1    170
: 0
: 0
   covid sex status values
4 0   0  0    199
8 1   0  0 25
---
: 1
: 0
   covid sex status values
2 0   1  0   4730
6 1   1  0    497
---
: 0
: 1
   covid sex status values
3 0   0  1  5
7 1   0  1  6
---
: 1
: 1
   covid sex status values
1 0   1  1    382
5 1   1  1    170
  
What am I doing wrong, or what don’t I understand

About the by function?
  
Thank you

John
  
  


















John David Sorkin M.D., Ph.D.

Professor of Medicine

Chief, Biostatistics and Informatics

University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine

Baltimore VA Medical Center

10 North Greene Street

GRECC (BT/18/GR)

Baltimore, MD 21201-1524

(Phone) 410-605-7119

(Fax) 410-605-7913 (Please call phone number above prior to faxing)



__
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] by function with sum does not give what is expected from by function with print

2020-07-23 Thread Bert Gunter
by() chooses **data frame** subsets -- sum() is acting on these frames,
adding up everything in them.
Try this instead:

> by(mydata,list(mydata$sex,mydata$status),function(x)sum(x$values))
: 0
: 0
[1] 224
---
: 1
: 0
[1] 5227
---
: 0
: 1
[1] 11
---
: 1
: 1
[1] 552

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Jul 23, 2020 at 3:25 PM Sorkin, John 
wrote:

> Colleagues,
>
> The by function in the R program below is not giving me the sums
> I expect to see, viz.,
> 382+170=552
> 4730+170=4900
> 5+6=11
> 199+25=224
> ###
> #full R program:
> mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
> sex=(rep(c(1,1,0,0),2)),
> status=rep(c(1,0),2),
> values=c(382,4730,5,199,170,497,6,25))
> mydata
> by(mydata,list(mydata$sex,mydata$status),sum)
> by(mydata,list(mydata$sex,mydata$status),print)
> ###
>
> More complete explanation of my question
>
> I have created a simple dataframe having three factors:
>  mydata <- data.frame(covid=c(0,0,0,0,1,1,1,1),
>  sex=(rep(c(1,1,0,0),2)),
>  status=rep(c(1,0),2),
>  values=c(382,4730,5,199,170,497,6,25))
>
>  > mydata
>   covid sex status values
> 1 0   1  1382
> 2 0   1  0   4730
> 3 0   0  1  5
> 4 0   0  0199
> 5 1   1  1170
> 6 1   1  0497
> 7 1   0  1  6
> 8 1   0  0 25
>
> When I use the by function with a sum as an argument, I don’t
> get the sums that I would expect to
> receive based either on the listing of the dataframe above,
> or from using by with print as an argument:
>
> > by(mydata,list(mydata$sex,mydata$status),sum)
> : 0
> : 0
> [1] 225
> ---
>
> : 1
> : 0
> [1] 5230
> ---
>
> : 0
> : 1
> [1] 14
> ---
>
> : 1
> : 1
> [1] 557
>
> I expected to see the following sums:
> 382+170=552
> 4730+170=4900
> 5+6=11
> 199+25=224
> Which as can be seen by the output above, I am not getting.
>
> Using print as an argument to the by function, I get the values
> grouped as I would expect, but for some reason I get a double
> printing of the values!
>
> > by(mydata,list(mydata$sex,mydata$status),print)
>   covid sex status values
> 4 0   0  0199
> 8 1   0  0 25
>   covid sex status values
> 2 0   1  0   4730
> 6 1   1  0497
>   covid sex status values
> 3 0   0  1  5
> 7 1   0  1  6
>   covid sex status values
> 1 0   1  1382
> 5 1   1  1170
> : 0
> : 0
>   covid sex status values
> 4 0   0  0199
> 8 1   0  0 25
> ---
>
> : 1
> : 0
>   covid sex status values
> 2 0   1  0   4730
> 6 1   1  0497
> ---
>
> : 0
> : 1
>   covid sex status values
> 3 0   0  1  5
> 7 1   0  1  6
> ---
>
> : 1
> : 1
>   covid sex status values
> 1 0   1  1382
> 5 1   1  1170
>
> What am I doing wrong, or what don’t I understand
> About the by function?
>
> Thank you
> John
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> John David Sorkin M.D., Ph.D.
>
> Professor of Medicine
>
> Chief, Biostatistics and Informatics
>
> University of Maryland School of Medicine Division of Gerontology and
> Geriatric Medicine
>
> Baltimore VA Medical Center
>
> 10 North Greene Street
>
> GRECC (BT/18/GR)
>
> Baltimore, MD 21201-1524
>
> (Phone) 410-605-7119
>
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
>
>
> __
> 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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Rasmus Liland
On 2020-05-13 13:13 -0700, Jeff Newmiller wrote:
> In general, any time you deal with floating 
> point numbers having different magnitudes, 
> you risk pushing some low precision bits 
> out of the result. Simply changing the 
> sequence of calculations such as a literal 
> polynomial evaluation versus Horner's 
> method can obtain different results. Take a 
> course in Numerical Analysis to learn 
> more.
> 
> [1] https://en.m.wikipedia.org/wiki/Horner%27s_method
> [2] https://en.m.wikipedia.org/wiki/Numerical_analysis

Right, it seems fairly interesting.  I'll 
look into it at some point.

/JR


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


Re: [R] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Jeff Newmiller
In general, any time you deal with floating point numbers having different 
magnitudes, you risk pushing some low precision bits out of the result. Simply 
changing the sequence of calculations such as a literal polynomial evaluation 
versus Horner's method can obtain different results. Take a course in Numerical 
Analysis to learn more.

[1] https://en.m.wikipedia.org/wiki/Horner%27s_method
[2] https://en.m.wikipedia.org/wiki/Numerical_analysis

On May 13, 2020 11:57:09 AM PDT, Rasmus Liland  wrote:
>On 2020-05-13 11:44 -0700, Jeff Newmiller wrote:
>> Depending on reproducibility in the least 
>> significant bits of floating point 
>> calculations is a bad practice. Just 
>> because you decide based on this one 
>> example that one implementation of BLAS is 
>> better than another does not mean that will 
>> be true for all specific examples. IMO you 
>> are drawing conclusions on data that is 
>> effectively random and should change your 
>> definition of "sufficient to the task".
>
>Dear Jeff,
>
>Right, so I really would have wanted OpenBLAS 
>to be as reproducible as regular BLAS in this 
>one random example, but my hands remains tied 
>on this since I do not know anything about 
>BLAS ... 
>
>More interestingly, could you dream up any 
>idea as to what might cause this difference?
>
>Best,
>Rasmus

-- 
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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Rasmus Liland
On 2020-05-13 11:44 -0700, Jeff Newmiller wrote:
> Depending on reproducibility in the least 
> significant bits of floating point 
> calculations is a bad practice. Just 
> because you decide based on this one 
> example that one implementation of BLAS is 
> better than another does not mean that will 
> be true for all specific examples. IMO you 
> are drawing conclusions on data that is 
> effectively random and should change your 
> definition of "sufficient to the task".

Dear Jeff,

Right, so I really would have wanted OpenBLAS 
to be as reproducible as regular BLAS in this 
one random example, but my hands remains tied 
on this since I do not know anything about 
BLAS ... 

More interestingly, could you dream up any 
idea as to what might cause this difference?

Best,
Rasmus

__
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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Jeff Newmiller
Depending on reproducibility in the least significant bits of floating point 
calculations is a bad practice. Just because you decide based on this one 
example that one implementation of BLAS is better than another does not mean 
that will be true for all specific examples. IMO you are drawing conclusions on 
data that is effectively random and should change your definition of 
"sufficient to the task".

On May 13, 2020 11:29:33 AM PDT, Rasmus Liland  wrote:
>On 2020-05-13 13:04 -0400, J C Nash wrote:
>> On 2020-05-13 11:28 a.m., Rasmus Liland wrote:
>> > 
>> > I get another solution on my Linux i7-7500U 
>> > 
>> > > D %*% solve(D)
>> >  [,1] [,2]
>> > [1,] 1.00e+000
>> > [2,] 8.881784e-161
>> > > sessionInfo()
>> > BLAS:   /usr/lib/libopenblasp-r0.3.9.so
>> > LAPACK: /usr/lib/liblapack.so.3.9.0
>> 
>> Note that my sessionInfo() gave
>> 
>> R version 4.0.0 (2020-04-24)
>> Platform: x86_64-pc-linux-gnu (64-bit)
>> Running under: Linux Mint 19.3
>> 
>> Matrix products: default
>> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
>> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
>> 
>> So you have an older R but newer libraries, and the libblas is
>> a different one.
>> 
>> Given the output is very similar, and within the rounding
>> margins of the double arithmetic, this looks like the
>> libraries are very slightly different. I suppose I should
>> be more inquisitive and try to seek out the changelog or other
>> description of the differences, but ...
>> 
>> JN
>
>Dear JN,
>
>I was thinking BLAS could be changed to 
>OpenBLAS, apparently not:
>
>If I switch from OpenBLAS back to regular 
>BLAS, the output is as expected ... I thought 
>OpenBLAS should be a real alternative to BLAS 
>in many cases, but not in this example?
>
>> D %*% solve(D)
> [,1] [,2]
>[1,]1 1.110223e-16
>[2,]0 1.00e+00
>> sessionInfo()
>R version 3.6.3 (2020-02-29)
>Platform: x86_64-pc-linux-gnu (64-bit)
>Running under: Arch Linux
>
>Matrix products: default
>BLAS:   /usr/lib/libblas.so.3.9.0
>LAPACK: /usr/lib/liblapack.so.3.9.0
>
>Best,
>Rasmus
>
>__
>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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Rasmus Liland
On 2020-05-13 13:04 -0400, J C Nash wrote:
> On 2020-05-13 11:28 a.m., Rasmus Liland wrote:
> > 
> > I get another solution on my Linux i7-7500U 
> > 
> > > D %*% solve(D)
> >  [,1] [,2]
> > [1,] 1.00e+000
> > [2,] 8.881784e-161
> > > sessionInfo()
> > BLAS:   /usr/lib/libopenblasp-r0.3.9.so
> > LAPACK: /usr/lib/liblapack.so.3.9.0
> 
> Note that my sessionInfo() gave
> 
> R version 4.0.0 (2020-04-24)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Linux Mint 19.3
> 
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
> 
> So you have an older R but newer libraries, and the libblas is
> a different one.
> 
> Given the output is very similar, and within the rounding
> margins of the double arithmetic, this looks like the
> libraries are very slightly different. I suppose I should
> be more inquisitive and try to seek out the changelog or other
> description of the differences, but ...
> 
> JN

Dear JN,

I was thinking BLAS could be changed to 
OpenBLAS, apparently not:

If I switch from OpenBLAS back to regular 
BLAS, the output is as expected ... I thought 
OpenBLAS should be a real alternative to BLAS 
in many cases, but not in this example?

> D %*% solve(D)
 [,1] [,2]
[1,]1 1.110223e-16
[2,]0 1.00e+00
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libblas.so.3.9.0
LAPACK: /usr/lib/liblapack.so.3.9.0

Best,
Rasmus

__
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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread J C Nash
Note that my sessionInfo() gave

R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19.3

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

So you have an older R but newer libraries, and the libblas is
a different one.

Given the output is very similar, and within the rounding
margins of the double arithmetic, this looks like the
libraries are very slightly different. I suppose I should
be more inquisitive and try to seek out the changelog or other
description of the differences, but ...

JN


On 2020-05-13 11:28 a.m., Rasmus Liland wrote:
> On 2020-05-09 11:40 -0400, J C Nash wrote:
>>
>>> solve(D)
>>  [,1] [,2]
>> [1,] -2.0  1.0
>> [2,]  1.5 -0.5
>>> D %*% solve(D)
>>  [,1] [,2]
>> [1,]1 1.110223e-16
>> [2,]0 1.00e+00
>>>
> 
> Dear list,
> 
> I get another solution on my Linux i7-7500U 
> laptop, but the same solution on my FreeBSD 
> E3-1240Lv5 machine with a really old R 
> version (without BLAS) ...
> 
> How is this possible?
> 
>> D %*% solve(D)
>  [,1] [,2]
> [1,] 1.00e+000
> [2,] 8.881784e-161
>> sessionInfo()
> R version 3.6.3 (2020-02-29)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Arch Linux
> 
> Matrix products: default
> BLAS:   /usr/lib/libopenblasp-r0.3.9.so
> LAPACK: /usr/lib/liblapack.so.3.9.0
> 
> locale:
>  [1] LC_CTYPE=en_GB.UTF-8   LC_NUMERIC=C  
>  [3] LC_TIME=en_DK.UTF-8LC_COLLATE=en_GB.UTF-8
>  [5] LC_MONETARY=nb_NO.UTF-8LC_MESSAGES=en_GB.UTF-8   
>  [7] LC_PAPER=nb_NO.UTF-8   LC_NAME=C 
>  [9] LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=nb_NO.UTF-8 LC_IDENTIFICATION=C   
> 
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base 
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.6.3
>>
> 
> From the machine running FreeBSD:
> 
>> D %*% solve(D)
>  [,1] [,2]
> [1,]1 1.110223e-16
> [2,]0 1.00e+00
>> sessionInfo()
> R version 3.5.2 (2018-12-20)
> Platform: amd64-portbld-freebsd12.0 (64-bit)
> Running under: FreeBSD hmm 12.0-RELEASE FreeBSD 12.0-RELEASE r341666 GENERIC  
> amd64
> 
> Matrix products: default
> LAPACK: /usr/local/lib/R/lib/libRlapack.so
> 
> locale:
> [1] C/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
> 
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.5.2
>>
> 
> Best,
> Rasmus
>

__
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] solve() function freezes CLI in GNU R 3.6.3

2020-05-13 Thread Rasmus Liland
On 2020-05-09 11:40 -0400, J C Nash wrote:
> 
> > solve(D)
>  [,1] [,2]
> [1,] -2.0  1.0
> [2,]  1.5 -0.5
> > D %*% solve(D)
>  [,1] [,2]
> [1,]1 1.110223e-16
> [2,]0 1.00e+00
> >

Dear list,

I get another solution on my Linux i7-7500U 
laptop, but the same solution on my FreeBSD 
E3-1240Lv5 machine with a really old R 
version (without BLAS) ...

How is this possible?

> D %*% solve(D)
 [,1] [,2]
[1,] 1.00e+000
[2,] 8.881784e-161
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS:   /usr/lib/libopenblasp-r0.3.9.so
LAPACK: /usr/lib/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8   LC_NUMERIC=C  
 [3] LC_TIME=en_DK.UTF-8LC_COLLATE=en_GB.UTF-8
 [5] LC_MONETARY=nb_NO.UTF-8LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=nb_NO.UTF-8   LC_NAME=C 
 [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=nb_NO.UTF-8 LC_IDENTIFICATION=C   

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

loaded via a namespace (and not attached):
[1] compiler_3.6.3
> 

>From the machine running FreeBSD:

> D %*% solve(D)
 [,1] [,2]
[1,]1 1.110223e-16
[2,]0 1.00e+00
> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: amd64-portbld-freebsd12.0 (64-bit)
Running under: FreeBSD hmm 12.0-RELEASE FreeBSD 12.0-RELEASE r341666 GENERIC  
amd64

Matrix products: default
LAPACK: /usr/local/lib/R/lib/libRlapack.so

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

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

loaded via a namespace (and not attached):
[1] compiler_3.5.2
>

Best,
Rasmus

__
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] solve() function freezes CLI in GNU R 3.6.3 - SOLVED

2020-05-10 Thread Ko Byeongmin
Dear list,

Dirk and Professor J.C. Nash gave me an invaluable help! Prof. Nash
mentioned:

> Possibly this is a quirk of the particular distro or machine, BLAS or 
> LAPACK[.]
This was indeed the case, and Dirk's suggestion to

> [install] libopenblas-openmp-dev" and
> [remove] both "libopenblas-pthread-dev libopenblas0-pthread"
immediately solved the problem!

Before applying the fix, I took note of my /sessionInfo() /output:

> > sessionInfo()
> R version 3.6.3 (2020-02-29)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Ubuntu 20.04 LTS
>
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
>
> locale:
>  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
>  [3] LC_TIME=de_DE.UTF-8    LC_COLLATE=en_US.UTF-8
>  [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8
>  [7] LC_PAPER=de_DE.UTF-8   LC_NAME=C
>  [9] LC_ADDRESS=C   LC_TELEPHONE=C
> [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods base
>
> loaded via a namespace (and not attached):
> [1] compiler_3.6.3
*Is there anything that I can do, so that others**
**do not experience the same issue?* Writing
a bug report comes into my mind...

In this last paragraph I append some information on
my hardware, as Dirk suggested in his previous mail,
as well as a summary of the software I'm using.

> Operating System: Kubuntu 20.04
> KDE Plasma Version: 5.18.4
> KDE Frameworks Version: 5.68.0
> Qt Version: 5.12.8
> Kernel Version: 5.4.0-28-generic
> OS Type: 64-bit
> Processors: 8 × Intel® Core™ i5-8300H CPU @ 2.30GHz
> Memory: 7,4 GiB of RAM
Again, thanks for all the help! I really appreciate it. :)

Byeongmin

On 09.05.20 18:35, Dirk Eddelbuettel wrote:
> On 9 May 2020 at 10:30, Dirk Eddelbuettel wrote:
> |
> | We can see that you use Linux.
> |
> | Are you by chance
> |
> |  - on a Debian or Ubuntu system, and
> |  - have the libopenblas package installed ?
> |
> | If so then it is a known bug with the libopenblas0-pthread package.
> |
> | Installing libopen0-openmp (and also removing libopenblas0-pthread) should
> | fix it.
>
> That was imprecise. It should read "installing libopenblas-openmp-dev" and
> removing both "libopenblas-pthread-dev libopenblas0-pthread".
>
> I cannot reproduce the bug on the hardware I have access to (i5, i7, xeon)
> though I was able to a few weeks ago. Maybe something changed already...
>
> There was also a brief thread on the debian-science list (within Debian)
> starting with https://lists.debian.org/debian-science/2020/04/msg00081.html
> and leading to https://lists.debian.org/debian-science/2020/05/msg3.html
> (and this cross from April to May)
>
> We would need some more information on hardware to chase this.
>
> Dirk
>   
> | This is likely CPU dependent. But we need more info. Can you maybe come to 
> the
> | r-sig-debian list (subscription needed to reduce spam) and we continue 
> there?
> |
> | Dirk
> |
> |
> | --
> | http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
> |
> | __
> | 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] solve() function freezes CLI in GNU R 3.6.3

2020-05-09 Thread Dirk Eddelbuettel


On 9 May 2020 at 10:30, Dirk Eddelbuettel wrote:
| 
| We can see that you use Linux.
| 
| Are you by chance
| 
|  - on a Debian or Ubuntu system, and 
|  - have the libopenblas package installed ?
| 
| If so then it is a known bug with the libopenblas0-pthread package.
| 
| Installing libopen0-openmp (and also removing libopenblas0-pthread) should
| fix it.

That was imprecise. It should read "installing libopenblas-openmp-dev" and
removing both "libopenblas-pthread-dev libopenblas0-pthread".

I cannot reproduce the bug on the hardware I have access to (i5, i7, xeon)
though I was able to a few weeks ago. Maybe something changed already...

There was also a brief thread on the debian-science list (within Debian)
starting with https://lists.debian.org/debian-science/2020/04/msg00081.html
and leading to https://lists.debian.org/debian-science/2020/05/msg3.html
(and this cross from April to May)

We would need some more information on hardware to chase this.

Dirk
 
| This is likely CPU dependent. But we need more info. Can you maybe come to the
| r-sig-debian list (subscription needed to reduce spam) and we continue there?
| 
| Dirk
| 
| 
| -- 
| http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org
| 
| __
| 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.

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
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] solve() function freezes CLI in GNU R 3.6.3

2020-05-09 Thread J C Nash
I get the output at the bottom, which seems OK.

Can you include sessionInfo() output?
Possibly this is a quirk of the particular distro or machine, BLAS or LAPACK,
or something in your workspace. However, if we have full information, someone 
may be
able to run the same setup in a VM (if I have the .iso and can find
a way to set up R 3.6.3 easily, I'll be willing). It may be that you should
reinstall libblas or liblapack as one thing to try.

JN

Here's my output:

> D = matrix(
+ data = c(1, 2, 3, 4),
+ nrow = 2,
+ ncol = 2,
+ byrow = TRUE)
> solve(D)
 [,1] [,2]
[1,] -2.0  1.0
[2,]  1.5 -0.5
> D %*% solve(D)
 [,1] [,2]
[1,]1 1.110223e-16
[2,]0 1.00e+00
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19.3

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_CA.UTF-8   LC_NUMERIC=C
 [3] LC_TIME=en_CA.UTF-8LC_COLLATE=en_CA.UTF-8
 [5] LC_MONETARY=en_CA.UTF-8LC_MESSAGES=en_CA.UTF-8
 [7] LC_PAPER=en_CA.UTF-8   LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C

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

loaded via a namespace (and not attached):
[1] compiler_4.0.0
>

On 2020-05-09 5:45 a.m., ‍고병민[ 학부재학 / 경제학과 ] wrote:
> Dear list,
> 
> there is a bug with the *solve() *function that I cannot find a solution
> for a month. So I ask for your help.
> 
> *Whenever I try to invert a matrix using the said function, the console
> hangs*.
> Below I explain more about this situation.
> 
> Consider the code
> 
> D = matrix(
> data = c(1, 2, 3, 4),
> nrow = 2,
> ncol = 2,
> byrow = TRUE)
> solve(D)
> 
> 1. *If I launch the code in R called from a terminal, say, Konsole, the
> session will freeze.*
>   * I know that the exact timing of the system freeze is when I execute the
> solve( ) function.
>   * According to htop, one of my CPU core is used by 100% when this happens.
> 
> 2. *If I launch the same code within RStudio, the code works as expected.*
> However, if I call it using the terminal inside RStudio, the session hangs.
>   * If the solve() function is used within RMarkdown document, the session
> will freeze and the document will not be generated.
> 
> 3. Launching R with --vanilla does not resolve the issue.
> 
> 4. Rebooting the PC, using my external graphic card, reinstalling the
> r-base-core package in apt, and trying with different terminal emulators do
> not help.
> 
> 5. From the documentation of the solve( ) function in R, it can be seen
> that solve(A, B) actually takes two arguments: A is a matrix, and B a
> vector or a matrix. If B is a vector, it solves the linear system Ax = B.
> If B is a matrix, it solves AX = B and returns X. If nothing is given in
> the second argument, it automatically assumes identity matrix of
> appropriate size as B. **The first function of solving linear system
> works.** If I specify matrices as the second argument, however, the same
> problem happens.
> 
> 6. Using QR decomposition with qr.solve(A) still works well.
> 
> Here are my questions:
> 
> *1. Has anyone had the same problem as me?*
> *2. I also seek recommendations on how to fix this issue.*
> 
> For your information, I am using R version 3.6.3 installed from the default
> apt repository. Here is the output of which R and R -- version:
> 
> kobyeongmin@odie:~$ which R
> /usr/bin/R
> 
> kobyeongmin@odie:~$ R --version
> R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
> Copyright (C) 2020 The R Foundation for Statistical Computing
> Platform: x86_64-pc-linux-gnu (64-bit)
> 
> R is free software and comes with ABSOLUTELY NO WARRANTY.
> You are welcome to redistribute it under the terms of the
> GNU General Public License versions 2 or 3.
> For more information about these matters see
> https://www.gnu.org/licenses/.
> 
> Thank you for reading this, and stay safe!
> 
> Best regards
> 
> Ko Byeongmin
> 
>   [[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] solve() function freezes CLI in GNU R 3.6.3

2020-05-09 Thread Dirk Eddelbuettel


We can see that you use Linux.

Are you by chance

 - on a Debian or Ubuntu system, and 
 - have the libopenblas package installed ?

If so then it is a known bug with the libopenblas0-pthread package.

Installing libopen0-openmp (and also removing libopenblas0-pthread) should
fix it.

This is likely CPU dependent. But we need more info. Can you maybe come to the
r-sig-debian list (subscription needed to reduce spam) and we continue there?

Dirk


-- 
http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org

__
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] custom function gives unexpected result - for me

2020-04-17 Thread Monica Palaseanu-Lovejoy
Hi,

I cannot believe I did that. Usually I remember to add parenthesis but this
time obviously I didn’t. Thank you all so much for answering so quickly.

Thanks,
Monica

On Fri, Apr 17, 2020 at 7:06 PM Peter Langfelder 
wrote:

> You need 1:(m-1) in your function. The operator : has precedence over -:
>
> > 1:3-1
> [1] 0 1 2
> > 1:(3-1)
> [1] 1 2
>
> Happened to me a few times as well before I remembered.
>
> HTH,
>
> Peter
>
> On Fri, Apr 17, 2020 at 3:50 PM Monica Palaseanu-Lovejoy
>  wrote:
> >
> > Hi,
> >
> > I wrote a relatively simple function. If i run the code inside the
> function
> > line by line i am getting the result i was expecting, but if i run the
> > function, i get a different result.
> >
> > The function:
> >
> > grr1 <- function(rn) {
> > r.up <- c()
> > for (i in 1:rn-1) {
> > if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> > r.up <- c(r.up, ru)
> > }
> > return(r.up)
> > }
> >
> > So, if rn is 3 for example i would expect to get 1 1 2
> >
> > grr1(3)
> > [1] 1 0 1 1 2
> >
> > If i run it line by line inside the function:
> > r.up <- c()
> > > r.up
> > NULL
> >
> > i=1
> > if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> > > ru
> > [1] 1
> >
> > r.up <- c(r.up, ru)
> > r.up
> > [1] 1
> >
> > i=2
> > if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> > ru
> > [1] 1 2
> > r.up <- c(r.up, ru)
> > > r.up
> > [1] 1 1 2
> >
> > So - i am getting the result i am expecting. From where the 1 0 before
> what
> > i expect as a result comes from? I am sure i am doing some very basic
> > error, but it seems i cannot figure it out.
> >
> > I run R x64  3.2.6. I know it is not the latest version, but it should
> not
> > give me unexpected results because of that i would think.
> >
> > sessionInfo()
> > R version 3.6.2 (2019-12-12)
> > Platform: x86_64-w64-mingw32/x64 (64-bit)
> > Running under: Windows 10 x64 (build 17763)
> >
> > Matrix products: default
> >
> > locale:
> > [1] LC_COLLATE=English_United States.1252
> > [2] LC_CTYPE=English_United States.1252
> > [3] LC_MONETARY=English_United States.1252
> > [4] LC_NUMERIC=C
> > [5] LC_TIME=English_United States.1252
> >
> > attached base packages:
> > [1] stats graphics  grDevices utils datasets  methods   base
> >
> > loaded via a namespace (and not attached):
> > [1] compiler_3.6.2
> >
> > Thanks,
> > Monica
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] custom function gives unexpected result - for me

2020-04-17 Thread Peter Langfelder
You need 1:(m-1) in your function. The operator : has precedence over -:

> 1:3-1
[1] 0 1 2
> 1:(3-1)
[1] 1 2

Happened to me a few times as well before I remembered.

HTH,

Peter

On Fri, Apr 17, 2020 at 3:50 PM Monica Palaseanu-Lovejoy
 wrote:
>
> Hi,
>
> I wrote a relatively simple function. If i run the code inside the function
> line by line i am getting the result i was expecting, but if i run the
> function, i get a different result.
>
> The function:
>
> grr1 <- function(rn) {
> r.up <- c()
> for (i in 1:rn-1) {
> if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> r.up <- c(r.up, ru)
> }
> return(r.up)
> }
>
> So, if rn is 3 for example i would expect to get 1 1 2
>
> grr1(3)
> [1] 1 0 1 1 2
>
> If i run it line by line inside the function:
> r.up <- c()
> > r.up
> NULL
>
> i=1
> if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> > ru
> [1] 1
>
> r.up <- c(r.up, ru)
> r.up
> [1] 1
>
> i=2
> if (i%%2==0) ru <- seq(1,i) else ru <- seq(i,1)
> ru
> [1] 1 2
> r.up <- c(r.up, ru)
> > r.up
> [1] 1 1 2
>
> So - i am getting the result i am expecting. From where the 1 0 before what
> i expect as a result comes from? I am sure i am doing some very basic
> error, but it seems i cannot figure it out.
>
> I run R x64  3.2.6. I know it is not the latest version, but it should not
> give me unexpected results because of that i would think.
>
> sessionInfo()
> R version 3.6.2 (2019-12-12)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 17763)
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] compiler_3.6.2
>
> Thanks,
> Monica
>
> [[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] custom function insite dlys::mutate

2020-03-30 Thread Rasmus Liland
On 2020-03-30 13:21 +, SIMON Nicolas wrote:
| Apparently the condition " if ( var1 > tt ) " 
| cannot be evaluated correcty.
| 
| Could you please give me an advice to write it 
| properly?

Hi!  To understand what you wanted to do, I tried 
to recreate your example in classic R indexing of 
a data.frame, then converting it to a tibble:

var1 <- seq(0, 20, 1)
var2 <- c(0, 2, 4, 8)
var3 <- 44.44
var4 <- 0.5

dat <- cbind(var1, var2)
dat <- as.data.frame(dat)
idx <- dat$var1 > dat$var2
dat[idx, "obs1"] <-
  var3 * exp(-var4 * dat[idx, "var1"])
dat[!idx, "obs1"] <- 0
dat <- tibble::as_tibble(dat)
dat

Apparently there is a dplyr::if_else function you 
can use with dplyr::mutate like you wanted to end 
up in the same place:

var1 <- seq(0, 20, 1)
var2 <- c(0, 2, 4, 8)
var3 <- 44.44
var4 <- 0.5

dat <- cbind(var1, var2)
dat <- as.data.frame(dat)
dat <- tibble::as_tibble(dat)
dat <-
  dplyr::mutate(.data=dat,
obs=dplyr::if_else(
  var1>var2,
  var3*exp(-var4 * var1),
  0)
  )
dat

Might this be close to what you were looking to 
do?

/Rasmus

__
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] custom function insite dlys::mutate

2020-03-30 Thread Rasmus Liland
On 2020-03-30 13:21 +, SIMON Nicolas wrote:
> Could you please give me an advice to write it 
> properly?

Hi!  To understand what you wanted to do, I tried 
to recreate your example in classic R indexing of 
a data.frame, then converting it to a tibble:

var1 <- seq(0, 20, 1)
var2 <- c(0, 2, 4, 8)
var3 <- 44.44
var4 <- 0.5

dat <- cbind(var1, var2)
dat <- as.data.frame(dat)
idx <- dat$var1 > dat$var2
dat[idx, "obs1"] <-
  var3 * exp(-var4 * dat[idx, "var1"])
dat[!idx, "obs1"] <- 0
dat <- tibble::as_tibble(dat)
dat

Apparently there is a dplyr::if_else function you 
can use with dplyr::mutate like you wanted to end 
up in the same place:

var1 <- seq(0, 20, 1)
var2 <- c(0, 2, 4, 8)
var3 <- 44.44
var4 <- 0.5

dat <- cbind(var1, var2)
dat <- as.data.frame(dat)
dat <- tibble::as_tibble(dat)
dat <-
  dplyr::mutate(.data=dat,
obs=dplyr::if_else(
  var1>var2,
  var3*exp(-var4 * var1),
  0)
  )
dat

Might this be close to what you were looking to 
do?

/Rasmus

__
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] custom function insite dlys::mutate

2020-03-30 Thread Jeff Newmiller
You are comparing a 21-element vector with a 1-element vector, which gives you 
21 answers. Which one of those answers are you actually interested in?

I am not sure what you are trying to accomplish here, so cannot offer further 
advice.

On March 30, 2020 6:21:45 AM PDT, SIMON Nicolas  wrote:
>Hi,
>
>I tried to use mutate from dplyr with a personal function to create a
>new variable of a data.frame.
>
>All examples I found are such as myfunction(x){...}.
>
>In my script, the function calls:
>- one variable coming from the data.frame
>- variables coming from lists or constant outside the data.frame
>
>My function is something like:
>
>var1 <- seq(0,20,1)
>dat <- data.frame(var1)
>var2 <- c(0,2,4,8)
>var3 <- 44.44
>var4 <- 0.5
>
>Myfunction <- function(var1, var2, var3,var4){
>   obs <- 0
>   obs1 <- 0
>   for (i in 1:length(var2)) {
> tt <- var2[i]
> if ( var1 > tt ){
> obs1 <- var3 * exp(-var4*var1)
> }
> else obs1 <- 0.0
> obs <- obs + obs1
>   }
>  return(obs)
>}
>dat2 <- mutate(dat, obs = Myfunction(var1=var1,var2=var2,var3=var3))
>
>Apparently the condition " if ( var1 > tt ) " cannot be evaluated
>correcty.
>
>Could you please give me an advice to write it properly?
>
>
>
>Best regards
>Nicolas
>
>
>   [[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.

-- 
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] rgamma function produces NaN values

2020-03-06 Thread Sanna Soomro
Hi

I didn't notice that I haven't cc the R-help so, unknowingly, I replied
back to you only.
That's understandable and I think I may consult for the wrong type of help.
However, I'll remain in the list in case if I get stuck in any simple R
problem again.

Best regards
Sanna Soomro

On Fri, 6 Mar 2020, 15:45 Bert Gunter,  wrote:

> Unless there is a good reason not to, always cc the list in your
> responses. I am not your free private consultant.
>
> Beause you use your own function, your result is not reproducible, so that
> it is unlikely you can get useful help.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Fri, Mar 6, 2020 at 6:11 AM Sanna Soomro  wrote:
>
>> Hi
>>
>>
>>
>> I apologize for the late response – I’ve been busy.
>>
>> I have attached my R code in the email but I don’t know if it gone
>> through.
>>
>> The gibb_lasso function is what I created myself.
>>
>> The packages I am using are mvtnorm, GIGrvg, tmvtnorm, RGeode and
>> truncnorm.
>>
>>
>>
>> Best regards
>>
>> Sanna
>>
>>
>>
>> *From: *Bert Gunter 
>> *Date: *Thursday, 5 March 2020 at 15:40
>> *To: *Sanna Soomro 
>> *Subject: *Re: [R] rgamma function produces NaN values
>>
>>
>>
>> What package are you using? -- this is requested by the posting guide.
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>>
>>
>>
>> On Thu, Mar 5, 2020 at 12:50 AM Sanna Soomro 
>> wrote:
>>
>> Hi,
>>
>> In my code, I want to sample from the posterior distribution to get
>> estimates for each parameter via the Bayesian approach. My model has
>> spatial coefficient and lasso penalty.
>>
>> When I run this line
>>
>> gibbs_lasso(y = Y, x= X, W=W.rook, tau = 0.5, M=2)
>>
>> It works, however, when I changed M from 2 to 5, I get the following
>> error:
>>
>> Error in rgig(1, 0.5, (SIGMA[m, ]/theta2) * ((y[ik] - crossprod(x[ik,  :
>>   invalid parameters for GIG distribution: lambda=0.5, chi=nan, psi=nan
>> In addition: Warning message:
>> In rgamma(1, shape = 1.5 * n + a0, rate = SIGMAgamma + b0) : NAs produced
>>
>> It makes sense that rgig cannot accept NaN values of sigma parameter in
>> its
>> computation. But why does rgamma produces NaN value for sigma? The gamma
>> distribution require both shape and scale parameters to be positive and my
>> R computations for both should always be positive then sigma can be
>> sampled
>> easily. So, what went wrong in my code?
>>
>> Best regards
>> Sanna
>> __
>> 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] rgamma function produces NaN values

2020-03-06 Thread Bert Gunter
Unless there is a good reason not to, always cc the list in your responses.
I am not your free private consultant.

Beause you use your own function, your result is not reproducible, so that
it is unlikely you can get useful help.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Mar 6, 2020 at 6:11 AM Sanna Soomro  wrote:

> Hi
>
>
>
> I apologize for the late response – I’ve been busy.
>
> I have attached my R code in the email but I don’t know if it gone
> through.
>
> The gibb_lasso function is what I created myself.
>
> The packages I am using are mvtnorm, GIGrvg, tmvtnorm, RGeode and
> truncnorm.
>
>
>
> Best regards
>
> Sanna
>
>
>
> *From: *Bert Gunter 
> *Date: *Thursday, 5 March 2020 at 15:40
> *To: *Sanna Soomro 
> *Subject: *Re: [R] rgamma function produces NaN values
>
>
>
> What package are you using? -- this is requested by the posting guide.
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>
>
>
> On Thu, Mar 5, 2020 at 12:50 AM Sanna Soomro  wrote:
>
> Hi,
>
> In my code, I want to sample from the posterior distribution to get
> estimates for each parameter via the Bayesian approach. My model has
> spatial coefficient and lasso penalty.
>
> When I run this line
>
> gibbs_lasso(y = Y, x= X, W=W.rook, tau = 0.5, M=2)
>
> It works, however, when I changed M from 2 to 5, I get the following error:
>
> Error in rgig(1, 0.5, (SIGMA[m, ]/theta2) * ((y[ik] - crossprod(x[ik,  :
>   invalid parameters for GIG distribution: lambda=0.5, chi=nan, psi=nan
> In addition: Warning message:
> In rgamma(1, shape = 1.5 * n + a0, rate = SIGMAgamma + b0) : NAs produced
>
> It makes sense that rgig cannot accept NaN values of sigma parameter in its
> computation. But why does rgamma produces NaN value for sigma? The gamma
> distribution require both shape and scale parameters to be positive and my
> R computations for both should always be positive then sigma can be sampled
> easily. So, what went wrong in my code?
>
> Best regards
> Sanna
> __
> 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] rgamma function produces NaN values

2020-03-05 Thread PIKAL Petr
Hi

what is gibbs_lasso? I did not find any function of this name.

Usually reproducible example greatly enhance your chances to get reasonable
answer.

If any n, a0, SIGMAgamma, b0 is NA rgamma gives you NA.

Cheers
Petr
> -Original Message-
> From: R-help  On Behalf Of Sanna Soomro
> Sent: Wednesday, March 4, 2020 4:22 PM
> To: r-help@r-project.org
> Subject: [R] rgamma function produces NaN values
> 
> Hi,
> 
> In my code, I want to sample from the posterior distribution to get
estimates for
> each parameter via the Bayesian approach. My model has spatial coefficient
and
> lasso penalty.
> 
> When I run this line
> 
> gibbs_lasso(y = Y, x= X, W=W.rook, tau = 0.5, M=2)
> 
> It works, however, when I changed M from 2 to 5, I get the following
error:
> 
> Error in rgig(1, 0.5, (SIGMA[m, ]/theta2) * ((y[ik] - crossprod(x[ik,  :
>   invalid parameters for GIG distribution: lambda=0.5, chi=nan, psi=nan In
> addition: Warning message:
> In rgamma(1, shape = 1.5 * n + a0, rate = SIGMAgamma + b0) : NAs produced
> 
> It makes sense that rgig cannot accept NaN values of sigma parameter in
its
> computation. But why does rgamma produces NaN value for sigma? The gamma
> distribution require both shape and scale parameters to be positive and my
R
> computations for both should always be positive then sigma can be sampled
> easily. So, what went wrong in my code?
> 
> Best regards
> Sanna
> __
> 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] about .function

2020-01-30 Thread Bogdan Tanasa
appreciate it ! thank you Duncan !

On Thu, Jan 30, 2020 at 11:18 AM Duncan Murdoch 
wrote:

> On 30/01/2020 1:38 p.m., Bogdan Tanasa wrote:
> > Dear all,
> >
> > if I may ask please a very simple question :
> >
> > what does "." mean in front of  function name : an example below . thank
> > you very much !
> >
> > .set_pbmc_color_11<-function() {
> >myColors <- c( "dodgerblue2",
> >   "green4",
> >   "#6A3D9A", # purple
> >   "grey",
> >   "tan4",
> >   "yellow",
> >   "#FF7F00", # orange
> >   "black",
> >   "#FB9A99", # pink
> >   "orchid",
> >   "red")
> >
>
> It means that the default ls() won't list the function, you'd need
> ls(all.names = TRUE).  By convention such functions are usually meant
> for internal use, but there are lots of exceptions to that convention.
> The same convention is used in Unix-alike file systems.
>
> Duncan Murdoch
>
>
>

[[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] about .function

2020-01-30 Thread Duncan Murdoch

On 30/01/2020 1:38 p.m., Bogdan Tanasa wrote:

Dear all,

if I may ask please a very simple question :

what does "." mean in front of  function name : an example below . thank
you very much !

.set_pbmc_color_11<-function() {
   myColors <- c( "dodgerblue2",
  "green4",
  "#6A3D9A", # purple
  "grey",
  "tan4",
  "yellow",
  "#FF7F00", # orange
  "black",
  "#FB9A99", # pink
  "orchid",
  "red")



It means that the default ls() won't list the function, you'd need 
ls(all.names = TRUE).  By convention such functions are usually meant 
for internal use, but there are lots of exceptions to that convention.

The same convention is used in Unix-alike file systems.

Duncan Murdoch

__
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] fa function in psych package and missing values

2019-08-01 Thread Gomez Cano, Mayam


Does fa function in psych package consider only complete cases when 
missing=FALSE?
Why do I get slightly different results when I restrict my data to complete 
cases compared to the whole data with the default missing=FALSE?





--
Sent from: http://r.789695.n4.nabble.com/R-help-f789696.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] Sample function and prob argument

2019-06-05 Thread Duncan Murdoch

On 05/06/2019 4:34 a.m., le Gleut, Ronan wrote:

Dear R-help mailing list,

  


First of all, many many thanks for your great work on the R project!

  


I have a very small issue regarding the sample function. Depending if we
specify values for the prob argument, we don't get the same result for a
random sampling with replacement and with equal probabilities. See the
attached R code for a minimal example with the R version 3.6.0.

  


With a previous R version (3.5.x), the result was just a permutation
between the possible realizations. They are now totally different with the
latest R version.

  


I understand that if we specify or not the prob argument, two different
internal functions are used: .Internal(sample()) or .Internal(sample2()).
Indeed, the algorithm used to draw a sample may not be the same if by
default we assume equal probabilities (without the prob argument) or if
the user defines himself the probabilities (even if they are equal).

  


I found this post on stackoverflow which explains the reasons of this
difference (answer by Matthew Lundberg):

https://stackoverflow.com/questions/23316729/r-sample-probabilities-defaul
t-is-equal-weight-why-does-specifying-equal-weigh

  


I was wondering whether the solution proposed by PatrickT could solve this
issue? He proposed to have something like if(all.equal(prob, prob,
tolerance = .Machine$double.eps) prob = NULL inside the sample.int routine
in order to replicate prob=NULL with prob=rep(1, length(x)).



R has never promised that these will be the same, so I doubt if R will 
change the sample() function.  However, it's very easy for you to adopt 
something like PatrickT's solution for yourself.  Just use this function:


PatrickTsample <- function(x, size, replace = FALSE, prob = NULL) {
  if (!is.null(prob) && max(prob) == min(prob))
prob <- NULL
  sample(x = x, size = size, replace = replace, prob = prob)
}

You might want a looser tolerance on the vector of probabilities 
depending on your context.


Duncan Murdoch

__
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] The function sink() --delete everything before printing by "sink()"

2019-05-06 Thread Eric Berger
?sink
 for the HELP page on that sink() function.
Check the description of the (optional) argument append to get an answer to
your question.

On Mon, May 6, 2019 at 5:24 AM John  wrote:

> I use the functions "sink" and "print" to print the results to a txt file.
> May I delete everything in the txt files before it start to print to the
> txt file? Thanks!
> ###
> sink(file_output.txt"))
> for(i in c("a", "b"))
> {
>   for(j in c("c", "d"))
>   {
> {
>   print(c(i,j))
>   print(xyz1[[i,j]])
>   print("t-stat")
>   print(abc1[[i,j]])
> }
>   }
> }
> sink()
> #
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] stukel function unavailable for some odd reason

2019-05-03 Thread Duncan Murdoch

On 03/05/2019 4:49 p.m., Paul Bernal wrote:

Dear friends,

I have been fitting a logistic regression and wanted to try a couple of
goodness of fit tests on the model.

Doing some research, I came across Chris Dardi's stukel and logiGOF
functions from package LogisticDx v0.1.


The current version of LogisticDx is 0.2 from 2015, and it doesn't 
export those functions.  Source for version 0.1 from 2014 is available, 
and it does contain those functions, but I wouldn't touch them with a 
ten foot pole unless I first heard from the author why he left them out 
of the next release.


Duncan Murdoch




I tried installing package LogisticDx in different R versions without any
success.

I first tried installing LogisticDx package in R version 3.5.3 (for windows
64-bit OS) and this is what happened:


install.packages("LogisticDx")

Installing package into 'C:/Users/PaulBernal/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
--- Please select a CRAN mirror for use in this session ---
also installing the dependencies 'polspline', 'rms', 'speedglm', 'aod'

trying URL '
https://cran.cnr.berkeley.edu/bin/windows/contrib/3.5/polspline_1.1.14.zip'
Content type 'application/zip' length 778099 bytes (759 KB)
downloaded 759 KB

trying URL '
https://cran.cnr.berkeley.edu/bin/windows/contrib/3.5/rms_5.1-3.1.zip'
Content type 'application/zip' length 2044689 bytes (1.9 MB)
downloaded 1.9 MB

trying URL '
https://cran.cnr.berkeley.edu/bin/windows/contrib/3.5/speedglm_0.3-2.zip'
Content type 'application/zip' length 187929 bytes (183 KB)
downloaded 183 KB

trying URL '
https://cran.cnr.berkeley.edu/bin/windows/contrib/3.5/aod_1.3.1.zip'
Content type 'application/zip' length 323652 bytes (316 KB)
downloaded 316 KB

trying URL '
https://cran.cnr.berkeley.edu/bin/windows/contrib/3.5/LogisticDx_0.2.zip'
Content type 'application/zip' length 931345 bytes (909 KB)
downloaded 909 KB

package 'polspline' successfully unpacked and MD5 sums checked
package 'rms' successfully unpacked and MD5 sums checked
package 'speedglm' successfully unpacked and MD5 sums checked
package 'aod' successfully unpacked and MD5 sums checked
package 'LogisticDx' successfully unpacked and MD5 sums checked



stukel(RegGLM_Mod1)

Error in stukel(RegGLM_Mod1) : could not find function "stukel"


library(LogisticDx)

Error: package or namespace load failed for 'LogisticDx':
  object 'plotp' not found whilst loading namespace 'rms'

install.packages("plotp")

Installing package into 'C:/Users/PaulBernal/Documents/R/win-library/3.5'
(as 'lib' is unspecified)
Warning: package 'plotp' is not available (for R version 3.5.3)

After this, I installed R version 3.6.0 and tried again:

install.packages("LogisticDx")

Installing package into ‘C:/Users/PaulBernal/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL '
https://mirrors.dotsrc.org/cran/bin/windows/contrib/3.6/LogisticDx_0.2.zip'
Content type 'application/zip' length 932914 bytes (911 KB)
downloaded 911 KB

package ‘LogisticDx’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in

C:\Users\PaulBernal\AppData\Local\Temp\RtmpInhoDn\downloaded_packages

library(LogisticDx)

Registered S3 methods overwritten by 'ggplot2':
   method from
   [.quosures rlang
   c.quosures rlang
   print.quosures rlang


stukel(GLM.1)

Error in stukel(GLM.1) : could not find function "stukel"

Anyone can give me some light on what could be happening here? Do I have to
try with another R version? Is it something else?

Thanks in advance,

Paul

[[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] picewise function in nls....

2019-04-19 Thread Ivan Krylov
On Fri, 19 Apr 2019 10:12:06 +
akshay kulkarni  wrote:

> But what do you mean by "since fx does not depend on any of the
> parameters you optimize in the nls() call."? Can you give an example?

By "parameters you optimize in the nls() call" I mean `a`. `a` does
not seem to be used in the calculation of `fx`. If it were, it would
have to look like:

fx <- function(x1, x2, a) { ... }
nls(y ~ a*(sin(x2) + fx(x1, x2, a)), start = list(a = ...))

so that nls() would be able to check different values of `a`.

-- 
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] picewise function in nls....

2019-04-18 Thread Ivan Krylov
On Thu, 18 Apr 2019 10:36:10 +
akshay kulkarni  wrote:

> fx <-   (x1 <= -2)*(x1^2) + (x1 > -2 && x1 < 2)*(x1^3) + (x1 > =
> 2)*(x1^4)
> 
> Can I include fx in an nls call  to create something like this:
> 
> NLS1 <- nls(y ~ a*(sin(x2) + fx), start = list(a = 2))   ?

For now, you can, since fx does not depend on any of the parameters you
optimize in the nls() call. (Actually, the model as presented should be
solveable by lm(y ~ I(sin(x2) + fx) + 0), since it is linear in its
only parameter.)

If you make fx a function and use ifelse() to provide different
outcomes depending on a condition in a vectorized fashion, you would
make it easier to add new parameters later, should the need arise:

fx <- function(x1, x2)
ifelse(x1 <= -2, EXPR_IF_TRUE..., EXPR_IF_FALSE...)

NLS1 <- nls(y ~ a*(sin(x2) + fx(x1, x2)), ...)

-- 
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] lag function row name with

2019-03-25 Thread Eric Berger
Hi John,
dplyr::lag expects a vector. The following should work

dplyr::lag(temp[,1],2)

HTH,
Eric


On Mon, Mar 25, 2019 at 9:45 AM John  wrote:

> Hi,
>
>I have a dataset whose row names corresponds to months. When I apply lag
> function (dplyr package) on this dataset, I get NAs with warning messages.
> Is there any lag function that carries out the lag but keep the row names?
>I will have two datasets. The dates of the datasets are not exactly the
> same, and I want to find out the correlation for the overlapping period.
>
>Thanks,
>
>
> > temp
>  oil95
> 1981M01 103.27
> 1981M02 107.92
> 1981M03 110.26
> 1981M04 110.26
> 1981M05 110.11
> 1981M06 109.93
> 1981M07 109.93
> 1981M08 109.93
> 1981M09 109.93
> 1981M10 109.93
>
> > dplyr::lag(temp, 2)
> oil95
> 1981M01NA
> 1981M02  
> 1981M03  
> 1981M04  
> 1981M05  
> 1981M06  
> 1981M07  
> 1981M08  
> 1981M09  
> 1981M10  
> Warning message:
> In format.data.frame(x, digits = digits, na.encode = FALSE) :
>   corrupt data frame: columns will be truncated or padded with NAs
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] Solving Function using Conjugate Gradient

2019-03-20 Thread Bert Gunter
You should (almost) always also reply to the list, especially in this case,
where others my also fail to help because they perceive your query as
homework.

However, I should warn you that such comprehensive queries are often
dismissed as "do my work for me" requests. See the Posting Guide linked
below for a comprehension description of what and how to post here.
Following the recommended protocols will certainly increase your chance of
receiving a helpful reply.

Cheers,
Bert




On Wed, Mar 20, 2019 at 10:42 AM smart hendsome 
wrote:

> Hi Gunter,
>
> Actually, thats not my homework, I just curios regarding how the paper of
> entropy optimized that function using either raphson method or conjugate
> gradient, if you dont want to help thats ok.
>
>
> Zuhri
>
> Sent from Yahoo Mail on Android
> 
>
> On Thu, 21 Mar 2019 at 1:34 a.m., Bert Gunter
>  wrote:
> This list has a no homework policy.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, Mar 20, 2019 at 8:54 AM smart hendsome via R-help <
> r-help@r-project.org> wrote:
>
> Hi R-user,
> I have a problem regarding to estimate lambda[r] by minimizing the convex
> function using conjugate gradient in R-programming.  The function as below:
>
>
> Hopefully, anybody in this forum can help me. Thanks so much.
>
> __
> 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] Solving Function using Conjugate Gradient

2019-03-20 Thread Bert Gunter
This list has a no homework policy.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Wed, Mar 20, 2019 at 8:54 AM smart hendsome via R-help <
r-help@r-project.org> wrote:

> Hi R-user,
> I have a problem regarding to estimate lambda[r] by minimizing the convex
> function using conjugate gradient in R-programming.  The function as below:
>
>
> Hopefully, anybody in this forum can help me. Thanks so much.
> __
> 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] profvis function parse_rprof not being loaded

2019-03-01 Thread Winston Chang
Thanks for the heads up - I've updated the development version of profvis
to export parse_rprof.

-Winston

On Thu, Feb 28, 2019 at 5:14 AM Duncan Murdoch 
wrote:

> On 27/02/2019 9:42 p.m., nevil amos wrote:
> > I have loaded the profvis library  but the function parse_ rprof()  is
> > absent.
> > below is the session info show the absence of the function ( which is
> > listed  in the package help for the current version.)
> >
>
> Looks as though they forgot to export it.  You can get to it using
> profvis:::parse_rprof, but the maintainer (who is cc'd) might want to
> fix this.
>
> Duncan Murdoch
>
> >
> > Documentation for package ‘profvis’ version 0.3.5
> >
> > DESCRIPTION file.
> > Help Pages
> >
> > parse_rprof Parse Rprof output file for use with profvis
> > pause Pause an R process
> > print.profvis Print a profvis object
> > profvis Profile an R expression and visualize profiling data
> > profvisOutput Widget output function for use in Shiny
> > renderProfvis Widget render function for use in Shiny
> >
> >
> > Rsession info:
> >
> >
> >> library(profvis)
> >> parse_rprof()
> > Error in parse_rprof() : could not find function "parse_rprof"
> >> ls("package:profvis")
> > [1] "pause" "profvis"   "profvisOutput" "renderProfvis"
> >> sessionInfo()
> > R version 3.5.2 (2018-12-20)
> > Platform: x86_64-w64-mingw32/x64 (64-bit)
> > Running under: Windows 7 x64 (build 7601) Service Pack 1
> >
> > Matrix products: default
> >
> > locale:
> > [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
> > LC_MONETARY=English_Australia.1252
> > [4] LC_NUMERIC=C   LC_TIME=English_Australia.1252
> >
> > attached base packages:
> > [1] stats graphics  grDevices utils datasets  methods   base
> >
> > other attached packages:
> > [1] profvis_0.3.5
> >
> > loaded via a namespace (and not attached):
> >   [1] htmlwidgets_1.3 compiler_3.5.2  magrittr_1.5htmltools_0.3.6
> > tools_3.5.2 yaml_2.2.0  Rcpp_1.0.0  stringi_1.2.4
> >   [9] stringr_1.3.1   digest_0.6.18
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>

[[alternative HTML version deleted]]

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


Re: [R] profvis function parse_rprof not being loaded

2019-02-28 Thread Duncan Murdoch

On 27/02/2019 9:42 p.m., nevil amos wrote:

I have loaded the profvis library  but the function parse_ rprof()  is
absent.
below is the session info show the absence of the function ( which is
listed  in the package help for the current version.)



Looks as though they forgot to export it.  You can get to it using 
profvis:::parse_rprof, but the maintainer (who is cc'd) might want to 
fix this.


Duncan Murdoch



Documentation for package ‘profvis’ version 0.3.5

DESCRIPTION file.
Help Pages

parse_rprof Parse Rprof output file for use with profvis
pause Pause an R process
print.profvis Print a profvis object
profvis Profile an R expression and visualize profiling data
profvisOutput Widget output function for use in Shiny
renderProfvis Widget render function for use in Shiny


Rsession info:



library(profvis)
parse_rprof()

Error in parse_rprof() : could not find function "parse_rprof"

ls("package:profvis")

[1] "pause" "profvis"   "profvisOutput" "renderProfvis"

sessionInfo()

R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C   LC_TIME=English_Australia.1252

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

other attached packages:
[1] profvis_0.3.5

loaded via a namespace (and not attached):
  [1] htmlwidgets_1.3 compiler_3.5.2  magrittr_1.5htmltools_0.3.6
tools_3.5.2 yaml_2.2.0  Rcpp_1.0.0  stringi_1.2.4
  [9] stringr_1.3.1   digest_0.6.18

[[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] geom_ribbon function in ggplot2 package

2018-12-19 Thread John
Thanks, Eik!

Eik Vettorazzi  於 2018年12月19日 週三 下午5:12寫道:

> Hi,
> just add +scale_fill_discrete(name=NULL)
>
> Cheers
>
> Am 19.12.2018 um 07:05 schrieb John:
> > Hi,
> >
> > When using the geom_ribbon function in gglot2 package, I got the text
> > "fill" above the legend "A" and "B". How can I get rid of the text "fill"
> > above the legend?
> >
> > Thanks!
> >
> > The code is as follows:
> >
> >
> > df<-data.frame(x=c(1,2), y=c(1,2), z=c(3,5))
> >> ggplot(df,
> >
> aes(1:2))+geom_ribbon(aes(ymin=0,ymax=df[,"y"],fill="A"),alpha=0.5)+geom_ribbon(aes(ymin=0,ymax=df[,"z"],fill="B"),alpha=0.5)
> >
> >   [[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.
> >
>
> --
> Eik Vettorazzi
>
> Department of Medical Biometry and Epidemiology
> University Medical Center Hamburg-Eppendorf
>
> Martinistrasse 52
> building W 34
> 20246 Hamburg
>
> Phone: +49 (0) 40 7410 - 58243
> Fax:   +49 (0) 40 7410 - 57790
> Web: www.uke.de/imbe
> --
>
> _
>
> Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen
> Rechts; Gerichtsstand: Hamburg | www.uke.de
> Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr.
> Uwe Koch-Gromus, Joachim Prölß, Marya Verdel
> _
>
> SAVE PAPER - THINK BEFORE PRINTING
>

[[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] geom_ribbon function in ggplot2 package

2018-12-19 Thread Eik Vettorazzi

Hi,
just add +scale_fill_discrete(name=NULL)

Cheers

Am 19.12.2018 um 07:05 schrieb John:

Hi,

When using the geom_ribbon function in gglot2 package, I got the text
"fill" above the legend "A" and "B". How can I get rid of the text "fill"
above the legend?

Thanks!

The code is as follows:


df<-data.frame(x=c(1,2), y=c(1,2), z=c(3,5))

ggplot(df,

aes(1:2))+geom_ribbon(aes(ymin=0,ymax=df[,"y"],fill="A"),alpha=0.5)+geom_ribbon(aes(ymin=0,ymax=df[,"z"],fill="B"),alpha=0.5)

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



--
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistrasse 52
building W 34
20246 Hamburg

Phone: +49 (0) 40 7410 - 58243
Fax:   +49 (0) 40 7410 - 57790
Web: www.uke.de/imbe
--

_

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; 
Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe 
Koch-Gromus, Joachim Prölß, Marya Verdel
_

SAVE PAPER - THINK BEFORE PRINTING
__
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] TrendRaster function

2018-11-26 Thread Jeff Newmiller
a) You had to go out of your way to even install this package... it is lonly 
available on R-forge. This kind of question seems likely to require support 
from the package author. (Use the"maintainer" function to identify the author.)

b) You may find a similar functionality in a CRAN package. Try describing what 
you want to accomplish on the R-sig-geo mailing list.

On November 23, 2018 5:02:04 PM PST, Jackson Rodrigues 
 wrote:
>Dear all,
>
>I am trying to run the codes of "greenbrown" package for detection in
>raster time serie.
>
>However the error below reported occurs.
>
>#
>library(greenbrown)
>data(ndvimap)
>ndvimap
>plot(ndvimap,8)
>
># calculate trend: annual aggregation method
>AATmap <- TrendRaster(ndvimap$X1982.01.01, start=c(1982, 1), freq=12,
>method="AAT", breaks=1)
>
>Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
>  cannot use this function
>
>
>Could anyone help me to solve it?
>
>Thank you,
>
>Jackson
>
>   [[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.

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


  1   2   3   4   5   6   7   8   9   10   >