Re: [R] Forestplot, grid graphics Viewplot grid.arange

2022-10-13 Thread Jim Lemon
Hi Mary,
I didn't see any answers to your post, but doing something like this
is quite easy in base graphics. If you are still stuck, I may be able
to suggest something.

Jim

On Mon, Oct 10, 2022 at 6:05 PM Putt, Mary  wrote:
>
>
> I have created several plots using the forestplot package and the link shown 
> here. 
>  > Great package !
> Next step is to combine two plots into a single graphic. The code provided on 
> the link results in 'bleeding' of the graphics/text into each other. I don't 
> want to clip it as I need the text elements. I am guessing the problem 
> involves the combination of text and graphics in the 'plot'. I fooled around 
> with the original post and also did some hunting online but no luck Thanks in 
> advance.
> library(foresplot)
> data("dfHRQoL")
>
> #create individual forest plots for Sweden and Denmark
> fp_sweden <- dfHRQoL |>
>   filter(group == "Sweden") |>
>   mutate(est = sprintf("%.2f", mean), .after = labeltext) |>
>   forestplot(labeltext = c(labeltext, est),
>  title = "Sweden",
>  clip = c(-.1, Inf),
>  xlab = "EQ-5D index",
>  new_page = FALSE)
>
> fp_denmark <- dfHRQoL |>
>   filter(group == "Denmark") |>
>   mutate(est = sprintf("%.2f", mean), .after = labeltext) |>
>   forestplot(labeltext = c(labeltext, est),
>  title = "Denmark",
>  clip = c(-.1, Inf),
>  xlab = "EQ-5D index",
>  new_page = FALSE)
>
>
>
> #now combine into a single plot using the web code; but this one bleeds into 
> each other
> library(grid)
>
> #
> #Put plots together using grid graphics
> #Attempt 1 from website
>
> #
> grid.newpage()
> borderWidth <- unit(4, "pt")
> width <- unit(convertX(unit(1, "npc") - borderWidth, unitTo = "npc", 
> valueOnly = TRUE)/2, "npc")
> pushViewport(viewport(layout = grid.layout(nrow = 1,
>ncol = 3,
>widths = unit.c(width,
>borderWidth,
>width))
> )
> )
> pushViewport(viewport(layout.pos.row = 1,
>   layout.pos.col = 1))
> fp_sweden
> upViewport()
> pushViewport(viewport(layout.pos.row = 1,
>   layout.pos.col = 2))
> grid.rect(gp = gpar(fill = "grey", col = "red"))
> upViewport()
> pushViewport(viewport(layout.pos.row = 1,
>   layout.pos.col = 3))
> fp_denmark
> upViewport(2)
>
>
> 
> #Attempt 2 from website, still a problem.
> 
> grid.newpage()
> borderWidth <- unit(4, "pt")
> width <- unit(convertX(unit(1, "npc") - borderWidth, unitTo = "npc", 
> valueOnly = TRUE)/2, "npc")
> pushViewport(viewport(layout = grid.layout(nrow = 1,
>ncol = 3,
>widths = c(0.45, 0.1, 0.45))
> )
> )
> pushViewport(viewport(layout.pos.row = 1,
>   layout.pos.col = 1))
> fp_sweden
> upViewport()
>
> pushViewport(viewport(layout.pos.row = 1,
>   layout.pos.col = 3))
> fp_denmark
> upViewport(2)
>
> ###
> #Attempt 3 converting to grobs and use patchwork
> ###
> library(ggplotify)
> library(patchwork)
>
> fpd_grob <- grid2grob(print(fp_denmark))
>
> p1 <- grid2grob(print(fp_denmark))
> p2 <- grid2grob(print(fp_sweden))
> p_both <- wrap_elements(p1) + wrap_elements(p2)
> p_both
>
> #same problem with grid.arrange()**strong text**
>
>
>
> Mary Putt, PhD, ScD
> Professor of Biostatistics
> Department of Biostatistics, Epidemiology & Informatics
> Pereleman School of Medicine
> University of Pennsylvania
>
> 215-573-7020
>
> [[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] cat in a subroutine

2022-10-13 Thread Steven T. Yen
Hello, Oh Lord, yes, I had a function called "cat", with argument "j". 
That was very dumb.

Renaming function cat resolved the problem. I had lived with this 
problem too long---avoiding printing with cat altogether in this 
program. Thanks to all-Bill, Iva, Jim, Erin, Andrew for help!

On 10/13/2022 10:30 PM, Bill Dunlap wrote:
> Do you have another function called "cat" in scope? (with an argument 
> called "j")?  Before calling cat("...") call print(cat) and 
> print(find("cat")).
>
> -Bill
>
>
> On Thu, Oct 13, 2022 at 12:35 AM Steven T. Yen  wrote:
>
> I have had an issue with printing (with cat) in a subroutine for
> which I
> do not have a applicable example, but I am still hoping to get some
> help. In the following, the first block of code works fine.
>
> ...
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> But as soon as I insert lines to print (cat) soething simple, it
> spits
> out message that appears to be nonsence (unrelated). Any idea. Please
> help. Thanks.
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> In this particular case, the error message was as follows:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities",  :
>    unused argument (logistic)
>
> I have printed this way in numerous routines without problem and
> do not
> see why this is happending.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/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] intersection in data frame

2022-10-13 Thread Dénes Tóth

Or if your data is really large, you can try data.table::dcast().

> library(data.table)
> dcast(ID ~ station, data = as.data.table(df1))
   ID xy xz
1: 12 15 20
2: 13 16 19

(Note: instead of `as.data.table()`, you can use `setDT` or create your 
object as a data.table in the first place.)



On 10/13/22 11:22 PM, Rui Barradas wrote:

Hello,

To reshape from long to wide format, here are two options:


df1 <- 'ID    station  value
12  xy    15
12  xz    20
13   xy   16
13   xz   19'
df1 <- read.table(textConnection(df1), header = TRUE)


# base R
reshape(df1, direction = "wide", idvar = "ID", timevar = "station")
#>   ID value.xy value.xz
#> 1 12   15   20
#> 3 13   16   19

# tidyverse
tidyr::pivot_wider(df1, ID, names_from = station)
#> # A tibble: 2 × 3
#>  ID    xy    xz
#>     
#> 1    12    15    20
#> 2    13    16    19


This question is StackOverflow question [1].

[1] 
https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format 




Hope this helps,

Rui Barradas


Às 19:08 de 13/10/2022, Gábor Malomsoki escreveu:

Dears,

i need to create from a column of observations variables in a datafram 
like

this way:
example:
original:
ID    station  value
12  xy    15
12  xz    20
13   xy   16
13   xz   19

new df:

  ID  xy xz
12  15 20
13  16 19

i have been looking around for examples, but i could not find any how to
change my df.
I would like to make regression analysis on the values from different
production stations, so my df is very huge.

Please help on finding the package, description or anything else could 
help.


Thank you in advance!

Best regards
Malo

