[Rd] Bug in "$<-.data.frame" yields corrupt data frame (PR#13724)

2009-05-26 Thread smckinney
Full_Name: Steven McKinney
Version: 2.9.0
OS: Mac OS X 10.5.6 
Submission from: (NULL) (142.103.207.10)



A corrupt data frame can be constructed as follows:
foo <- matrix(1:12, nrow = 3)
bar <- data.frame(foo)
bar$NewCol <- foo[foo[, 1] == 4, 4]
bar
lapply(bar, length)




> foo <- matrix(1:12, nrow = 3)
> bar <- data.frame(foo)
> bar$NewCol <- foo[foo[, 1] == 4, 4] bar
   X1 X2 X3 X4 NewCol
 1  1  4  7 10   
 2  2  5  8 11   
 3  3  6  9 12   
 Warning message:
 In format.data.frame(x, digits = digits, na.encode = FALSE) :
   corrupt data frame: columns will be truncated or padded with NAs
> lapply(bar, length)
 $X1
 [1] 3

 $X2
 [1] 3

 $X3
 [1] 3

 $X4
 [1] 3

 $NewCol
 [1] 0


The data.frame method is

> getAnywhere("$<-.data.frame" )
A single object matching '$<-.data.frame' was found It was found in the
following places
  package:base
  registered S3 method for $<- from namespace base
  namespace:base
with value

function (x, i, value)
{
cl <- oldClass(x)
class(x) <- NULL
nrows <- .row_names_info(x, 2L)
if (!is.null(value)) {
N <- NROW(value)
if (N > nrows) 
stop(gettextf("replacement has %d rows, data has %d", 
N, nrows), domain = NA)
if (N < nrows && N > 0L) 
if (nrows%%N == 0L && length(dim(value)) <= 1L) 
value <- rep(value, length.out = nrows)
else stop(gettextf("replacement has %d rows, data has %d", 
N, nrows), domain = NA)
if (is.atomic(value)) 
names(value) <- NULL
}
x[[i]] <- value
class(x) <- cl
return(x)
}
>


I placed a browser() command before return(x) and did some poking
around.  The issue is that the example above creates an object with
N < nrows but N == 0L, so either an else clause to check for this
condition is needed, or, it appears to me, the N > 0L part of the
conditional clause needs to be moved to the next if clause.

I modified the rows
  if (N < nrows && N > 0L) 
if (nrows%%N == 0L && length(dim(value)) <= 1L)
to read
   if (N < nrows) 
if (N > 0L && nrows%%N == 0L && length(dim(value)) <= 1L)

as in

"$<-.data.frame" <-
function (x, i, value) 
{
cl <- oldClass(x)
class(x) <- NULL
nrows <- .row_names_info(x, 2L)
if (!is.null(value)) {
N <- NROW(value)
if (N > nrows) 
stop(gettextf("replacement has %d rows, data has %d", 
N, nrows), domain = NA)
if (N < nrows) 
if (N > 0L && nrows%%N == 0L && length(dim(value)) <= 1L) 
value <- rep(value, length.out = nrows)
else stop(gettextf("replacement has %d rows, data has %d", 
N, nrows), domain = NA)
if (is.atomic(value)) 
names(value) <- NULL
}
x[[i]] <- value
class(x) <- cl
return(x)
} 

Now it detects the problem above:

> foo <- matrix(1:12, nrow = 3)
> bar <- data.frame(foo)
> bar$NewCol <- foo[foo[, 1] == 4, 4]
Error in `$<-.data.frame`(`*tmp*`, "NewCol", value = integer(0)) : 
  replacement has 0 rows, data has 3

It doesn't appear to stumble on weird data frames (these from the
?data.frame help page)


> L3 <- LETTERS[1:3]
> (d <- data.frame(cbind(x=1, y=1:10), fac=sample(L3, 10,
replace=TRUE)))
> (d0  <- d[, FALSE]) # NULL data frame with 10 rows
 
> (d.0 <- d[FALSE, ]) # <0 rows> data frame  (3 cols)

> (d00 <- d0[FALSE,])  # NULL data frame with 0 rows
 
> d0$NewCol <- foo[foo[, 1] == 4, 4]
Error in `$<-.data.frame`(`*tmp*`, "NewCol", value = integer(0)) : 
  replacement has 0 rows, data has 10