[[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] intersection in data frame

2022-10-13 Thread Rui Barradas

Hello,

To reshape from long to wide format, here are two options:


df1 <- 'IDstation  value
12  xy15
12  xz20
13   xy   16
13   xz   19'
df1 <- read.table(textConnection(df1), header = TRUE)


# base R
reshape(df1, direction = "wide", idvar = "ID", timevar = "station")
#>   ID value.xy value.xz
#> 1 12   15   20
#> 3 13   16   19

# tidyverse
tidyr::pivot_wider(df1, ID, names_from = station)
#> # A tibble: 2 × 3
#>  IDxyxz
#> 
#> 1121520
#> 2131619


This question is StackOverflow question [1].

[1] 
https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format



Hope this helps,

Rui Barradas


Às 19:08 de 13/10/2022, Gábor Malomsoki escreveu:

Dears,

i need to create from a column of observations variables in a datafram like
this way:
example:
original:
IDstation  value
12  xy15
12  xz20
13   xy   16
13   xz   19

new df:

  ID  xy xz
12  15 20
13  16 19

i have been looking around for examples, but i could not find any how to
change my df.
I would like to make regression analysis on the values from different
production stations, so my df is very huge.

Please help on finding the package, description or anything else could help.

Thank you in advance!

Best regards
Malo

[[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] intersection in data frame

2022-10-13 Thread Ben Tupper
Hi,

If you are game to use a tidy approach then you can use tidyr::pivot_wider()

library(dplyr)
library(tidyr)

x <- dplyr::tribble(
  ~ID, ~station,  ~value,
  12, "xy", 15,
  12, "xz", 20,
  13, "xy", 16,
  13, "xz", 19)

tidyr::pivot_wider(x,
   names_from = station,
   values_from = value)
#> # A tibble: 2 × 3
#>  IDxyxz
#> 
#> 1121520
#> 2131619

Cheers,
Ben


On Thu, Oct 13, 2022 at 5:07 PM Gábor Malomsoki
 wrote:
>
> Dears,
>
> i need to create from a column of observations variables in a datafram like
> this way:
> example:
> original:
> IDstation  value
> 12  xy15
> 12  xz20
> 13   xy   16
> 13   xz   19
>
> new df:
>
>  ID  xy xz
> 12  15 20
> 13  16 19
>
> i have been looking around for examples, but i could not find any how to
> change my df.
> I would like to make regression analysis on the values from different
> production stations, so my df is very huge.
>
> Please help on finding the package, description or anything else could help.
>
> Thank you in advance!
>
> Best regards
> Malo
>
> [[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] intersection in data frame

2022-10-13 Thread Gábor Malomsoki
Dears,

i need to create from a column of observations variables in a datafram like
this way:
example:
original:
IDstation  value
12  xy15
12  xz20
13   xy   16
13   xz   19

new df:

 ID  xy xz
12  15 20
13  16 19

i have been looking around for examples, but i could not find any how to
change my df.
I would like to make regression analysis on the values from different
production stations, so my df is very huge.

Please help on finding the package, description or anything else could help.

Thank you in advance!

Best regards
Malo

[[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] [R-pkgs] gsDesign 3.4.0 is released

2022-10-13 Thread Nan Xiao
Dear all,

A new version of gsDesign (3.4.0) is now on CRAN 
(https://cran.r-project.org/package=gsDesign). gsDesign supports group 
sequential clinical trial design, largely as presented in Jennison and Turnbull 
(2000).

This version removes restrictions on conditional power calculations for interim 
test statistic and brings improvements on dependencies and lifecycle 
management. Please see the changelog (https://keaven.github.io/gsDesign/news/) 
for details.

Thanks,
-Nan

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] UTF-8 to the console

2022-10-13 Thread Tomas Kalibera



On 6/23/22 13:36, Ivan Krylov wrote:

On Thu, 23 Jun 2022 12:26:23 +0200
Helmut Schütz  wrote:


txt <- "x ≥ y, x \u2265 y; a ≈ b, a \u2248 b"
Encoding(txt) <- "UTF-8"

There shouldn't be a need to change the encoding. If you're creating a
Unicode literal, R should already choose UTF-8 for the resulting
string. Either way, R automatically converts the strings from their
source encoding on output.

Moreover, `Encoding<-` doesn't perform any conversion, it only changes
the declared encoding on the string, affecting the way it may be
encoded or decoded in the future. If Encoding(txt) wasn't already UTF-8,
you would likely be damaging the data:

string <- 'Ы' # is already UTF-8
# No conversion happens, the same bytes re-interpreted differently
Encoding(string) <- 'latin1'
string
# [1] "Ы"


R 4.2.0 on Windows 7

On Windows 7, Rterm will stay limited to the OEM encoding, since UCRT
only supports UTF-8 locales on Windows ≥ 10, version 1903. If your OEM
encoding doesn't have the ≥, ≈ characters, printing them to the console
is going to be hard. Not impossible -- e.g. an R extension written in C
could obtain a handle to the current console and use Unicode-aware
Windows API to print these characters -- but just getting it to work
would be hard, and it will be likely unportable.


and Windows 11.

I think it should be possible. What does system('chcp') say in your
Rterm session?

For console UTF-8 output to work, two things should happen:

1. The console must be using UTF-8, i.e. chcp must say it's using code
page 65001.

2. Rterm must understand that and also use UTF-8 on output.

What does sessionInfo() and l10n_info() say in your Rterm session on
Windows 11? In Rterm source code, I see a check for GetACP() == 65001,
which should have switched the console encoding to UTF-8 automatically.

Perhaps you need to run chcp 65001 before starting Rterm? Maybe you
need to set a checkbox [*] to make the ANSI codepage UTF-8 by default?
I'm not sure any of this is going to work, but it's something to try
before someone more knowledgeable with R on Windows can help you.


You are right, R already tries to set the console code page itself to 
65001, so chcp is no longer needed to change it.


On Windows 7 UTF-8 would not be used by R because it can't be the 
"system encoding" (ACP), and I suppose Helmut's output was from that 
version of Windows:


x = y, x = y; a ˜ b, a ˜ b

This suggests transliteration ("best-fit") of the characters not 
representable in the session encoding, done by Windows. On Windows 7, 
characters not representable in the user locale encoding will not be 
usable in Rterm, there is no way around that, but one can e.g. use Rgui.


On new Windows, such as Windows 11, there was a bug in Rterm as I 
reported in the other email, fixed now. The output on my Windows 10 was:


x  y, x ≥ y; a  b, a ≈ b

So the characters pasted were missing, but the characters expressed via 
\u escapes were printed correctly. This was a problem between the 
windows console implementation used in cmd.exe/powershell and 
Rterm/getline. What is characteristic for these problems is that the 
behavior differs from when Rterm runs from the Windows Terminal 
application (or possibly Msys2 mintty/winpty/bash).


Best
Tomas





__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] UTF-8 to the console

2022-10-13 Thread Tomas Kalibera

Dear Helmut,

thanks for the report, this is actually a bug in Rterm (or Windows, hard 
to tell, but something that can be fixed in Rterm). More below


On 6/23/22 12:26, Helmut Schütz wrote:

Dear all,

I want to send UTF-8 characters to the console. Font in the 
GUI-Preference 'Lucida Console', supporting the desired symbols:
greater than or equal: UTF-8 2265, HTML-entity  HTML-Unicode 
 TeX \ge
approximately equal: UTF-8 2248, HTML-entity  HTML-Unicode 
 TeX \approx


txt <- "x ≥ y, x \u2265 y; a ≈ b, a \u2248 b"
Encoding(txt) <- "UTF-8"
print(txt)
[1] "x = y, x = y; a \230 b, a \230 b"
cat(txt, "\n")
x = y, x = y; a ˜ b, a ˜ b

Desired "x ≥ y, x ≥ y; a ≈ b, a ≈ b"

I'm sending the email in UTF-8. Don’t know how @r-project.org is 
configured (ASCII?) If you see garbage, I'm sorry but you should get 
the idea.


R 4.2.0 on Windows 7 (UCRT10.0.10240.16390) and Windows 11.


The underlying problem I can reproduce on my Windows 10 (which is almost 
surely what you are seeing on Windows 11) is that characters ≥ and ≈ 
cannot be pasted to RTerm when running in cmd.exe or PowerShell. Pasting 
these characters pastes nothing.


I've fixed this now in R-devel 83094 (and R-patched 83095). I would be 
grateful if you (or anyone else) could test e.g. in R-patched, most 
likely this example will work as it did for me, but also other examples 
you can think of. Processing the input keys in Rterm/getline is very 
tricky and brittle. What the code sees depends on what the console 
implementation decides to do, and it differs for different console 
implementations, and sadly this is not documented as far as I could find.


Now, the problem you reported does not happen in Msys2/mintty (so 
Rtools42) terminal, because the terminal uses a different console 
implementation. Also, the problem doesn't happen with the Windows 
Terminal application, which has a yet different implementation. If you 
ever needed a work-around to such problems, I would recommend trying the 
Windows Terminal application.


The problem doesn't happen in Rgui, either, but that uses a different 
code path completely on R end, indeed it does not run Rterm.


There is a key combination "Alt+I" you can press in RTerm, which will 
switch to debug mode and will display the keyboard codes R receives (it 
matches the sources in getline.c). When one sees different behavior of 
things like your reported problem in with different console 
implementations, it usually comes with different keyboard codes sent to R.


Your report has been very useful, thanks, and sorry for the long delay. 
I would have spotted it earlier on R bugzilla (or R-devel) list.


Best
Tomas



Helmut


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] prcomp - arbitrary direction of the returned principal components

2022-10-13 Thread John C Nash
This reminds me of a situation in 1975 where a large computer service bureau had
contracted to migrate scientific software from a Univac 1108 to a an IBM System 
360.
They spent 3 weeks trying to get the IBM to give the same eigenvectors on a 
problem as the
Univac. There were at least 2 eigenvalues that were equal. They were trying to 
fix
something that was not broken. Their desperation was enough to offer me a very 
large fee
to "fix" things. However, I had a nice job, so told them to go away and read a 
couple of
books on the real-symmetric eigenvalue problem or singular value decomposition, 
though the
latter was just becoming known outside of numerical linear algebra.

I suspect the OP should go back to basics with principal components and not try 
to fiddle
with the output. It is likely that the "loadings" (I'm never sure of the 
nomenclature -- I
use the matrix setup) can be rotated, but you can't just rotate one vector of a 
set on its
own.

Amazing how these old issues linger for decades. Or maybe linear algebra is not 
on the
curriculum.

John Nash



On Thu, 2022-10-13 at 19:35 +0530, Ashim Kapoor wrote:
> Dear All,
> 
> Many thanks for your replies.
> 
> My PC1 loading turns out to be :
> 
> 1/sqrt(2) , -1/sqrt(2)
> 
> In simple words : I had 2 variables and I ran prcomp on them. I got my
> PC1 as :  .7071068 var1 - .7071068 var2
> 
> PC2 turned out to be the same as PC1 with a PLUS replacing the minus,
> ie. .7071068 var1 + .7071068 var2
> 
> But forget PC2 for the time being.
> 
> Now my question is : I am not able to use the rule that : choose the
> variable with a bigger magnitude of loading and multiply PC1 by -1 if
> needed (to flip the PC1 since any vector x and it's flipped version -x
>  are the same vector but with opposite direction) if the variable with
> bigger magnitude is of negative sign.
> 
> I have an alternative measure of stress which is trending UP and has 2
> peaks during 2 recessions and I can see that PC1 is trending DOWN and
> has 2 TROUGHS during the same recessions. That's how I wish to FLIP
> PC1 with a negative sign.
> 
> The data is not mine and I am not at liberty to share it. I can
> construct an artificial example but I would need time to do that.
> 
> That's what's happening.
> 
> Best Regards and
> Many thanks.
> Ashim
> 
> 
> 
> 
> 
> 
> 
> 
> 
> On Thu, Oct 13, 2022 at 5:38 PM Ebert,Timothy Aaron  wrote:
> > 
> > I still do not understand. However, the general approach would be to 
> > identify a
> > specific value to test. If the test is TRUE then do "this" otherwise do 
> > nothing. Once
> > the test condition is properly identified, the coding easily follows.
> > 
> >  abs() is the same as
> > if x<0 then x = -x   (non-R code, just idea)
> > The R code might look something more like
> > for (number in 1:ncol(x)){
> >    if (x[3,2] < 0) {
> >  x[number, number] = -x[number, number] #only change the diagonal
> >    }
> > }
> > 
> > Depending on what values need to be changed you may need a nested for loop 
> > to go
> > through all values of x[number1, number2].
> > 
> > Your words: " I can forcefully use a NEGATIVE sign to FLIP the index when 
> > it is LOW."
> > Where it appeared that "low" was defined as values that are negative. You 
> > still will
> > have low values (close to zero) and high values (far from zero).
> > 
> > You could make the condition some other value:
> > 
> > if x< -4 then x = -x
> > 
> > If you just want to rotate about zero then
> > x = -x
> > In this case the positive values will become negative and the negative 
> > values
> > positive.
> > Add an if test to selectively rotate based on the value of a single test 
> > element in x
> > (as in x[3,2]).
> > 
> > In debugging or trouble shooting setting seed is useful. For actual data 
> > analysis you
> > should not set seed, or possibly better yet use set.seed(NULL).
> > 
> > Tim
> > 
> > 
> > 
> > -Original Message-
> > From: Ashim Kapoor 
> > Sent: Thursday, October 13, 2022 12:28 AM
> > To: Ebert,Timothy Aaron 
> > Cc: R Help 
> > Subject: Re: [R] prcomp - arbitrary direction of the returned principal 
> > components
> > 
> > [External Email]
> > 
> > Dear Aaron,
> > 
> > Many thanks for your reply.
> > 
> > Please allow me to illustrate my query a bit.
> > 
> > I take some data, throw it to prcomp and extract the x data frame from 
> > prcomp.
> > 
> > From ?prcomp:
> > 
> >    x: if 'retx' is true the value of the rotated data (the centred
> >   (and scaled if requested) data multiplied by the 'rotation'
> >   matrix) is returned.  Hence, 'cov(x)' is the diagonal matrix
> >   'diag(sdev^2)'.  For the formula method, 'napredict()' is
> >   applied to handle the treatment of values omitted by the
> >   'na.action'.
> > 
> > I consider x[,1] as my index. This makes sense as x[,1] is the projection 
> > of the data
> > on the FIRST principal component.
> > Now this x[,1] can be a high +ve number or a low -ve number. I 

Re: [R] cat in a subroutine

2022-10-13 Thread Bill Dunlap
Do you have another function called "cat" in scope? (with an argument
called "j")?  Before calling cat("...") call print(cat) and
print(find("cat")).

-Bill


On Thu, Oct 13, 2022 at 12:35 AM Steven T. Yen  wrote:

> I have had an issue with printing (with cat) in a subroutine for which I
> do not have a applicable example, but I am still hoping to get some
> help. In the following, the first block of code works fine.
>
> ...
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> But as soon as I insert lines to print (cat) soething simple, it spits
> out message that appears to be nonsence (unrelated). Any idea. Please
> help. Thanks.
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> In this particular case, the error message was as follows:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities",  :
>unused argument (logistic)
>
> I have printed this way in numerous routines without problem and do not
> see why this is happending.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/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] prcomp - arbitrary direction of the returned principal components

2022-10-13 Thread Ashim Kapoor
Dear All,

Many thanks for your replies.

My PC1 loading turns out to be :

1/sqrt(2) , -1/sqrt(2)

In simple words : I had 2 variables and I ran prcomp on them. I got my
PC1 as :  .7071068 var1 - .7071068 var2

PC2 turned out to be the same as PC1 with a PLUS replacing the minus,
ie. .7071068 var1 + .7071068 var2

But forget PC2 for the time being.

Now my question is : I am not able to use the rule that : choose the
variable with a bigger magnitude of loading and multiply PC1 by -1 if
needed (to flip the PC1 since any vector x and it's flipped version -x
 are the same vector but with opposite direction) if the variable with
bigger magnitude is of negative sign.

I have an alternative measure of stress which is trending UP and has 2
peaks during 2 recessions and I can see that PC1 is trending DOWN and
has 2 TROUGHS during the same recessions. That's how I wish to FLIP
PC1 with a negative sign.

The data is not mine and I am not at liberty to share it. I can
construct an artificial example but I would need time to do that.

That's what's happening.

Best Regards and
Many thanks.
Ashim









On Thu, Oct 13, 2022 at 5:38 PM Ebert,Timothy Aaron  wrote:
>
> I still do not understand. However, the general approach would be to identify 
> a specific value to test. If the test is TRUE then do "this" otherwise do 
> nothing. Once the test condition is properly identified, the coding easily 
> follows.
>
>  abs() is the same as
> if x<0 then x = -x   (non-R code, just idea)
> The R code might look something more like
> for (number in 1:ncol(x)){
>if (x[3,2] < 0) {
>  x[number, number] = -x[number, number] #only change the diagonal
>}
> }
>
> Depending on what values need to be changed you may need a nested for loop to 
> go through all values of x[number1, number2].
>
> Your words: " I can forcefully use a NEGATIVE sign to FLIP the index when it 
> is LOW." Where it appeared that "low" was defined as values that are 
> negative. You still will have low values (close to zero) and high values (far 
> from zero).
>
> You could make the condition some other value:
>
> if x< -4 then x = -x
>
> If you just want to rotate about zero then
> x = -x
> In this case the positive values will become negative and the negative values 
> positive.
> Add an if test to selectively rotate based on the value of a single test 
> element in x (as in x[3,2]).
>
> In debugging or trouble shooting setting seed is useful. For actual data 
> analysis you should not set seed, or possibly better yet use set.seed(NULL).
>
> Tim
>
>
>
> -Original Message-
> From: Ashim Kapoor 
> Sent: Thursday, October 13, 2022 12:28 AM
> To: Ebert,Timothy Aaron 
> Cc: R Help 
> Subject: Re: [R] prcomp - arbitrary direction of the returned principal 
> components
>
> [External Email]
>
> Dear Aaron,
>
> Many thanks for your reply.
>
> Please allow me to illustrate my query a bit.
>
> I take some data, throw it to prcomp and extract the x data frame from prcomp.
>
> From ?prcomp:
>
>x: if 'retx' is true the value of the rotated data (the centred
>   (and scaled if requested) data multiplied by the 'rotation'
>   matrix) is returned.  Hence, 'cov(x)' is the diagonal matrix
>   'diag(sdev^2)'.  For the formula method, 'napredict()' is
>   applied to handle the treatment of values omitted by the
>   'na.action'.
>
> I consider x[,1] as my index. This makes sense as x[,1] is the projection of 
> the data on the FIRST principal component.
> Now this x[,1] can be a high +ve number or a low -ve number. I can't ignore 
> the sign.
>
> If I ignore the sign by taking the absolute value, the HIGH / LOW stress 
> values will be indistinguishable.
>
> Hence I do not think using absolute values of x[,1] is the solution.
> Yes it will make the results REPRODUCIBLE but that will be at the cost of 
> losing information.
>
> Any other idea ?
>
> Many thanks,
> Ashim
>
> On Wed, Oct 12, 2022 at 5:23 PM Ebert,Timothy Aaron  wrote:
> >
> > Use absolute value
> >
> > Tim
> >
> > -Original Message-
> > From: R-help  On Behalf Of Ashim Kapoor
> > Sent: Wednesday, October 12, 2022 7:48 AM
> > To: R Help 
> > Subject: [R] prcomp - arbitrary direction of the returned principal
> > components
> >
> > [External Email]
> >
> > Dear R experts,
> >
> > From ?prcomp,
> >
> >  snip -
> > Note:
> >
> >  The signs of the columns of the rotation matrix are arbitrary, and
> >  so may differ between different programs for PCA, and even between
> >  different builds of R.
> >  snip --
> >
> > My problem is that I am building an index based on Principal Components 
> > Analysis.
> > When the index is high it should indicate stress in the market. Due to the 
> > arbitrary sign sometimes I get an index which is HIGH when there is stress 
> > and sometimes I get  the OPPOSITE - an index which is LOW when there is 
> > stress.
> > This program is shared with other people who may have a different 

Re: [R] Problem with Windows clipboard and UTF-8

2022-10-13 Thread Tomas Kalibera

Hi Andrew,

On 9/30/22 15:05, Andrew Hart via R-help wrote:

Hi everyone,

Recently I upgraded to R 4.2.1 which now uses UTF-8 internally as its 
native encoding. Very nice. However, I've discovered that if I use 
writeClipboard to try and move a string containing accented characters 
to the Windows clipboard and then try and paste that into another 
application (e.g. notepad, Eclipse, etc.), the accents turn out all 
garbled. Here's an example:


writeClipboard("categoría")
Pasting the result into this e-mail message yields
Categoría

As near as I can tell, the problem seems to have something to do with 
the format parameter of writeClipboard. By default, format has a value 
of 1, which tells the clipboard to receive Text in the machine's 
locale. If I set format=13 in the call, the accents transfer to the 
clipboard correctly:


writeClipboard("categoría", format=13)
and the result is
Categoría


Ivan Krylov has kindly turned this into a bug report, please see

https://bugs.r-project.org/show_bug.cgi?id=18412

for more details. In short, yes, using format=13 is recommended, but 
please note it has already been documented in ?writeClipboard.


It seems that format=13 may be a better default now that R is using 
UTF-8. It would be nice not to have to specify the format every time I 
want to copy text to the clipboard with writeClipboard.


Yes, I agree, I've changed the default to format=13.

Is writeClipboard supposed to perform any kind of encoding conversion 
or is the format parameter merely informing the clipboard of the kind 
of payload it's being handed?


Btw, with pre-4.2.0 versions of R, this wasn't a problem. I am very 
much in favour of R using some kind of Unicode encoding natively, but 
this wrinkle seems to be something the user shouldn't have to deal 
with since the Windows clipboard is capable of holding Unicode text. 
Any advice would be gratefully received.


This is a bit complicated and more can be found in the bug report 
response. In short, the clipboard is capable of holding either "text" 
(then with locale information) or "Unicode text". One can ask Windows 
for either content and Windows will do the conversion, it would convert 
from "text" to "Unicode text" using that locale. If that locale is not 
filled in explicitly, it is the current input language (so the 
"keyboard" the user has selected at the time of the copying to 
clipboard, e.g. of writeClipboard). If that locale encoding doesn't 
match the R current native encoding, and you are using "text", 
characters may be mis-represented. This could have happened even before 
R 4.2.0, but is more likely from R 4.2.0 when it uses UTF-8. Going via 
"Unicode text" resolves the issue as the conversion to/from UTF-16LE is 
done by readClipboard/writeClipboard using the R current native encoding.


Users who don't want to deal with these complexities can use the 
higher-level connections interface (?connections, "clipboard").


Best
Tomas



Thanks,
Andrew.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] replicate results of tree package

2022-10-13 Thread Naresh Gurbuxani


I am trying to understand ``deviance'' in classification tree output
from tree package.

library(tree)

set.seed(911)
mydf <- data.frame(
name = as.factor(rep(c("A", "B"), c(10, 10))),
x = c(rnorm(10, -1), rnorm(10, 1)),
y = c(rnorm(10, 1), rnorm(10, -1)))

mytree <- tree(name ~ ., data = mydf)

mytree
# node), split, n, deviance, yval, (yprob)
#   * denotes terminal node

# 1) root 20 27.730 A ( 0.5 0.5 )  
#   2) y < -0.00467067 10  6.502 B ( 0.1 0.9 )  
# 4) x < 1.50596 5  5.004 B ( 0.2 0.8 ) *
# 5) x > 1.50596 5  0.000 B ( 0.0 1.0 ) *
#   3) y > -0.00467067 10  6.502 A ( 0.9 0.1 )  
# 6) x < -0.578851 5  0.000 A ( 1.0 0.0 ) *
# 7) x > -0.578851 5  5.004 A ( 0.8 0.2 ) *

# Replicate results for node 2
# Probabilities tie out
with(subset(mydf, y < -0.00457), table(name))
# name
# A B 
# 1 9

# Cannot replicate deviance = -1 * sum(p_mk * log(p_mk))
0.1 * log(0.1) + 0.9 * log(0.9)
# [1] 0.325083

1.  In the documentation, is it possible to find the definition of
deviance?
2.  Is it possible to see the code where it calculates deviance?

Thanks,
Naresh

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] prcomp - arbitrary direction of the returned principal components

2022-10-13 Thread Ebert,Timothy Aaron
I still do not understand. However, the general approach would be to identify a 
specific value to test. If the test is TRUE then do "this" otherwise do 
nothing. Once the test condition is properly identified, the coding easily 
follows.

 abs() is the same as
if x<0 then x = -x   (non-R code, just idea)
The R code might look something more like
for (number in 1:ncol(x)){
   if (x[3,2] < 0) {
 x[number, number] = -x[number, number] #only change the diagonal
   }
}

Depending on what values need to be changed you may need a nested for loop to 
go through all values of x[number1, number2]. 

Your words: " I can forcefully use a NEGATIVE sign to FLIP the index when it is 
LOW." Where it appeared that "low" was defined as values that are negative. You 
still will have low values (close to zero) and high values (far from zero). 

You could make the condition some other value:

if x< -4 then x = -x

If you just want to rotate about zero then 
x = -x
In this case the positive values will become negative and the negative values 
positive.
Add an if test to selectively rotate based on the value of a single test 
element in x (as in x[3,2]). 

In debugging or trouble shooting setting seed is useful. For actual data 
analysis you should not set seed, or possibly better yet use set.seed(NULL).

Tim



-Original Message-
From: Ashim Kapoor  
Sent: Thursday, October 13, 2022 12:28 AM
To: Ebert,Timothy Aaron 
Cc: R Help 
Subject: Re: [R] prcomp - arbitrary direction of the returned principal 
components