### Catches this problem above alright.

> d.0$NewCol <- foo[foo[, 1] == 4, 4]
> d.0
[1] x  y  facNewCol
<0 rows> (or 0-length row.names)

### Lets the above one through alright.

> d00$NewCol <- foo[foo[, 1] == 4, 4]
> 
> d00
[1] NewCol
<0 rows> (or 0-length row.names)
### Lets the above one through alright.


Would the above modification work to fix this problem?






> sessionInfo()
 R version 2.9.0 (2009-04-17)
 powerpc-apple-darwin8.11.1

 locale:
 en_CA.UTF-8/en_CA.UTF-8/C/C/en_CA.UTF-8/en_CA.UTF-8

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

 other attached packages:
 [1] nlme_3.1-90

 loaded via a namespace (and not attached):
 [1] grid_2.9.0  lattice_0.17-22 tools_2.9.0


 Also occurs on Windows box with R 2.8.1



 Steven McKinney

 Statistician
 Molecular Oncology and Breast Cancer Program British Columbia Cancer
 Research Centre

 email: smckinney +at+ bccrc +dot+ ca

 tel: 604-675-8000 x7561

 BCCRC
 Molecular Oncology
 675 West 10th Ave, Floor 4
 Vancouver B.C.
 V5Z 1L3
 Canada

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Wishlist: Navigate to "Index" page of help when no topic specified (PR#13860)

2009-07-28 Thread smckinney
Hi all,

When I install a new package, and don't yet know any function names,
I have to play the "poor man's game" to get to the standard
help system "Index" page for the package:
Poor Man's Game -=20
  Load new package;=20
  issue search() command;
  find position (say N) of loaded package;=20
  issue objects(pos =3D N) command;
  get name of a random function (san newFunction);=20
  issue ?newFunction command;
  scroll to bottom of page;=20
  click on the "Index" hyperlink

There are other variations, but they all involve this=20
long march to the Index page.


What I'd like to be able to do is enter the command

> help(package =3D "survival")

or

> ?survival::

and get the usual hyperlinked help page displayed (the "00Index" page)
instead of the static "text only" display or an error message

(for example, on Windows, this equates to invoking
"C:/PROGRA~1/R/R-29~1.1/library/survival/chm/00Index"
on Apple Mac,
"/Library/Frameworks/R.framework/Resources/library/survival/html/00Index.ht=
ml"
etc.)


Details:
---

The help() function returns an object of
class "help_files_with_topic".
The object consists of a character vector
with several attributes.

PC:  Windows XP

> library("survival")
> foo <- help("aareg", package =3D "survival")
> class(foo)
[1] "help_files_with_topic"
> foo[1]
[1] "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/aareg"
> attributes(foo)
$call
help(topic =3D "aareg", package =3D "survival")

$pager
[1] "internal"

$topic
[1] "aareg"

$tried_all_packages
[1] FALSE

$type
[1] "chm"

$class
[1] "help_files_with_topic"

> bar <- help("", package =3D "survival")
> class(bar)
[1] "help_files_with_topic"
> bar[1]
[1] NA
> attributes(bar)
$call
help(topic =3D "", package =3D "survival")

$pager
[1] "internal"

$topic
[1] ""

$tried_all_packages
[1] FALSE

$type
[1] "chm"

$class
[1] "help_files_with_topic"

If I alter the character vector to
point to "00Index"

> bar[1] <- "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/00Index"
> bar

I see exactly what I've been attempting to achieve.


Mac OS X:

> foo <- help("aareg", package =3D "survival")
> foo[1]
[1] "/Library/Frameworks/R.framework/Resources/library/survival/html/aareg.=
html"
> attributes(foo)
$call
help(topic =3D "aareg", package =3D "survival")

$pager
[1] "/Library/Frameworks/R.framework/Resources/bin/pager"

$topic
[1] "aareg"

$tried_all_packages
[1] FALSE

$type
[1] "html"

$class
[1] "help_files_with_topic"

> bar <- help("", package =3D "survival")
> bar[1]
[1] NA
> bar[1] <- "/Library/Frameworks/R.framework/Resources/library/survival/htm=
l/00Index.html"
> bar

Again I see exactly what I've been after.

Running R in Emacs on Mac OS X:

> foo <- help(topic =3D "aareg", package =3D "survival")
> foo[1]
[1] "/Library/Frameworks/R.framework/Resources/library/survival/html/aareg.=
html"
> attributes(foo)
$call
help(topic =3D "aareg", package =3D "survival")

$pager
[1] "/Library/Frameworks/R.framework/Resources/bin/pager"

$topic
[1] "aareg"

$tried_all_packages
[1] FALSE

$type
[1] "html"

$class
[1] "help_files_with_topic"

> bar <- help(topic =3D "", package =3D "survival")
> bar[1]
[1] NA
> bar[1] <- "/Library/Frameworks/R.framework/Resources/library/survival/htm=
l/00Index.html"
> bar
Help for '' is shown in browser /usr/bin/open ...
Use
help("", htmlhelp =3D FALSE)
or
    options(htmlhelp =3D FALSE)
to revert.
>

Again, what I've been trying to achieve.

When a user loads a new library and doesn't yet know any function names,
I think
> help(package =3D "newLibrary")
or
> ?newLibrary::

should perform the above action whenever possible instead of
producing static help or an error message.

The "00Index" 'object' should be available for such use
whenever it exists. =20

I have not yet worked out all the coding details to make this happen,
but before I do, am I missing some key point?  Any reasons why this
would be a Bad Idea?



Steven McKinney, Ph.D.

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wishlist: Navigate to "Index" page of help when no topic (PR#13871)

2009-08-04 Thread smckinney
 "survival")
> >
> > $pager
> > [1] "/Library/Frameworks/R.framework/Resources/bin/pager"
> >
> > $topic
> > [1] "aareg"
> >
> > $tried_all_packages
> > [1] FALSE
> >
> > $type
> > [1] "html"
> >
> > $class
> > [1] "help_files_with_topic"
> >
> >> bar <- help("", package =3D3D "survival")
> >> bar[1]
> > [1] NA
> >> bar[1] <-
> "/Library/Frameworks/R.framework/Resources/library/survival/htm=3D
> > l/00Index.html"
> >> bar
> >
> > Again I see exactly what I've been after.
> >
> > Running R in Emacs on Mac OS X:
> >
> >> foo <- help(topic =3D3D "aareg", package =3D3D "survival")
> >> foo[1]
> > [1]
> "/Library/Frameworks/R.framework/Resources/library/survival/html/aareg.
> =3D
> > html"
> >> attributes(foo)
> > $call
> > help(topic =3D3D "aareg", package =3D3D "survival")
> >
> > $pager
> > [1] "/Library/Frameworks/R.framework/Resources/bin/pager"
> >
> > $topic
> > [1] "aareg"
> >
> > $tried_all_packages
> > [1] FALSE
> >
> > $type
> > [1] "html"
> >
> > $class
> > [1] "help_files_with_topic"
> >
> >> bar <- help(topic =3D3D "", package =3D3D "survival")
> >> bar[1]
> > [1] NA
> >> bar[1] <-
> "/Library/Frameworks/R.framework/Resources/library/survival/htm=3D
> > l/00Index.html"
> >> bar
> > Help for '' is shown in browser /usr/bin/open ...
> > Use
> > help("", htmlhelp =3D3D FALSE)
> > or
> > options(htmlhelp =3D3D FALSE)
> > to revert.
> >>
> >
> > Again, what I've been trying to achieve.
> >
> > When a user loads a new library and doesn't yet know any function
> names,
> > I think
> >> help(package =3D3D "newLibrary")
> > or
> >> ?newLibrary::
> >
> > should perform the above action whenever possible instead of
> > producing static help or an error message.
> >
> > The "00Index" 'object' should be available for such use
> > whenever it exists. =3D20
> >
> > I have not yet worked out all the coding details to make this happen,
> > but before I do, am I missing some key point?  Any reasons why this
> > would be a Bad Idea?
> >
> >
> >
> > Steven McKinney, Ph.D.
> >
> > Statistician
> > Molecular Oncology and Breast Cancer Program
> > British Columbia Cancer Research Centre
> >
> > email: smckinney +at+ bccrc +dot+ ca
> >
> > tel: 604-675-8000 x7561
> >
> > BCCRC
> > Molecular Oncology
> > 675 West 10th Ave, Floor 4
> > Vancouver B.C.
> > V5Z 1L3
> > Canada
> >
> > __
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Wishlist: Navigate to "Index" page of help when no topic (PR#13873)

2009-08-04 Thread smckinney
> >>>
> >>> The help() function returns an object of
> >>> class "help_files_with_topic".
> >>> The object consists of a character vector
> >>> with several attributes.
> >>>
> >>> PC:  Windows XP
> >>>
> >>>> library("survival")
> >>>> foo <- help("aareg", package =3D3D "survival")
> >>>> class(foo)
> >>> [1] "help_files_with_topic"
> >>>> foo[1]
> >>> [1] "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/aareg"
> >>>> attributes(foo)
> >>> $call
> >>> help(topic =3D3D "aareg", package =3D3D "survival")
> >>>
> >>> $pager
> >>> [1] "internal"
> >>>
> >>> $topic
> >>> [1] "aareg"
> >>>
> >>> $tried_all_packages
> >>> [1] FALSE
> >>>
> >>> $type
> >>> [1] "chm"
> >>>
> >>> $class
> >>> [1] "help_files_with_topic"
> >>>
> >>>> bar <- help("", package =3D3D "survival")
> >>>> class(bar)
> >>> [1] "help_files_with_topic"
> >>>> bar[1]
> >>> [1] NA
> >>>> attributes(bar)
> >>> $call
> >>> help(topic =3D3D "", package =3D3D "survival")
> >>>
> >>> $pager
> >>> [1] "internal"
> >>>
> >>> $topic
> >>> [1] ""
> >>>
> >>> $tried_all_packages
> >>> [1] FALSE
> >>>
> >>> $type
> >>> [1] "chm"
> >>>
> >>> $class
> >>> [1] "help_files_with_topic"
> >>>
> >>> If I alter the character vector to
> >>> point to "00Index"
> >>>
> >>>> bar[1] <- "C:/PROGRA~1/R/R-29~1.1/library/survival/chm/00Index"
> >>>> bar
> >>> I see exactly what I've been attempting to achieve.
> >>>
> >>>
> >>> Mac OS X:
> >>>
> >>>> foo <- help("aareg", package =3D3D "survival")
> >>>> foo[1]
> >>> [1]
> >>
> "/Library/Frameworks/R.framework/Resources/library/survival/html/aareg.
> >> =3D
> >>> html"
> >>>> attributes(foo)
> >>> $call
> >>> help(topic =3D3D "aareg", package =3D3D "survival")
> >>>
> >>> $pager
> >>> [1] "/Library/Frameworks/R.framework/Resources/bin/pager"
> >>>
> >>> $topic
> >>> [1] "aareg"
> >>>
> >>> $tried_all_packages
> >>> [1] FALSE
> >>>
> >>> $type
> >>> [1] "html"
> >>>
> >>> $class
> >>> [1] "help_files_with_topic"
> >>>
> >>>> bar <- help("", package =3D3D "survival")
> >>>> bar[1]
> >>> [1] NA
> >>>> bar[1] <-
> >> "/Library/Frameworks/R.framework/Resources/library/survival/htm=3D
> >>> l/00Index.html"
> >>>> bar
> >>> Again I see exactly what I've been after.
> >>>
> >>> Running R in Emacs on Mac OS X:
> >>>
> >>>> foo <- help(topic =3D3D "aareg", package =3D3D "survival")
> >>>> foo[1]
> >>> [1]
> >>
> "/Library/Frameworks/R.framework/Resources/library/survival/html/aareg.
> >> =3D
> >>> html"
> >>>> attributes(foo)
> >>> $call
> >>> help(topic =3D3D "aareg", package =3D3D "survival")
> >>>
> >>> $pager
> >>> [1] "/Library/Frameworks/R.framework/Resources/bin/pager"
> >>>
> >>> $topic
> >>> [1] "aareg"
> >>>
> >>> $tried_all_packages
> >>> [1] FALSE
> >>>
> >>> $type
> >>> [1] "html"
> >>>
> >>> $class
> >>> [1] "help_files_with_topic"
> >>>
> >>>> bar <- help(topic =3D3D "", package =3D3D "survival")
> >>>> bar[1]
> >>> [1] NA
> >>>> bar[1] <-
> >> "/Library/Frameworks/R.framework/Resources/library/survival/htm=3D
> >>> l/00Index.html"
> >>>> bar
> >>> Help for '' is shown in browser /usr/bin/open ...
> >>> Use
> >>> help("", htmlhelp =3D3D FALSE)
> >>> or
> >>> options(htmlhelp =3D3D FALSE)
> >>> to revert.
> >>> Again, what I've been trying to achieve.
> >>>
> >>> When a user loads a new library and doesn't yet know any function
> >> names,
> >>> I think
> >>>> help(package =3D3D "newLibrary")
> >>> or
> >>>> ?newLibrary::
> >>> should perform the above action whenever possible instead of
> >>> producing static help or an error message.
> >>>
> >>> The "00Index" 'object' should be available for such use
> >>> whenever it exists. =3D20
> >>>
> >>> I have not yet worked out all the coding details to make this
> happen,
> >>> but before I do, am I missing some key point?  Any reasons why this
> >>> would be a Bad Idea?
> >>>
> >>>
> >>>
> >>> Steven McKinney, Ph.D.
> >>>
> >>> Statistician
> >>> Molecular Oncology and Breast Cancer Program
> >>> British Columbia Cancer Research Centre
> >>>
> >>> email: smckinney +at+ bccrc +dot+ ca
> >>>
> >>> tel: 604-675-8000 x7561
> >>>
> >>> BCCRC
> >>> Molecular Oncology
> >>> 675 West 10th Ave, Floor 4
> >>> Vancouver B.C.
> >>> V5Z 1L3
> >>> Canada
> >>>
> >>> __
> >>> R-devel@r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] loadings function (PR#13886)

2009-08-11 Thread smckinney
Hi Mike,

Please don't file bug reports without first posing such
questions on R-help or R-devel first, asking if such behaviour
really is a bug.  (This generates extra work for the volunteers
who maintain R).

In this case, it's your misunderstanding of the information
about three different functions on the "loadings" help
page.  The loadings() function is shown with one argument
"x" only.  The other parameters described are for the
print methods also discussed on the loadings() help page.

Looking at the factanal example:

> # A little demonstration, v2 is just v1 with noise,
> # and same for v4 vs. v3 and v6 vs. v5
> # Last four cases are there to add noise
> # and introduce a positive manifold (g factor)
> v1 <- c(1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,4,5,6)
> v2 <- c(1,2,1,1,1,1,2,1,2,1,3,4,3,3,3,4,6,5)
> v3 <- c(3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,5,4,6)
> v4 <- c(3,3,4,3,3,1,1,2,1,1,1,1,2,1,1,5,6,4)
> v5 <- c(1,1,1,1,1,3,3,3,3,3,1,1,1,1,1,6,4,5)
> v6 <- c(1,1,1,2,1,3,3,3,4,3,1,1,1,2,1,6,5,4)
> m1 <- cbind(v1,v2,v3,v4,v5,v6)
>=20
>=20
> fa <- factanal(m1, factors=3D3) # varimax is the default
> fa

Call:
factanal(x =3D m1, factors =3D 3)

Uniquenesses:
   v1v2v3v4v5v6=20
0.005 0.101 0.005 0.224 0.084 0.005=20

Loadings:
   Factor1 Factor2 Factor3
v1 0.944   0.182   0.267 =20
v2 0.905   0.235   0.159 =20
v3 0.236   0.210   0.946 =20
v4 0.180   0.242   0.828 =20
v5 0.242   0.881   0.286 =20
v6 0.193   0.959   0.196 =20

   Factor1 Factor2 Factor3
SS loadings  1.893   1.886   1.797
Proportion Var   0.316   0.314   0.300
Cumulative Var   0.316   0.630   0.929

The degrees of freedom for the model is 0 and the fit was 0.4755=20

### Now extract just the loadings
> loadings(fa)

Loadings:
   Factor1 Factor2 Factor3
v1 0.944   0.182   0.267 =20
v2 0.905   0.235   0.159 =20
v3 0.236   0.210   0.946 =20
v4 0.180   0.242   0.828 =20
v5 0.242   0.881   0.286 =20
v6 0.193   0.959   0.196 =20

   Factor1 Factor2 Factor3
SS loadings  1.893   1.886   1.797
Proportion Var   0.316   0.314   0.300
Cumulative Var   0.316   0.630   0.929

### Now attempt
> loadings(fa, digits =3D 5)
Error in loadings(fa, digits =3D 5) : unused argument(s) (digits =3D 5)

### loadings() takes only one argument.

### The digits and other arguments are for the print methods, e.g.

> print(fa, digits =3D 5)

Call:
factanal(x =3D m1, factors =3D 3)

Uniquenesses:
 v1  v2  v3  v4  v5  v6=20
0.00500 0.10090 0.00500 0.22405 0.08429 0.00500=20

Loadings:
   Factor1 Factor2 Factor3
v1 0.94384 0.18193 0.26661
v2 0.90472 0.23484 0.15948
v3 0.23564 0.20960 0.94634
v4 0.17998 0.24225 0.82757
v5 0.24211 0.88064 0.28560
v6 0.19277 0.95884 0.19621

   Factor1 Factor2 Factor3
SS loadings1.89304 1.88576 1.79702
Proportion Var 0.31551 0.31429 0.29950
Cumulative Var 0.31551 0.62980 0.92930

The degrees of freedom for the model is 0 and the fit was 0.4755=20

> print(loadings(fa), digits =3D 5)

Loadings:
   Factor1 Factor2 Factor3
v1 0.94384 0.18193 0.26661
v2 0.90472 0.23484 0.15948
v3 0.23564 0.20960 0.94634
v4 0.17998 0.24225 0.82757
v5 0.24211 0.88064 0.28560
v6 0.19277 0.95884 0.19621

   Factor1 Factor2 Factor3
SS loadings1.89304 1.88576 1.79702
Proportion Var 0.31551 0.31429 0.29950
Cumulative Var 0.31551 0.62980 0.92930
>=20

Best

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada


From: r-devel-boun...@r-project.org [r-devel-boun...@r-project.org] On Beha=
lf Of mikedulr...@gmail.com [mikedulr...@gmail.com]
Sent: August 11, 2009 9:15 AM
To: r-de...@stat.math.ethz.ch
Cc: r-b...@r-project.org
Subject: [Rd] loadings function (PR#13886)

Full_Name: Mike Ulrich
Version: 2.9
OS: Mac OSX
Submission from: (NULL) (69.169.178.34)


The help documentation for loadings() lists more then one parameter. The
function call only expects one parameter. The digits, cutoff, and sort
parameters are not used in the function.

## S3 method for class 'loadings':
print(x, digits =3D 3, cutoff =3D 0.1, sort =3D FALSE, ...)

## S3 method for class 'factanal':
print(x, digits =3D 3, ...)


> loadings
function (x)
x$loadings


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Cannot Change Function (PR#14041)

2009-11-04 Thread smckinney


> -Original Message-
> From: r-devel-boun...@r-project.org [mailto:r-devel-boun...@r-
> project.org] On Behalf Of Stefan Evert
> Sent: Wednesday, November 04, 2009 4:40 AM
> To: michael_ka...@earthlink.net
> Cc: r-b...@r-project.org; r-de...@stat.math.ethz.ch
> Subject: Re: [Rd] Cannot Change Function (PR#14041)
>=20
> What makes you think this is a bug in R?
>=20
> > Whenever I try changing a function, it keeps coming up with the same
> > error
> > message.
> >
> > I have the function
> > CN_state_log_sum=3Dfunction(Tot_log_sum){ #estimate copy number state
> > for the log
>   [...]
> > }
> >
> > When I try to run it in the loop:
> > for (j in 1:length(BB_mean_ref)){   # find copy number states (0 for
> > homozygous
> > deletion, 1 for hemizygous deletion, 2 for neutral, 3 for single
> > amplification,
> > 4 for multiple amplification)
> > state_log_sum[j]=3DCN_state_log_sum(Tot_log_sum[j])
> > state_sum_log[j]=3DCN_state_sum_log(Tot_sum_log[j])
> > }
> > I get the error message:
> > Error in Im(Tot_sum_log) !=3D 0 || Re(Tot_sum_log) <- 2 :
> >  could not find function "||<-"
>=20
> The error message indicates that the problem happens in the function
> CN_state_sum_log() rather than CN_state_log_sum(), so it's hardly
> surprising that changing the latter doesn't have any effect.
>=20
> My guess: you've go a condition (Re(Tot_sum_log)<-2), which is
> mistaken for the assignment operator "<-" by the R parser. Adding some
> whitespace (Re(Tot_sum_log) < -2) should help both R and human readers
> to make sense of it.

Adding parentheses around the negative number is the only way to keep
such an expression robustly safe given the various whitespace alterations
that happen as such text passes through different parsers, editors,
mailers, proportional pitch font displays (which can fool human readers) ..=
.

( ( Im(Tot_sum_log) !=3D 0 ) || ( Re(Tot_sum_log) < (-2) ) )

Best

Steven McKinney

>=20
> Best,
> Stefan
>=20
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Bug in package stats function ar() (PR#10459)

2007-11-23 Thread smckinney
Full_Name: Steven McKinney
Version: 2.6.0
OS: OS X
Submission from: (NULL) (142.103.207.10)



Function ar() in package "stats" is showing
a quirky bug.  Some calls to ar() run to
completion, others throw an error.

The bug is reproducible by several people on different
machines, however, the ar() function itself ends
up throwing the error sporadically.  Several calls to
ar() may be necessary to trip the error condition.

Code to reproduce:

x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
# ar function
res.ar<-ar(x,aic=TRUE,demean=F)
# call "ar" again and 
res.ar<-ar(x,aic=TRUE,demean=F)


Example output:
(Note that on this attempt the first call to ar()
 tripped the error.)

> x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-
+ 1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
> 
> # ar function
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In log(var.pred) : NaNs produced
2: In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> 
> # call "ar" again and 
> res.ar<-ar(x,aic=TRUE,demean=F)
> x
   Qtr1   Qtr2   Qtr3   Qtr4
1978   -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164   
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar$resid
   Qtr1   Qtr2   Qtr3   Qtr4
1978   -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164   
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar$resid
   Qtr1   Qtr2   Qtr3   Qtr4
1978   -0.2052083 -0.3764986
1979 -0.3762448  0.3740089  0.2737568  2.8235722
1980 -1.7783313  0.2728676 -0.3273164   
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In log(var.pred) : NaNs produced
2: In if (order > 0) coefs[order, 1:order] else numeric(0) :
  the condition has length > 1 and only the first element will be used
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> sessionInfo()
R version 2.6.0 (2007-10-03) 
powerpc-apple-darwin8.10.1 

locale:
C

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


###-

Also seen in R 2.5.0:

> x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1980,3))
> # ar function
> res.ar<-ar(x,aic=TRUE,demean=F)
> # call "ar" again and 
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
> res.ar<-ar(x,aic=TRUE,demean=F)
Error in if (order > 0) coefs[order, 1:order] else numeric(0) : 
missing value where TRUE/FALSE needed
In addition: Warning message:
the condition has length > 1 and only the first element will be used in: if
(order > 0) coefs[order, 1:order] else numeric(0) 
> sessionInfo()
R version 2.5.0 (2007-04-23) 
x86_64-redhat-linux-gnu 

locale:
LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_P
APER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C

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

###-

However, if I lengthen the input vector 

x<-ts(c(-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164,
  
-0.2052083,-0.3764986,-0.3762448,0.3740089,0.2737568,2.8235722,-1.7783313,0.2728676,-0.3273164),start=c(1978,3),frequency=4,end=c(1982,3))

I can not trip the error, so perhaps it is due to the short length of
the user's input data.



###-

Nov 23, 2007 original email from R-help

[R] help please


Dears Sirs

During my computational work 

[Rd] lines.formula() problem when data argument is missing (PR#13296)

2008-11-17 Thread smckinney
Full_Name: Steven McKinney
Version: R 2.8.0 Patched svn rev 46845
OS: powerpc-apple-darwin9.5.0
Submission from: (NULL) (142.103.207.10)



<>

lines.formula() throws an error when subset argument is used but nothing is
provided for data argument.

Reproduce:

x<-1:5
y<-c(1,3,NA,2,5)
plot(y~x, type="n")  # set up frame
lines(y~x, subset=!is.na(y))  # works OK
lines(y~x, type="o", col="blue")  # works OK
# but
lines(y~x, subset=!is.na(y), col="red")  # gives an error:



This situation is handled appropriately by points.formula().

Following the coding style of points.formula() the 
proposed modifications would be

lines.formula <-
function (formula, data = parent.frame(), ..., subset) 
{
m <- match.call(expand.dots = FALSE)
if (is.matrix(eval(m$data, parent.frame( 
m$data <- as.data.frame(data)
dots <- m$...
dots <- lapply(dots, eval, data, parent.frame())
m$... <- NULL
m[[1]] <- as.name("model.frame")
m <- as.call(c(as.list(m), list(na.action = NULL)))
mf <- eval(m, parent.frame())
if (!missing(subset)) {
s <- eval(m$subset, data, parent.frame())
###current
###   l <- nrow(data)
###\current
###new (as per points.formula)
if (!missing(data)) {
l <- nrow(data)
}
else {
mtmp <- m
mtmp$subset <- NULL
l <- nrow(eval(mtmp, parent.frame()))
}
###\new
dosub <- function(x) if (length(x) == l) 
x[s]
else x
###current
###dots <- lapply(dots, dosub, s)
###\current
###new (as per points.formula)
dots <- lapply(dots, dosub)
###\new
}
response <- attr(attr(mf, "terms"), "response")
if (response) {
varnames <- names(mf)
y <- mf[[response]]
if (length(varnames) > 2) 
stop("cannot handle more than one 'x' coordinate")
xn <- varnames[-response]
if (length(xn) == 0) 
do.call("lines", c(list(y), dots))
else do.call("lines", c(list(mf[[xn]], y), dots))
}
else stop("must have a response variable")
}


Original report from R-help was:


> -Original Message-
> From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
> On Behalf Of John Field
> Sent: Thursday, November 13, 2008 4:41 PM
> To: [EMAIL PROTECTED]
> Subject: [R] lines.formula with subset
>
> Dear list,
>
> When I try to use lines.formula with subset and another argument I
> get an error.
> e.g.
>
> x<-1:5
> y<-c(1,3,NA,2,5)
> plot(y~x, type="n")  # set up frame
> lines(y~x, subset=!is.na(y))  # works OK
> lines(y~x, type="o", col="blue")  # works OK
> # but
> lines(y~x, subset=!is.na(y), col="red")  # gives an error:
>
> Error in if (length(x) == l) x[s] else x : argument is of length zero
>
> Why does this happen?
>

It happens because the function
graphics:::lines.formula

tries to assess the number of rows of data in the data frame
containing the variables in the formula y~x
(see the line of code
l <- nrow(data)
in graphics:::lines.formula
This is the 'el' in the 'length(x) == l'
portion of the line you see in the error message)

Because you did not provide the data frame,
nrow(data) returns NULL, and thus the
if() clause is 'length(x) == NULL' which
yields answer logical(0), an invalid
answer in an if() clause.

Done this way, all is well:

mydf <- data.frame(x = 1:5, y = c(1,3,NA,2,5))
plot(y~x, type="n", data = mydf)  # set up frame
lines(y~x, subset=!is.na(y), data = mydf)  # works OK
lines(y~x, type="o", col="blue", data = mydf)  # works OK
lines(y~x, subset=!is.na(y), col="red", data = mydf)  # works OK

The formula - based functions expect to see a dataframe object
from the 'data' arg, but don't enforce this in this case.

This may qualify as a logical bug in the graphics:::lines.formula
function.  An error should have been thrown before the if()
clause evaluation, but I'm not sure where in the chain of
function calls the check for a valid data object should be
done and the error thrown. Otherwise, the data objects
y and x that you set up should have been passed downwards
in some fashion for evaluation.  R-core members who know
the rules better than I will have to determine how best to
handle this one.

HTH

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: [EMAIL PROTECTED]
tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3

Canada



> With thanks,
> John
>
> =
> John Field Consulting Pty Ltd
> 10 High Street, Burnside SA 5066
> Phone 08 8332 5294 or 0409 097 586
> Fax   08 8332 1229
> Email  [EMAIL PROTECTED]
>
> __
> [EMAIL PROTECTED] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




--please do not edit the information