[External Email]

Dear Aaron,

Many thanks for your reply.

Please allow me to illustrate my query a bit.

I take some data, throw it to prcomp and extract the x data frame from prcomp.

>From ?prcomp:

   x: if 'retx' is true the value of the rotated data (the centred
  (and scaled if requested) data multiplied by the 'rotation'
  matrix) is returned.  Hence, 'cov(x)' is the diagonal matrix
  'diag(sdev^2)'.  For the formula method, 'napredict()' is
  applied to handle the treatment of values omitted by the
  'na.action'.

I consider x[,1] as my index. This makes sense as x[,1] is the projection of 
the data on the FIRST principal component.
Now this x[,1] can be a high +ve number or a low -ve number. I can't ignore the 
sign.

If I ignore the sign by taking the absolute value, the HIGH / LOW stress values 
will be indistinguishable.

Hence I do not think using absolute values of x[,1] is the solution.
Yes it will make the results REPRODUCIBLE but that will be at the cost of 
losing information.

Any other idea ?

Many thanks,
Ashim

On Wed, Oct 12, 2022 at 5:23 PM Ebert,Timothy Aaron  wrote:
>
> Use absolute value
>
> Tim
>
> -Original Message-
> From: R-help  On Behalf Of Ashim Kapoor
> Sent: Wednesday, October 12, 2022 7:48 AM
> To: R Help 
> Subject: [R] prcomp - arbitrary direction of the returned principal 
> components
>
> [External Email]
>
> Dear R experts,
>
> From ?prcomp,
>
>  snip -
> Note:
>
>  The signs of the columns of the rotation matrix are arbitrary, and
>  so may differ between different programs for PCA, and even between
>  different builds of R.
>  snip --
>
> My problem is that I am building an index based on Principal Components 
> Analysis.
> When the index is high it should indicate stress in the market. Due to the 
> arbitrary sign sometimes I get an index which is HIGH when there is stress 
> and sometimes I get  the OPPOSITE - an index which is LOW when there is 
> stress.
> This program is shared with other people who may have a different build of R.
>
> I can forcefully use a NEGATIVE sign to FLIP the index when it is LOW.
> That works.
>
> Now my query is : Just like we do set.seed(1234) and force the pattern of 
> generation of random number and make it REPRODUCIBLE, can I do something like 
> :
>
> set.direction.for.vector.in.pca(1234)
>
> Now each time I do prcomp it should choose the SAME ( high or low ) direction 
> of the principle component on ANY computer having ANY version of R installed.
>
> That's what I want. I don't want the the returned principal component to be 
> HIGH(LOW) on my computer and LOW(HIGH) on someone else's computer.
> That would confuse the people the code is shared with.
>
> Is this possible ? How do people deal with this ?
>
> Many thanks,
> Ashim
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-helpdata=05%7C01%7Ctebert%40ufl
> .edu%7C60e6d6ae8645462db99b08daacd36b76%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C638012321302591064%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> sdata=AHMEDU%2BTyInvW%2FH6EZQteO1qZ%2BtW3JZfybfaveTD8Yk%3Dre
> served=0 PLEASE do read the posting guide 
> 

Re: [R] prcomp - arbitrary direction of the returned principal components

2022-10-13 Thread Ivan Krylov
В Wed, 12 Oct 2022 17:18:26 +0530
Ashim Kapoor  пишет:

> My problem is that I am building an index based on Principal
> Components Analysis.
> When the index is high it should indicate stress in the market.

Have you considered using supervised methods, like PLS, to predict
stress in the market?

Imagine what happens when you take the points where there's stress in
the market and feed only those to PCA. The first principal direction is
now gone (if there is a variation along this axis, it's much smaller
than it was), so now some other direction occupies its place. Even if
the first direction is preserved, after centering, there are now points
with low values of PC1, despite all points should correspond to stress
in the market.

Apologies if the paragraph above is complete nonsense, a reasonable
researched would always conduct the analysis on a representative sample
of the points, and the whole point of the proposed index is that high
stress is indicated by points on the positive end of the multivariate
sausage that PCA considers the data to be. If that's the case,
post-processing the signs as described by Chris Evans could be the
right solution.

-- 
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] cat in a subroutine

2022-10-13 Thread Ivan Krylov
В Thu, 13 Oct 2022 15:49:57 +0800
"Steven T. Yen"  пишет:

> No. Removing the second line (so that cat simply prints something
> else),
> 
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
> Probabilities")
> #    "\n\nlogistic =",logistic)
> 
> I get yet another nonsense:
> 
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / 
> Probit Probabilities") :
>    argument "j" is missing, with no default

I don't see any problems with quoting or parentheses in the parts of
the code you're showing us, but they may be present elsewhere.

If you step the function in debugger (see ?debugonce and ?browser),
does it crash in the call to cat()?

If you take your function apart by applying as.list(body(fun)) (and
furthermore applying as.list() to any parts of the function), do you
see the call to cat() taking exactly the arguments you mean it to take?

-- 
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] cat in a subroutine

2022-10-13 Thread Steven Yen
No. I just run the subroutine containing those line, with result going to 
destination go probit1.r.me.it and printed it.

Steven from iPhone

> On Oct 13, 2022, at 5:09 PM, Jim Lemon  wrote:
> 
> Have you assigned the valuie of goprobit1.r.me.kr to "fortytwo", or
> indeed anything?
> 
> Jim
> 
>> On Thu, Oct 13, 2022 at 8:02 PM Steven T. Yen  wrote:
>> 
>> Not really.
>> 
>> fortytwo<-42
>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
>> Probabilities",
>> "logisitic =",fortytwo,"\n")
>> 
>>> goprobit1.r.me.kr<-me.gologit.r(goprobit1,embellished=TRUE,
>> + resampling=TRUE,ndraws=5);
>> goprobit1.r.me.kr
>> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
>> Probit Probabilities",  :
>>   unused arguments (fortytwo, "\n")
>> 
>>> On 10/13/2022 4:46 PM, Jim Lemon wrote:
>>> Hi Steven & Erin,
>>> This works:
>>> 
>>> fortytwo<-42
>>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
>>> Probabilities","logisitic =",fortytwo,"\n")
>>> j<-grep(".one\\b",c(".one\\a",".one\\b"))
>>> 
>>> Marginal and Discrete Effects of Gen Ordered Logit / Probit
>>> Probabilities logisitic = 42
>>> 
>>> If I don't define fortytwo before calling cat, it doesn't.
>>> So we know what the answer is.
>>> 
>>> Jim
>>> 
>>> On Thu, Oct 13, 2022 at 7:36 PM Steven Yen  wrote:
 t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))

[[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] cat in a subroutine

2022-10-13 Thread Jim Lemon
Have you assigned the valuie of goprobit1.r.me.kr to "fortytwo", or
indeed anything?

Jim

On Thu, Oct 13, 2022 at 8:02 PM Steven T. Yen  wrote:
>
> Not really.
>
> fortytwo<-42
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "logisitic =",fortytwo,"\n")
>
>  > goprobit1.r.me.kr<-me.gologit.r(goprobit1,embellished=TRUE,
> + resampling=TRUE,ndraws=5);
> goprobit1.r.me.kr
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities",  :
>unused arguments (fortytwo, "\n")
>
> On 10/13/2022 4:46 PM, Jim Lemon wrote:
> > Hi Steven & Erin,
> > This works:
> >
> > fortytwo<-42
> > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> > Probabilities","logisitic =",fortytwo,"\n")
> > j<-grep(".one\\b",c(".one\\a",".one\\b"))
> >
> > Marginal and Discrete Effects of Gen Ordered Logit / Probit
> > Probabilities logisitic = 42
> >
> > If I don't define fortytwo before calling cat, it doesn't.
> > So we know what the answer is.
> >
> > Jim
> >
> > On Thu, Oct 13, 2022 at 7:36 PM Steven Yen  wrote:
> >> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> >> sig<-my.sig.levels(p)
> >> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> >> rownames(out)<-names(me)
> >> colnames(out)<-c("est","se","t","p","sig")
> >> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> >> Probabilities",
> >>   "\n\nlogistic =",logistic)
> >> j<-grep(".one\\b",rownames(out))
> >> out<-out[-j,]
> >> return(out)
> >> }
> >>
> >> Steven from iPhone
> >>
> >>> On Oct 13, 2022, at 3:37 PM, Erin Hodgess  wrote:
> >>>
> >>> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> >>> sig<-my.sig.levels(p)
> >>> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> >>> rownames(out)<-names(me)
> >>> colnames(out)<-c("est","se","t","p","sig")
> >>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> >>> Probabilities",
> >>>   "\n\nlogistic =",logistic)
> >>> j<-grep(".one\\b",rownames(out))
> >>> out<-out[-j,]
> >>> return(out)
> >>> }
> >>  [[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] cat in a subroutine

2022-10-13 Thread Steven T. Yen

Not really.

fortytwo<-42
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
Probabilities",

    "logisitic =",fortytwo,"\n")

> goprobit1.r.me.kr<-me.gologit.r(goprobit1,embellished=TRUE,
+ resampling=TRUE,ndraws=5); 
goprobit1.r.me.kr
Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / 
Probit Probabilities",  :

  unused arguments (fortytwo, "\n")

On 10/13/2022 4:46 PM, Jim Lemon wrote:

Hi Steven & Erin,
This works:

fortytwo<-42
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities","logisitic =",fortytwo,"\n")
j<-grep(".one\\b",c(".one\\a",".one\\b"))

Marginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities logisitic = 42

If I don't define fortytwo before calling cat, it doesn't.
So we know what the answer is.

Jim

On Thu, Oct 13, 2022 at 7:36 PM Steven Yen  wrote:

t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
sig<-my.sig.levels(p)
out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
rownames(out)<-names(me)
colnames(out)<-c("est","se","t","p","sig")
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities",
  "\n\nlogistic =",logistic)
j<-grep(".one\\b",rownames(out))
out<-out[-j,]
return(out)
}

Steven from iPhone


On Oct 13, 2022, at 3:37 PM, Erin Hodgess  wrote:

t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
sig<-my.sig.levels(p)
out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
rownames(out)<-names(me)
colnames(out)<-c("est","se","t","p","sig")
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities",
  "\n\nlogistic =",logistic)
j<-grep(".one\\b",rownames(out))
out<-out[-j,]
return(out)
}

 [[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] cat in a subroutine

2022-10-13 Thread Jim Lemon
Hi Steven & Erin,
This works:

fortytwo<-42
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities","logisitic =",fortytwo,"\n")
j<-grep(".one\\b",c(".one\\a",".one\\b"))

Marginal and Discrete Effects of Gen Ordered Logit / Probit
Probabilities logisitic = 42

If I don't define fortytwo before calling cat, it doesn't.
So we know what the answer is.

Jim

On Thu, Oct 13, 2022 at 7:36 PM Steven Yen  wrote:
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> Steven from iPhone
>
> > On Oct 13, 2022, at 3:37 PM, Erin Hodgess  wrote:
> >
> > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> > sig<-my.sig.levels(p)
> > out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> > rownames(out)<-names(me)
> > colnames(out)<-c("est","se","t","p","sig")
> > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> > Probabilities",
> >  "\n\nlogistic =",logistic)
> > j<-grep(".one\\b",rownames(out))
> > out<-out[-j,]
> > return(out)
> > }
>
> [[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] cat in a subroutine

2022-10-13 Thread Steven Yen
t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
sig<-my.sig.levels(p)
out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
rownames(out)<-names(me)
colnames(out)<-c("est","se","t","p","sig")
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
Probabilities",
 "\n\nlogistic =",logistic)
j<-grep(".one\\b",rownames(out))
out<-out[-j,]
return(out)
}

Steven from iPhone

> On Oct 13, 2022, at 3:37 PM, Erin Hodgess  wrote:
> 
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }

[[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] cat in a subroutine

2022-10-13 Thread Erin Hodgess
Steven, would you mind putting that section of code in again, with the cat
statement, please?

I have an idea...
Erin Hodgess, PhD
mailto: erinm.hodg...@gmail.com


On Thu, Oct 13, 2022 at 2:13 AM Jim Lemon  wrote:

> Hi Steven,
> I think Erin is right, unless there is a variable named "logistic"
> visible within the function, "cat" will think it is either an argument
> or an R object. If you have the "psych" package loaded, it may see it
> as a closure. There is also a "Logistic" in the stats package, but
> that shouldn't be visible due to the capital L
>
> Jim
>
> On Thu, Oct 13, 2022 at 6:55 PM Steven T. Yen  wrote:
> >
> > Thanks Erin.
> >
> > No. Removing the second line (so that cat simply prints something else),
> >
> > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> > Probabilities")
> > #"\n\nlogistic =",logistic)
> >
> > I get yet another nonsense:
> >
> > Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> > Probit Probabilities") :
> >argument "j" is missing, with no default
> >  >
> >
> > On 10/13/2022 3:37 PM, Erin Hodgess wrote:
> > > Hi Steven:
> > >
> > > Do you have a variable called logistic, please?  I think that might be
> > > the culprit.
> > >
> > > Thanks,
> > > Erin
> > >
> > > On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen 
> wrote:
> > >
> > > I have had an issue with printing (with cat) in a subroutine for
> > > which I
> > > do not have a applicable example, but I am still hoping to get some
> > > help. In the following, the first block of code works fine.
> > >
> > > ...
> > >
> > > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> > > sig<-my.sig.levels(p)
> > > out<-data.frame(round(cbind(me,se,t,p),digits));
> out<-cbind(out,sig)
> > > rownames(out)<-names(me)
> > > colnames(out)<-c("est","se","t","p","sig")
> > > j<-grep(".one\\b",rownames(out))
> > > out<-out[-j,]
> > > return(out)
> > > }
> > >
> > > But as soon as I insert lines to print (cat) soething simple, it
> > > spits
> > > out message that appears to be nonsence (unrelated). Any idea.
> Please
> > > help. Thanks.
> > >
> > > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> > > sig<-my.sig.levels(p)
> > > out<-data.frame(round(cbind(me,se,t,p),digits));
> out<-cbind(out,sig)
> > > rownames(out)<-names(me)
> > > colnames(out)<-c("est","se","t","p","sig")
> > > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> > > Probabilities",
> > >  "\n\nlogistic =",logistic)
> > > j<-grep(".one\\b",rownames(out))
> > > out<-out[-j,]
> > > return(out)
> > > }
> > >
> > > In this particular case, the error message was as follows:
> > >
> > > Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit
> /
> > > Probit Probabilities",  :
> > >unused argument (logistic)
> > >
> > > I have printed this way in numerous routines without problem and
> > > do not
> > > see why this is happending.
> > >
> > > __
> > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> > > http://www.R-project.org/posting-guide.html
> > > 
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > > --
> > > Erin Hodgess, PhD
> > > mailto: erinm.hodg...@gmail.com
> > [[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] cat in a subroutine

2022-10-13 Thread Steven Yen
Yes, these lines are part of a subroutine calling yet more procedures. What 
frustrated me was, I am not doing anything different from other routines. My 
cat command in the recent code does not do anything other than printing a line 
of plain text. I am clueless.

Steven from iPhone

> On Oct 13, 2022, at 3:54 PM, Erin Hodgess  wrote:
> 
> 
> All right.  Are these lines of code part of a larger function, please?  Is 
> that function possibly calling a loop, please?
> 
> Thanks,
> Erin 
> 
>> On Thu, Oct 13, 2022 at 1:49 AM Steven T. Yen  wrote:
>> Thanks Erin. 
>> 
>> No. Removing the second line (so that cat simply prints something else),
>> 
>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
>> Probabilities")
>> #"\n\nlogistic =",logistic)
>> 
>> I get yet another nonsense:
>> 
>> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
>> Probabilities") : 
>>   argument "j" is missing, with no default
>> > 
>> On 10/13/2022 3:37 PM, Erin Hodgess wrote:
>>> Hi Steven:
>>> 
>>> Do you have a variable called logistic, please?  I think that might be the 
>>> culprit.
>>> 
>>> Thanks,
>>> Erin 
>>> 
>>> On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen  wrote:
 I have had an issue with printing (with cat) in a subroutine for which I 
 do not have a applicable example, but I am still hoping to get some 
 help. In the following, the first block of code works fine.
 
 ...
 
 t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
 sig<-my.sig.levels(p)
 out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
 rownames(out)<-names(me)
 colnames(out)<-c("est","se","t","p","sig")
 j<-grep(".one\\b",rownames(out))
 out<-out[-j,]
 return(out)
 }
 
 But as soon as I insert lines to print (cat) soething simple, it spits 
 out message that appears to be nonsence (unrelated). Any idea. Please 
 help. Thanks.
 
 t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
 sig<-my.sig.levels(p)
 out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
 rownames(out)<-names(me)
 colnames(out)<-c("est","se","t","p","sig")
 cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
 Probabilities",
  "\n\nlogistic =",logistic)
 j<-grep(".one\\b",rownames(out))
 out<-out[-j,]
 return(out)
 }
 
 In this particular case, the error message was as follows:
 
 Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / 
 Probit Probabilities",  :
unused argument (logistic)
 
 I have printed this way in numerous routines without problem and do not 
 see why this is happending.
 
 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide 
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.
>>> -- 
>>> Erin Hodgess, PhD
>>> mailto: erinm.hodg...@gmail.com
> -- 
> Erin Hodgess, PhD
> mailto: erinm.hodg...@gmail.com

[[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] cat in a subroutine

2022-10-13 Thread Jim Lemon
Hi Steven,
I think Erin is right, unless there is a variable named "logistic"
visible within the function, "cat" will think it is either an argument
or an R object. If you have the "psych" package loaded, it may see it
as a closure. There is also a "Logistic" in the stats package, but
that shouldn't be visible due to the capital L

Jim

On Thu, Oct 13, 2022 at 6:55 PM Steven T. Yen  wrote:
>
> Thanks Erin.
>
> No. Removing the second line (so that cat simply prints something else),
>
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities")
> #"\n\nlogistic =",logistic)
>
> I get yet another nonsense:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities") :
>argument "j" is missing, with no default
>  >
>
> On 10/13/2022 3:37 PM, Erin Hodgess wrote:
> > Hi Steven:
> >
> > Do you have a variable called logistic, please?  I think that might be
> > the culprit.
> >
> > Thanks,
> > Erin
> >
> > On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen  wrote:
> >
> > I have had an issue with printing (with cat) in a subroutine for
> > which I
> > do not have a applicable example, but I am still hoping to get some
> > help. In the following, the first block of code works fine.
> >
> > ...
> >
> > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> > sig<-my.sig.levels(p)
> > out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> > rownames(out)<-names(me)
> > colnames(out)<-c("est","se","t","p","sig")
> > j<-grep(".one\\b",rownames(out))
> > out<-out[-j,]
> > return(out)
> > }
> >
> > But as soon as I insert lines to print (cat) soething simple, it
> > spits
> > out message that appears to be nonsence (unrelated). Any idea. Please
> > help. Thanks.
> >
> > t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> > sig<-my.sig.levels(p)
> > out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> > rownames(out)<-names(me)
> > colnames(out)<-c("est","se","t","p","sig")
> > cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> > Probabilities",
> >  "\n\nlogistic =",logistic)
> > j<-grep(".one\\b",rownames(out))
> > out<-out[-j,]
> > return(out)
> > }
> >
> > In this particular case, the error message was as follows:
> >
> > Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> > Probit Probabilities",  :
> >unused argument (logistic)
> >
> > I have printed this way in numerous routines without problem and
> > do not
> > see why this is happending.
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > 
> > and provide commented, minimal, self-contained, reproducible code.
> >
> > --
> > Erin Hodgess, PhD
> > mailto: erinm.hodg...@gmail.com
> [[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] cat in a subroutine

2022-10-13 Thread Steven T. Yen
Thanks Erin.

No. Removing the second line (so that cat simply prints something else),

cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
Probabilities")
#    "\n\nlogistic =",logistic)

I get yet another nonsense:

Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / 
Probit Probabilities") :
   argument "j" is missing, with no default
 >

On 10/13/2022 3:37 PM, Erin Hodgess wrote:
> Hi Steven:
>
> Do you have a variable called logistic, please?  I think that might be 
> the culprit.
>
> Thanks,
> Erin
>
> On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen  wrote:
>
> I have had an issue with printing (with cat) in a subroutine for
> which I
> do not have a applicable example, but I am still hoping to get some
> help. In the following, the first block of code works fine.
>
> ...
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> But as soon as I insert lines to print (cat) soething simple, it
> spits
> out message that appears to be nonsence (unrelated). Any idea. Please
> help. Thanks.
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> In this particular case, the error message was as follows:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities",  :
>    unused argument (logistic)
>
> I have printed this way in numerous routines without problem and
> do not
> see why this is happending.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> 
> and provide commented, minimal, self-contained, reproducible code.
>
> -- 
> Erin Hodgess, PhD
> mailto: erinm.hodg...@gmail.com
[[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] cat in a subroutine

2022-10-13 Thread Erin Hodgess
All right.  Are these lines of code part of a larger function, please?  Is
that function possibly calling a loop, please?

Thanks,
Erin

On Thu, Oct 13, 2022 at 1:49 AM Steven T. Yen  wrote:

> Thanks Erin.
>
> No. Removing the second line (so that cat simply prints something else),
>
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities")
> #"\n\nlogistic =",logistic)
>
> I get yet another nonsense:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities") :
>   argument "j" is missing, with no default
> >
> On 10/13/2022 3:37 PM, Erin Hodgess wrote:
>
> Hi Steven:
>
> Do you have a variable called logistic, please?  I think that might be the
> culprit.
>
> Thanks,
> Erin
>
> On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen  wrote:
>
>> I have had an issue with printing (with cat) in a subroutine for which I
>> do not have a applicable example, but I am still hoping to get some
>> help. In the following, the first block of code works fine.
>>
>> ...
>>
>> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
>> sig<-my.sig.levels(p)
>> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
>> rownames(out)<-names(me)
>> colnames(out)<-c("est","se","t","p","sig")
>> j<-grep(".one\\b",rownames(out))
>> out<-out[-j,]
>> return(out)
>> }
>>
>> But as soon as I insert lines to print (cat) soething simple, it spits
>> out message that appears to be nonsence (unrelated). Any idea. Please
>> help. Thanks.
>>
>> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
>> sig<-my.sig.levels(p)
>> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
>> rownames(out)<-names(me)
>> colnames(out)<-c("est","se","t","p","sig")
>> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
>> Probabilities",
>>  "\n\nlogistic =",logistic)
>> j<-grep(".one\\b",rownames(out))
>> out<-out[-j,]
>> return(out)
>> }
>>
>> In this particular case, the error message was as follows:
>>
>> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
>> Probit Probabilities",  :
>>unused argument (logistic)
>>
>> I have printed this way in numerous routines without problem and do not
>> see why this is happending.
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> --
> Erin Hodgess, PhD
> mailto: erinm.hodg...@gmail.com
>
> --
Erin Hodgess, PhD
mailto: erinm.hodg...@gmail.com

[[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] cat in a subroutine

2022-10-13 Thread Erin Hodgess
Hi Steven:

Do you have a variable called logistic, please?  I think that might be the
culprit.

Thanks,
Erin

On Thu, Oct 13, 2022 at 1:35 AM Steven T. Yen  wrote:

> I have had an issue with printing (with cat) in a subroutine for which I
> do not have a applicable example, but I am still hoping to get some
> help. In the following, the first block of code works fine.
>
> ...
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> But as soon as I insert lines to print (cat) soething simple, it spits
> out message that appears to be nonsence (unrelated). Any idea. Please
> help. Thanks.
>
> t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
> sig<-my.sig.levels(p)
> out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
> rownames(out)<-names(me)
> colnames(out)<-c("est","se","t","p","sig")
> cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit
> Probabilities",
>  "\n\nlogistic =",logistic)
> j<-grep(".one\\b",rownames(out))
> out<-out[-j,]
> return(out)
> }
>
> In this particular case, the error message was as follows:
>
> Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit /
> Probit Probabilities",  :
>unused argument (logistic)
>
> I have printed this way in numerous routines without problem and do not
> see why this is happending.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
-- 
Erin Hodgess, PhD
mailto: erinm.hodg...@gmail.com

[[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] cat in a subroutine

2022-10-13 Thread Steven T. Yen
I have had an issue with printing (with cat) in a subroutine for which I 
do not have a applicable example, but I am still hoping to get some 
help. In the following, the first block of code works fine.


...

t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
sig<-my.sig.levels(p)
out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
rownames(out)<-names(me)
colnames(out)<-c("est","se","t","p","sig")
j<-grep(".one\\b",rownames(out))
out<-out[-j,]
return(out)
}

But as soon as I insert lines to print (cat) soething simple, it spits 
out message that appears to be nonsence (unrelated). Any idea. Please 
help. Thanks.


t<-abs(me)/se; p<-2*(1-pt(t,nrow(x)))
sig<-my.sig.levels(p)
out<-data.frame(round(cbind(me,se,t,p),digits)); out<-cbind(out,sig)
rownames(out)<-names(me)
colnames(out)<-c("est","se","t","p","sig")
cat("\nMarginal and Discrete Effects of Gen Ordered Logit / Probit 
Probabilities",

    "\n\nlogistic =",logistic)
j<-grep(".one\\b",rownames(out))
out<-out[-j,]
return(out)
}

In this particular case, the error message was as follows:

Error in cat("\nMarginal and Discrete Effects of Gen Ordered Logit / 
Probit Probabilities",  :

  unused argument (logistic)

I have printed this way in numerous routines without problem and do not 
see why this is happending.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/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] prcomp - arbitrary direction of the returned principal components

2022-10-13 Thread Chris Evans
I agree with this and I'm not very sure why you feel you need the signs
fixed one way: they are arbitrary and how they come out is generally a
function of things in the handling of rounding as values hit the limit
of the finite arithmetic in the particular program and OS and hardware
on which it's running.  Can't you just say that?!

If you must have it aligned one way then I think the only thing you can
do is to select the row/item/variable with the highest absolute loading
on the component and set that to be positive (say, whether you choose
positive or negative is up to you).  I think this does it:

### create some data to analyse
set.seed(12345)
n <- 500 # number of observations
k <- 8 # number of variables
fuzz <- .1 # used to add noise
varLatent <- rnorm(n) # create the values of the dominant PC
vecLoadings <- c(rep(1, k/2), rep(-1, k/2)) # binary loadings 

### make the raw data
matData <- matrix(rep(NA, n * k), ncol = k)
for (i in 1:k) {
  matData[, i] <- vecLoadings[i] * varLatent + rnorm(n, fuzz)
}
head(matData)

### get the PCA
matPrcomp <- prcomp(matData)$rotation
round(matPrcomp, 2)
# PC1   PC2   PC3   PC4   PC5   PC6   PC7   PC8
# [1,]  0.34  0.00 -0.74  0.32 -0.38 -0.25  0.15  0.00
# [2,]  0.37 -0.19  0.49  0.33 -0.57  0.38  0.07 -0.01
# [3,]  0.34  0.80  0.14  0.11  0.10 -0.05 -0.16 -0.43
# [4,]  0.33 -0.47 -0.16  0.10  0.34  0.17 -0.61 -0.35
# [5,] -0.35  0.15 -0.15 -0.34 -0.61  0.10 -0.57 -0.08
# [6,] -0.37 -0.25  0.11  0.13 -0.14 -0.30  0.27 -0.77
# [7,] -0.35  0.07  0.17  0.72  0.04 -0.32 -0.38  0.28
# [8,] -0.38  0.17 -0.32  0.33  0.14  0.75  0.16 -0.12

### find the sign of the maximum absolute loading for each component
vecMaxItemSigns <- apply(matPrcomp, 2,
function(x){sign(x[(which.max(abs(x)))])})
vecMaxItemSigns
# PC1 PC2 PC3 PC4 PC5 PC6 PC7 PC8 
# -1   1  -1   1  -1   1  -1  -1

### now use that to create a new PCA where the strongest loadings are
always positive
newMatPrcomp <- matrix(rep(NA, k * k), ncol = k)
colnames(newMatPrcomp) <- colnames(matPrcomp)
rownames(newMatPrcomp) <- rownames(matPrcomp)
for (i in 1:k) {
  newMatPrcomp[, i] <- matPrcomp[, i] * vecMaxItems[i]
}
round(newMatPrcomp, 2)
# PC1   PC2   PC3   PC4   PC5   PC6   PC7   PC8
# [1,] -0.34  0.00  0.74  0.32  0.38 -0.25 -0.15  0.00
# [2,] -0.37 -0.19 -0.49  0.33  0.57  0.38 -0.07  0.01
# [3,] -0.34  0.80 -0.14  0.11 -0.10 -0.05  0.16  0.43
# [4,] -0.33 -0.47  0.16  0.10 -0.34  0.17  0.61  0.35
# [5,]  0.35  0.15  0.15 -0.34  0.61  0.10  0.57  0.08
# [6,]  0.37 -0.25 -0.11  0.13  0.14 -0.30 -0.27  0.77
# [7,]  0.35  0.07 -0.17  0.72 -0.04 -0.32  0.38 -0.28
# [8,]  0.38  0.17  0.32  0.33 -0.14  0.75 -0.16  0.12


Apologies for the coding: I'm a better therapist than coder and
it's a while since I've done much in base R like this.  Quite fun
to get back to it!  R artistes can probably do that in four lines!

But I'm not convinced doing this to "fix" the signs is really 
worth it however many lines one uses to code it!!

Very best all,

Chris

 On Thu, 2022-10-13 at 09:58 +0530, Ashim Kapoor wrote:
> Dear Aaron,
> 
> Many thanks for your reply.
> 
> Please allow me to illustrate my query a bit.
> 
> I take some data, throw it to prcomp and extract the x data frame
> from prcomp.
> 
> From ?prcomp:
> 
>    x: if ‘retx’ is true the value of the rotated data (the
> centred
>   (and scaled if requested) data multiplied by the ‘rotation’
>   matrix) is returned.  Hence, ‘cov(x)’ is the diagonal
> matrix
>   ‘diag(sdev^2)’.  For the formula method, ‘napredict()’ is
>   applied to handle the treatment of values omitted by the
>   ‘na.action’.
> 
> I consider x[,1] as my index. This makes sense as x[,1] is the
> projection of the data on the FIRST principal component.
> Now this x[,1] can be a high +ve number or a low -ve number. I can't
> ignore the sign.
> 
> If I ignore the sign by taking the absolute value, the HIGH / LOW
> stress values will be indistinguishable.
> 
> Hence I do not think using absolute values of x[,1] is the solution.
> Yes it will make the results REPRODUCIBLE but that will be at the
> cost
> of losing information.
> 
> Any other idea ?
> 
> Many thanks,
> Ashim
> 
> On Wed, Oct 12, 2022 at 5:23 PM Ebert,Timothy Aaron 
> wrote:
> > 
> > Use absolute value
> > 
> > Tim
> > 
> > -Original Message-
> > From: R-help  On Behalf Of Ashim
> > Kapoor
> > Sent: Wednesday, October 12, 2022 7:48 AM
> > To: R Help 
> > Subject: [R] prcomp - arbitrary direction of the returned principal
> > components
> > 
> > [External Email]
> > 
> > Dear R experts,
> > 
> > From ?prcomp,
> > 
> >  snip -
> > Note:
> > 
> >  The signs of the columns of the rotation matrix are arbitrary,
> > and
> >  so may differ between different programs for PCA, and even
> > between
> >  different builds of R.
> >  snip --
> > 
> > My problem is that I am building an index based on Principal
> > Components Analysis.
> > When the index is high it should indicate