Re: [R] legend for the plot with type = "b"

2013-09-22 Thread David Winsemius


On Sep 22, 2013, at 10:54 PM, Jinsong Zhao wrote:


Hi there,

I plot a simple plot with the following code:

plot (rnorm(1:10), type = "b")
legend("top", "test", lty = 1, pch = 21)


?par
plot (rnorm(1:10), type = "b")
legend("top", "test", lty = "69", pch = 21)



The result is something wired for the line crosses the point in the  
legend while the line does not cross the point in the main plot.


Is there possibility to draw the legend that line does not cross the  
point, i.e., like the pattern in the main plot?


Any help is really appreciated.


--

David Winsemius, MD
Alameda, CA, USA

__
R-help@r-project.org 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.


Re: [R] basic matrix function

2013-09-22 Thread arun
Hi,
Use `drop=FALSE`.
 b<- matrix(c(2,1,-1,-2),ncol=1)
 b[1:3,1]
#[1]  2  1 -1
 b[1:3,1,drop=FALSE]
#or
b[1:3,,drop=FALSE]
# [,1]
#[1,]    2
#[2,]    1
#[3,]   -1


A.K.



hi all, 

i got a small question tonight. 
> matrix(b,4)[] 
     [,1] 
[1,]    2 
[2,]    1 
[3,]   -1 
[4,]   -2 
> dim(matrix(betan,4)) 
[1] 4 1 
As shown, b is a 4X1 matrix. 

> matrix(betan,4)[1:3,1] 
[1]  2  1 -1 

However, I think the result should be 
     [,1] 
[1,]    2 
[2,]    1 
[3,]   -1 

How could I get the result above? 
Many thanks,

__
R-help@r-project.org 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.


Re: [R] legend for the plot with type = "b"

2013-09-22 Thread arun
Hi,
May be this helps.


set.seed(55)
 x<-rnorm(1:10)
 plot(x,type="n",xaxt="n",yaxt="n")
 legend1<- legend("top","test",lty=1,pch=21)
range1<- range(x)
 range1[2]<- 1.05* (range1[2]+ legend1$rect$h)
 plot(x,ylim=range1,type="b")
 legend1<- legend("top","test",lty=1,pch=21)

A.K.



- Original Message -
From: Jinsong Zhao 
To: R help 
Cc: 
Sent: Sunday, September 22, 2013 11:54 PM
Subject: [R] legend for the plot with type = "b"

Hi there,

I plot a simple plot with the following code:

plot (rnorm(1:10), type = "b")
legend("top", "test", lty = 1, pch = 21)

The result is something wired for the line crosses the point in the 
legend while the line does not cross the point in the main plot.

Is there possibility to draw the legend that line does not cross the 
point, i.e., like the pattern in the main plot?

Any help is really appreciated.

Best regards,
Jinsong

__
R-help@r-project.org 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.


__
R-help@r-project.org 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.


Re: [R] xlim with barplot

2013-09-22 Thread Jim Lemon

On 09/23/2013 01:56 PM, arun wrote:

You could try ggplot() as well.
library(ggplot2)
library(gridExtra)
library(plyr)
x1<- count(x)
  y1<- count(y)
p1<-ggplot(x1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="red")+xlim(c(35,85))+
 theme_bw()+ theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
panel.border=element_blank(),panel.background=element_blank())
  
p2<-ggplot(y1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="blue")+xlim(c(35,85))
 +theme_bw()+ theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(), panel.grid.minor=element_blank(),
panel.border=element_blank(),panel.background=element_blank())
  grid.arrange(p1,p2,nrow=2)

A.K.


Hi arun,
Okay, if we're allowed to use packages, challenge taken:

library(plotrix)
barp(tabulate(x,nbins=85)[35:85],names.arg=35:85)
barp(tabulate(y,nbins=85)[35:85],names.arg=35:85)

Jim

__
R-help@r-project.org 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.


Re: [R] xlim with barplot

2013-09-22 Thread arun
You could try ggplot() as well.
library(ggplot2)
library(gridExtra)
library(plyr)
x1<- count(x)
 y1<- count(y)
p1<-ggplot(x1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="red")+xlim(c(35,85))+
 theme_bw()+ theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
panel.border=element_blank(),panel.background=element_blank())
 
p2<-ggplot(y1,aes(x=x,y=freq))+geom_bar(stat="identity",colour="gray",fill="blue")+xlim(c(35,85))
 +theme_bw()+ theme(axis.line=element_line(colour="black"),
panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
panel.border=element_blank(),panel.background=element_blank())
 grid.arrange(p1,p2,nrow=2)

A.K.



- Original Message -
From: David Arnold 
To: r-help@r-project.org
Cc: 
Sent: Sunday, September 22, 2013 10:55 PM
Subject: [R] xlim with barplot

Hi,

I want to compare to barplots with same horizontal axis limits.

x=c(55,56,57,58,59,60,60,60,61,62,63,64,65)
y=c(35,40,45,50,55,60,60,60,65,70,75,80,85)

par(mfrow=c(2,1))
barplot(table(x),xlim=c(35,85))
barplot(table(y),xlim=c(35,85))
par(mfrow=c(1,1))

But the bars disappear.

 

Any suggestions?






--
View this message in context: 
http://r.789695.n4.nabble.com/xlim-with-barplot-tp4676717.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


__
R-help@r-project.org 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.


[R] legend for the plot with type = "b"

2013-09-22 Thread Jinsong Zhao

Hi there,

I plot a simple plot with the following code:

plot (rnorm(1:10), type = "b")
legend("top", "test", lty = 1, pch = 21)

The result is something wired for the line crosses the point in the 
legend while the line does not cross the point in the main plot.


Is there possibility to draw the legend that line does not cross the 
point, i.e., like the pattern in the main plot?


Any help is really appreciated.

Best regards,
Jinsong

__
R-help@r-project.org 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.


Re: [R] xlim with barplot

2013-09-22 Thread Jim Lemon

On 09/23/2013 12:55 PM, David Arnold wrote:

Hi,

I want to compare to barplots with same horizontal axis limits.

x=c(55,56,57,58,59,60,60,60,61,62,63,64,65)
y=c(35,40,45,50,55,60,60,60,65,70,75,80,85)

par(mfrow=c(2,1))
barplot(table(x),xlim=c(35,85))
barplot(table(y),xlim=c(35,85))
par(mfrow=c(1,1))

But the bars disappear.



Any suggestions?


Hi David,
The first suggestion is:

 par("usr")
[1] -0.32 13.72 -0.03  3.00

Specifying the x limits as above means that the bars are floating 
somewhere off to the left of the plot and thus not visible. You are 
mistaking the labels of the bars for their position. Now, having 
admonished you like some grumpy old school teacher, I suppose I should 
do something useful:


barplot(tabulate(x,nbins=85)[35:85])
barplot(tabulate(y,nbins=85)[35:85])

This is an underhanded trick to line up the bars as I think you want 
them. I suppose you want x labels as well:


barpos<-barplot(tabulate(x,nbins=85)[35:85],names.arg=xylabels)
axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80))
barplot(tabulate(y,nbins=85)[35:85],names.arg=xylabels)
axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80))

Jim

__
R-help@r-project.org 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.


Re: [R] Correlate rows of 2 matrices

2013-09-22 Thread arun
Hi,
You may try:
set.seed(49)
m1 = matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3)
 corsP<-vector()
  for(i in 1:3) corsP[i] =  cor(m1[i,], m2[i,])
 corsP
#[1]  0.58411274 -0.02382329  0.03760757

diag(cor(t(m1),t(m2)))
#[1]  0.58411274 -0.02382329  0.03760757

#or
mNew<- rbind(m1,m2)
 indx<-rep(seq(nrow(mNew)/2),2)
 sapply(split(seq_len(nrow(mNew)),indx),function(x) 
cor(t(mNew[x,]),t(mNew[x,]))[2])
 # 1   2   3 
 #0.58411274 -0.02382329  0.03760757 
#or
tapply(seq_along(indx),list(indx),FUN=function(x) 
cor(t(mNew[x,]),t(mNew[x,]))[2])
 # 1   2   3 
 #0.58411274 -0.02382329  0.03760757 
A.K.







From: Ira Sharenow 
To: arun  
Sent: Sunday, September 22, 2013 9:57 PM
Subject: Correlate rows of 2 matrices



Arun,

I have a new problem for you. 

I have two data frames (or matrices) and row by row I want to take the 
correlations.

So if I have a 3 row by 10 column matrix, I would produce 3 correlations.

Is there a way to merge the matrices and then use some sort of split?

Ideas/solutions much appreciated.

m1 = matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3)

> set.seed(22)
> m1 = matrix(rnorm(30), nrow = 3)
> m2 = matrix(rnorm(30), nrow = 3)
> for(i in 1:3) corsP[i] =  cor(m1[i,], m2[i,])
> corsP
[1] -0.50865019 -0.27760046  0.01423144

Thanks.

Ira  

__
R-help@r-project.org 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.


[R] xlim with barplot

2013-09-22 Thread David Arnold
Hi,

I want to compare to barplots with same horizontal axis limits.

x=c(55,56,57,58,59,60,60,60,61,62,63,64,65)
y=c(35,40,45,50,55,60,60,60,65,70,75,80,85)

par(mfrow=c(2,1))
barplot(table(x),xlim=c(35,85))
barplot(table(y),xlim=c(35,85))
par(mfrow=c(1,1))

But the bars disappear.

 

Any suggestions?






--
View this message in context: 
http://r.789695.n4.nabble.com/xlim-with-barplot-tp4676717.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Arcsine transformation

2013-09-22 Thread peake
Thanks for the advice. Like I said, I am still pretty new to R, and stats
in general. I tried wading through the search results on google, but I
didn't really find anything that I could understand. I am working with
percentages as my dependent variable, and I am trying to get my
distribution as close to normal as possible. Is an arcsine transformation
even the correct choice for my data? Querying R for help usually leaves me
even more confused, so I was hoping someone could help me out by walking me
through the process.


On Sun, Sep 22, 2013 at 8:00 PM, chuck.01 [via R] <
ml-node+s789695n4676708...@n4.nabble.com> wrote:

> ?asin
>
> also, try "Googling" anything you might want to do in R... it is there
>
> also, google... "R cheatsheet"  you will find several helpful sheets of
> useful functions.
>
>
>
> peake wrote
> I am tryin to perform an arcsine transformation on my data containig
> percentages as the dep. variable. Does anyone have a code that I could use
> to do that? I am relatively new to R. Thanks for your help!
>
>
>
> --
>  If you reply to this email, your message will be added to the discussion
> below:
> http://r.789695.n4.nabble.com/Arcsine-transformation-tp4676706p4676708.html
>  To unsubscribe from Arcsine transformation, click 
> here
> .
> NAML
>




--
View this message in context: 
http://r.789695.n4.nabble.com/Arcsine-transformation-tp4676706p4676712.html
Sent from the R help mailing list archive at Nabble.com.
[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


[R] PLS1 NIPALS question: error with chemometrics package?

2013-09-22 Thread Brad P
I am doing a self study, trying to understand PLS.
I have run across the following and hope that someone here can clarify as
to what is going on.

These are the data from Wold et al. (1984)

dat <- structure(list(t = 1:15, y = c(4.39, 4.42, 5, 5.85, 4.35, 4.51,
6.33, 6.37, 4.68, 5.04, 7.1, 5.04, 6, 5.48, 7.1), x1 = c(4.55,
4.74, 5.07, 5.77, 4.62, 4.41, 6.17, 6.17, 4.33, 4.62, 7.22, 4.64,
5.62, 6.19, 7.85), x2 = c(8.93, 8.93, 9.29, 9.9, 9.9, 9.93, 9.19,
9.19, 10.03, 10.29, 9.29, 10.22, 9.94, 9.77, 9.29), x3 = c(1.14,
1.14, 1.14, 1.14, 1.14, 1.14, 1.14, 1.14, 1.14, 1.14, 1.14, 1.14,
-0.07, -0.07, -0.07), x4 = c(0.7, 1.23, 0.19, 0.19, 1.23, 1.23,
0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19), x5 = c(0.19,
0.19, 0.7, 1.64, 1.64, 2.35, 2.83, 2.56, 2.42, 3.36, 2.43, 2.95,
1.64, 1.64, 3.8), x6 = c(0.49, 0.49, 0, -0.1, -0.1, -0.2, -0.13,
-0.13, -0.08, -0.13, -0.3, -0.08, -0.19, -0.19, -0.3), x7 = c(1.24,
1.24, 0, -0.47, -0.47, -0.51, -0.93, -0.93, -0.38, -0.93, -1.6,
-0.38, -0.47, -0.47, -1.6), x8 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L)), .Names = c("t", "y", "x1",
"x2", "x3", "x4", "x5", "x6", "x7", "x8"), class = "data.frame", row.names
= c(NA,
-15L))
In Wold et al. (1984) (pg 741, Table 2) beta coefficients are given for PLS
(1) and PLS (2) where (1) and (2) correspond to the number of PLS
components retained. The coefficients are not the same as those when I run
the following:

library("chemometrics")

# retaining 1 component:
pls1_nipals(X=dat[,c(3:10)], y=dat[2], a=1, scale=TRUE)$b

# retaining 2 components:
pls1_nipals(X=dat[,c(3:10)], y=dat[2], a=2, scale=TRUE)$b


However, if I modify the source code like this:

 pls1_nipals_mod <- function(X, y, a, it = 50, tol = 1e-08, scale = FALSE)
{
Xh <- scale(X, center = TRUE, scale = scale)
yh <- scale(y, center = TRUE, scale = scale)
T <- NULL
P <- NULL
C <- NULL
W <- NULL
for (h in 1:a) {
wh <- t(Xh) %*% yh/sum(yh^2)  # modified: / SS
wh <- wh/as.vector(sqrt(t(wh) %*% wh))
th <- Xh %*% wh
ch <- as.numeric(t(yh) %*% th)/sum(th^2)  # modified: / SS
# ch <- ch/as.vector(sqrt(t(th) %*% th))   # modified: removed
normalization of ch
ph <- t(Xh) %*% th/as.vector(t(th) %*% th)
Xh <- Xh - th %*% t(ph)
yh <- yh - th * ch
T <- cbind(T, th)
P <- cbind(P, ph)
C <- c(C, ch)
W <- cbind(W, wh)
}
b <- W %*% solve(t(P) %*% W) %*% C
list(P = P, T = T, W = W, C = C, b = b)
}

pls1_nipals_mod(X=dat[,c(3:10)], y=dat[2], a=1, scale=TRUE)$b
pls1_nipals_mod(X=dat[,c(3:10)], y=dat[2], a=2, scale=TRUE)$b

These beta coefficients are exactly the same as in Wold et al. (1984)


Furthermore, if I do a leave-one-out CV, my modified version has a PRESS =
1.27, whereas the original pls1_nipals() function has a PRESS = 18.11!

That's not good, right? Here is my LOOCV code, 1:1 lines added in plots:

### LOOCV for original function
out.j <- vector("list", length=nrow(dat))
for(j in c(2:nrow(dat), 1)){
b <- pls1_nipals(X=dat[-j,c(3:10)], y=dat[-j,2], a=2, scale=TRUE)$b
dats <- scale(dat)
y.est <- dats[j,c(3:10)] %*% b
y.obs <- dats[j,2]
out.j[[j]] <- data.frame(y.obs, y.est)
}
out <- do.call(rbind, out.j)
sqrt(sum((out[,1]-out[,2])^2) )
plot(out[,2]~ out[,1], ylab="pred", xlab="obs")
abline(0,1, col="grey")

### LOOCV for modified function
out.j <- vector("list", length=nrow(dat))
for(j in c(2:nrow(dat), 1)){
b <- pls1_nipals_mod(X=dat[-j,c(3:10)], y=dat[-j,2], a=2, scale=TRUE)$b
dats <- scale(dat)
y.est <- dats[j,c(3:10)] %*% b
y.obs <- dats[j,2]
out.j[[j]] <- data.frame(y.obs, y.est)
}
out <- do.call(rbind, out.j)
sqrt(sum((out[,1]-out[,2])^2) )
plot(out[,2]~ out[,1], ylab="pred", xlab="obs")
abline(0,1, col="grey")


Is this an error with the chemometrics function; or am I simply
understanding something incorrectly?
Thank you.


Citation: Wold, S., A. Ruhe, H. Wold, W. Dunn. (1984) The collinearity
problem in linear regression. The partial least squares (PLS) approach to
generalized inverses*" SIAM J. Sci. Stat. Comput.

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Arcsine transformation

2013-09-22 Thread Ben Bolker
On 13-09-22 09:25 PM, Peter Alspach wrote:
> Tena koe
> 
> I think you'll find the arcsine transformation is asin(sqrt(x/100))
> where × is the percentage.  However, it might be better to ask
> whether the data wouldn't be better analysed using generalised models
> (e.g., glm).
> 
> HTH 
> 

  Good point about arcsine-sqrt, and about GLMs: specifically see

Warton, David I., and Francis K. C. Hui. 2011. “The Arcsine Is Asinine:
The Analysis of Proportions in Ecology.” Ecology 92 (1) (January): 3–10.
doi:10.1890/10-0340.1.
http://www.esajournals.org/doi/full/10.1890/10-0340.1.

  I think the title is a little silly, but it's worth reading.

> Peter Alspach
> 
> -Original Message- From: r-help-boun...@r-project.org
> [mailto:r-help-boun...@r-project.org] On Behalf Of Ben Bolker Sent:
> Monday, 23 September 2013 12:54 p.m. To: r-h...@stat.math.ethz.ch 
> Subject: Re: [R] Arcsine transformation
> 
> peake  osu.edu> writes:
> 
>> 
>> I am tryin to perform an arcsine transformation on my data
>> containig percentages as the dep. variable. Does anyone have a code
>> that I could use to do that? I am relatively new to R. Thanks for
>> your help!
> 
> asin(x/100)
> 
> ? or
> 
> asin(x/100)*2/pi if you want the results rescaled to (0,1)
> 
> curve(asin(x/100)*2/pi,from=0,to=100)
> 
> __ R-help@r-project.org
> 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.
> 
> The contents of this e-mail are confidential and may be subject to
> legal privilege. If you are not the intended recipient you must not
> use, disseminate, distribute or reproduce all or any part of this
> e-mail or attachments.  If you have received this e-mail in error,
> please notify the sender and delete all material pertaining to this 
> e-mail.  Any opinion or views expressed in this e-mail are those of
> the individual sender and may not represent those of The New Zealand
> Institute for Plant and Food Research Limited.
>

__
R-help@r-project.org 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.


Re: [R] Arcsine transformation

2013-09-22 Thread Peter Alspach
Tena koe

I think you'll find the arcsine transformation is asin(sqrt(x/100)) where × is 
the percentage.  However, it might be better to ask whether the data wouldn't 
be better analysed using generalised models (e.g., glm).

HTH 

Peter Alspach

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Ben Bolker
Sent: Monday, 23 September 2013 12:54 p.m.
To: r-h...@stat.math.ethz.ch
Subject: Re: [R] Arcsine transformation

peake  osu.edu> writes:

> 
> I am tryin to perform an arcsine transformation on my data containig 
> percentages as the dep. variable. Does anyone have a code that I could 
> use to do that? I am relatively new to R. Thanks for your help!

asin(x/100)

? or

asin(x/100)*2/pi if you want the results rescaled to (0,1)

curve(asin(x/100)*2/pi,from=0,to=100)

__
R-help@r-project.org 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.

The contents of this e-mail are confidential and may be ...{{dropped:14}}

__
R-help@r-project.org 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.


Re: [R] Arcsine transformation

2013-09-22 Thread Ben Bolker
peake  osu.edu> writes:

> 
> I am tryin to perform an arcsine transformation on my data containig
> percentages as the dep. variable. Does anyone have a code that I could use
> to do that? I am relatively new to R. Thanks for your help!

asin(x/100)

? or

asin(x/100)*2/pi if you want the results rescaled to (0,1)

curve(asin(x/100)*2/pi,from=0,to=100)

__
R-help@r-project.org 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.


Re: [R] Conditioning plots (wth coplot function) with logistic regression curves

2013-09-22 Thread Kiyoshi Sasaki
Thank you!  Your codes produced plots that was fairly close to what I wanted. I 
am not familiar with ggplot2, so I need to study what exactly each code is 
doing. Actually, I also want to plot the observed (raw) data points (not just 
confidence intervals shown by ggplot, assuming that colored portion is 
confidence intervals). I actually produced the one I wanted before, but my hard 
drive died and lost all R codes I wrote are lost. I have posted an example plot 
I created before using coplot function (downloadable in PowerPoint format from 
http://laurentian.academia.edu/KiyoshiSasaki/Miscellaneous ).

Could you please show me how to plot raw data points in those ggplots? 

And, can anyone help me reproduce that plot I made before using coplot? I just 
cannot figure out how I did to make that plot (I spent several days...).

Thank you for taking your time to help me out.

Sincerely,

Kiyoshi
 


 From: Michael Friendly 

Cc: "r-help@r-project.org"  
Sent: Sunday, September 22, 2013 7:46:04 PM
Subject: Re: Conditioning plots (wth coplot function) with logistic regression 
curves


On 9/21/2013 11:12 PM, Kiyoshi Sasaki wrote:
> I have been trying to produce a conditional plot using coplot function 
> (http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/coplot.html) for 
> a binary response ("Presence" in my case) variable and one continuous 
> variable ("Overstory") given a specific levels of the other continuous 
> variable ("Ivy"). But, my codes produces an overlapping graph. Also, I want 
> to use three equal intervals for "Ivy" (i.e.,33.3 each), but I could not 
> figure out how. Here is my data and codes I used:
> 

If you feel it hurts because you are banging your head into a wall trying 
t[[elided Yahoo spam]]

Instead, you might consider using ggplot2, which handles this case
nicely, as far as I can tell from your description.

But first a due diligence caveat:  Say you come to me for consulting
on this little plotting question. I look at your data frame, dat,
and I see there are a number of other variables that might explain
Presence, so maybe the marginal plot that ignores them could be
misleading, e.g., any of Moist, Leaf, Prey, ... could moderate
the relation between overstory and presence, but you won't see that
in a marginal plot.


Here are a couple of quick ggplot examples, plotting classes of Ivy in the
same plot frame, and on the probability scale.

library(ggplot2)
ggplot(dat, aes(Overstory, Presence), color=Ivy>50 ) +
  stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy>50))

dat$Ivy3 <- factor(cut(dat$Ivy,3))
plt <- ggplot(dat, aes(Overstory, Presence), color=Ivy3 ) +
  stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy3))
plt

If you want separate panels, try something like

plt + facet_grid(. ~ Ivy3)

For plots on the logit scale, try something like
logit <- function(p) log(p)/log(1-p), then

plt + coord_trans(y="logit")

-- Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:  http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA
[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread jim holtman
CHeck out the 'tables' package if you want to create pretty outputs of
your tables.  Exactly where do you plan to use them?  You can also use
Sweave/Latex to create such tables.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Sep 22, 2013 at 4:45 PM, Laz  wrote:
> Hi,
>
> fortune("Yoda") returns a quote of fortunes.
>
> My problem was to draw horizontal and vertical lines to separate the blocks
> in my design. Each block has 4 elements. The first and second rows and
> columns are in block 1, 3rd  and 4th rows and 1st and 2nd columns are in 3
> etc
>
>
> [,1] [,2] [,3] [,4]
> [1,]2423
> [2,]1341
> [3,]3223
> [4,]1441
>
>
>
> On 9/22/2013 4:38 PM, Rui Barradas wrote:
>>
>> fortune("Yoda"). :-)
>
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


[R] Arcsine transformation

2013-09-22 Thread peake
I am tryin to perform an arcsine transformation on my data containig
percentages as the dep. variable. Does anyone have a code that I could use
to do that? I am relatively new to R. Thanks for your help!



--
View this message in context: 
http://r.789695.n4.nabble.com/Arcsine-transformation-tp4676706.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Conditioning plots (wth coplot function) with logistic regression curves

2013-09-22 Thread Michael Friendly

On 9/21/2013 11:12 PM, Kiyoshi Sasaki wrote:

I have been trying to produce a conditional plot using coplot function 
(http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/coplot.html) for a binary response ("Presence" in 
my case) variable and one continuous variable ("Overstory") given a specific levels of the other continuous 
variable ("Ivy"). But, my codes produces an overlapping graph. Also, I want to use three equal intervals for 
"Ivy" (i.e.,33.3 each), but I could not figure out how. Here is my data and codes I used:



If you feel it hurts because you are banging your head into a wall 
trying to get coplot to do this, the answer is: Don't do that!


Instead, you might consider using ggplot2, which handles this case
nicely, as far as I can tell from your description.

But first a due diligence caveat:  Say you come to me for consulting
on this little plotting question. I look at your data frame, dat,
and I see there are a number of other variables that might explain
Presence, so maybe the marginal plot that ignores them could be
misleading, e.g., any of Moist, Leaf, Prey, ... could moderate
the relation between overstory and presence, but you won't see that
in a marginal plot.


Here are a couple of quick ggplot examples, plotting classes of Ivy in the
same plot frame, and on the probability scale.

library(ggplot2)
ggplot(dat, aes(Overstory, Presence), color=Ivy>50 ) +
  stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy>50))


dat$Ivy3 <- factor(cut(dat$Ivy,3))
plt <- ggplot(dat, aes(Overstory, Presence), color=Ivy3 ) +
  stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy3))

plt

If you want separate panels, try something like

plt + facet_grid(. ~ Ivy3)

For plots on the logit scale, try something like
logit <- function(p) log(p)/log(1-p), then

plt + coord_trans(y="logit")

--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University  Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

__
R-help@r-project.org 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.


Re: [R] Coding several dummy variables into a single categorical variable

2013-09-22 Thread Ista Zahn
Hi Mosi,

The easiest approach would be be generate native from re and usborn
directly, rather than generating dummy codes as an intermediate step:

n$native <- interaction(n[c("re", "usborn")])

This has the advantage of preserving the meanings of the levels of
native in the resulting factor, so that you don't have to remember
what 0,1,2,3 mean. If for some reason you prefer the numeric levels
you can then change them using the levels() function.

If for some reason you don't have the original variables (re and
usborn in your example), you can construct a factor from the dummy
codes as follows:

n$native <- factor(
with(n, paste(native0, native1, native2, native3, sep = "")),
levels = c("1000", "0100", "0010", "0001"),
labels = c("0", "1", "2", "3"))


Best,
Ista

On Sun, Sep 22, 2013 at 2:40 PM, Mosi Ifatunji  wrote:
> Colleagues,
>
> I have generated several dummy variables:
>
> n$native0 <- 1 * (n$re=="white" & n$usborn=="yes")
> n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes")
> n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes")
> n$native3 <- 1 * (n$re=="carib" & n$usborn=="no")
>
> I would now like to combine these into a single categorical variable where 
> the new variable would be n$native.
>
> And values of native would be 0 through 3, where n$native0 would be a 0 value 
> on n$native, n$native1 would be a 1 value on n$native etc.
>
> Any help would be greatly appreciated.
>
> -- Mosi
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread Laz

Hi,

fortune("Yoda") returns a quote of fortunes.

My problem was to draw horizontal and vertical lines to separate the 
blocks in my design. Each block has 4 elements. The first and second 
rows and columns are in block 1, 3rd  and 4th rows and 1st and 2nd 
columns are in 3 etc


[,1] [,2] [,3] [,4]
[1,]2423
[2,]1341
[3,]3223
[4,]1441



On 9/22/2013 4:38 PM, Rui Barradas wrote:

fortune("Yoda"). :-)


__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread Rui Barradas

Hello,

No, 'Yoda' is not a package, the package is 'fortunes':

library(fortunes)  # load it in the R session
fortune("Yoda")

Or, if you don't want to load the package,

fortunes::fortune("Yoda")


Rui Barradas

Em 22-09-2013 21:32, Laz escreveu:

Is "Yoda" an R package? I tried:

install.packages("Yoda")
Warning in install.packages :
package 'Yoda' is not available (for R version 3.0.1)
Warning in install.packages :
package 'Yoda' is not available (for R version 3.0.1)


How do I proceed from here?
Regards,
Laz


On 9/22/2013 4:21 PM, Rolf Turner wrote:

On 09/23/13 07:52, Laz wrote:



I was wondering if we could draw some horizontal and vertical lines
to separate the blocks. Is it possible using R?



See fortune("Yoda"). :-)

 cheers,

 Rolf Turner




[[alternative HTML version deleted]]

__
R-help@r-project.org 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.



__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread Laz
Is "Yoda" an R package? I tried:

install.packages("Yoda")
Warning in install.packages :
   package 'Yoda' is not available (for R version 3.0.1)
Warning in install.packages :
   package 'Yoda' is not available (for R version 3.0.1)


How do I proceed from here?
Regards,
Laz


On 9/22/2013 4:21 PM, Rolf Turner wrote:
> On 09/23/13 07:52, Laz wrote:
>
> 
>> I was wondering if we could draw some horizontal and vertical lines 
>> to separate the blocks. Is it possible using R?
> 
>
> See fortune("Yoda"). :-)
>
> cheers,
>
> Rolf Turner
>


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread Rolf Turner

On 09/23/13 07:52, Laz wrote:


I was wondering if we could draw some horizontal and vertical lines to 
separate the blocks. Is it possible using R?



See fortune("Yoda"). :-)

cheers,

Rolf Turner

__
R-help@r-project.org 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.


Re: [R] gam and optim

2013-09-22 Thread Greg Dropkin
just to clarify how I see the error, it was the mis-definition of the
penalty term in the function dv. The following code corrects this error.
What is actually being minimised at this step is the penalised deviance
conditional on the smoothing parameter. A second issue is that the optim
default ("Nelder-Mead") would have to be recycled several times to
approximate the minimum obtained by gam, however, in this example, "BFGS"
gets it straight away.

sorry for all the confusion!

greg

set.seed(1)
library(mgcv)
x<-runif(100)
lp<-exp(-2*x)*sin(8*x)
y<-rpois(100,exp(lp))
m1<-gam(y~s(x),poisson)
W<-diag(fitted(m1))
X<-predict(m1,type="lpmatrix")
S<-m1$smooth[[1]]$S[[1]]
S<-rbind(0,cbind(0,S))
A<-X%*%solve(t(X)%*%W%*%X+m1$sp*S)%*%t(X)%*%W
sum(diag(A))
sum(m1$edf)
fit<-fitted(m1)
b<-m1$coef
range(exp(X%*%b)-fit)
z<-y/fit-1+X%*%b
range(A%*%z-X%*%b)

s<-m1$sp
dv<-function(t)
{
f<-exp(X%*%t)
-2*sum(y*log(f)-f-ifelse(y==0,0,y*log(y))+y)+s*t%*%S%*%t
}
dv(b)
m1$dev+m1$sp*b%*%S%*%b

t1<-optim(rep(0,10),dv,method="BFGS")
t1$p
b

dv(t1$p)
dv(b)

fit1<-exp(X%*%t1$p)

plot(x,y)
points(x,exp(lp),pch=16,col="green3")
points(x,fitted(m1),pch=16,cex=0.5,col="blue")
points(x,fit1,cex=1.5,col="red")





> please ignore this, I see the error.
>
> greg
>
>> hi
>>
>> probably a silly mistake, but I expected gam to minimise the penalised
>> deviance.
>>
>> thanks
>>
>> greg
>>
>> set.seed(1)
>> library(mgcv)
>> x<-runif(100)
>> lp<-exp(-2*x)*sin(8*x)
>> y<-rpois(100,exp(lp))
>> plot(x,y)
>> m1<-gam(y~s(x),poisson)
>> points(x,exp(lp),pch=16,col="green3")
>> points(x,fitted(m1),pch=16,cex=0.5,col="blue")
>> W<-diag(fitted(m1))
>> X<-predict(m1,type="lpmatrix")
>> S<-m1$smooth[[1]]$S[[1]]
>> S<-rbind(0,cbind(0,S))
>> A<-X%*%solve(t(X)%*%W%*%X+m1$sp*S)%*%t(X)%*%W
>> sum(diag(A))
>> sum(m1$edf)
>> fit<-fitted(m1)
>> b<-m1$coef
>> range(exp(X%*%b)-fit)
>> z<-y/fit-1+X%*%b
>> range(A%*%z-X%*%b)
>>
>> dv<-function(t)
>> {
>> f<-exp(X%*%t)
>> -2*sum(y*log(f)-f-ifelse(y==0,0,y*log(y))+y)+t%*%S%*%t
>> }
>> dv(b)
>> m1$dev+b%*%S%*%b
>>
>> #so far, so good
>>
>>
>> t1<-optim(rep(0,10),dv)
>> t1$p
>> b
>>
>> #different
>>
>> dv(t1$p)
>> dv(b)
>>
>> #different, and dv(t1$p) is lower!
>>
>> fit1<-exp(X%*%t1$p)
>> points(x,fit1,pch=16,cex=0.5,col="red")
>>
>> # different
>> # gam found b which does approximate the true curve, but does not
>> minimise
>> the penalised deviance, by a long shot.
>>
>>
>
>

__
R-help@r-project.org 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.


Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread Laz

Thanks Jim,
What you have done is very good.
I was wondering if we could draw some horizontal and vertical lines to 
separate the blocks. Is it possible using R?

Regards,
Laz

On 9/22/2013 11:10 AM, jim holtman wrote:

Is this what you were after:



## A function to generate a RCB design

rcbd<-function(b,g,rb,cb,r,c)

+ {
+# b =number of blocks
+# g = a vector of treatments
+# rb = number of rows per blocks
+# cb =number of columns per block
+# r = total rows
+# c = total columns
+ library(foreach)
+ genotypes<-times(b) %do% sample(g,length(g))
+ block<-rep(1:b,each=length(g))
+ genotypes<-factor(genotypes)
+ block<-factor(block)
+ ### generate the base design
+ k<-c/cb # number of blocks on the x-axis
+ x<-rep(rep(1:r,each=cb),k)  # X-coordinate
+ l<-cb
+ p<-r/rb
+ m<-l+1
+ d<-l*b/p
+ y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
+ data.frame(x,y,block,genotypes)
+ }

set.seed(100)
ans <- rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)

result <- matrix(nrow = max(ans$x), ncol = max(ans$y))
result[cbind(ans$y, ans$x)] <- ans$genotypes
result

  [,1] [,2] [,3] [,4]
[1,]2423
[2,]1341
[3,]3223
[4,]1441
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Sep 22, 2013 at 1:07 AM, Laz  wrote:

Dear R users,

I have a function I created to generate randomized block designs given
below. Once  I generate the design, I would like to plot it in a
rectangular form to mimic the excel style where the blocks are
represented with the treatment values placed exactly where their
appropriate X and Y coordinates are.

## A function to generate a RCB design

rcbd<-function(b,g,rb,cb,r,c)
{
# b =number of blocks
# g = a vector of treatments
# rb = number of rows per blocks
# cb =number of columns per block
# r = total rows
# c = total columns
library(foreach)
genotypes<-times(b) %do% sample(g,length(g))
block<-rep(1:b,each=length(g))
genotypes<-factor(genotypes)
block<-factor(block)
### generate the base design
k<-c/cb # number of blocks on the x-axis
x<-rep(rep(1:r,each=cb),k)  # X-coordinate
l<-cb
p<-r/rb
m<-l+1
d<-l*b/p
y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
data.frame(x,y,block,genotypes)
}
set.seed(100)
rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)

 x y block genotypes
1  1 1 1 2
2  1 2 1 1
3  2 1 1 4
4  2 2 1 3
5  3 1 2 2
6  3 2 2 4
7  4 1 2 3
8  4 2 2 1
9  1 3 3 3
10 1 4 3 1
11 2 3 3 2
12 2 4 3 4
13 3 3 4 2
14 3 4 4 4
15 4 3 4 3
16 4 4 4 1

How can I produce a diagram like this one below or a better one for any run of 
my function?


2   4   2   3
1   3   4   1
3   2   2   3
1   4   4   1


Regards,
Laz



 [[alternative HTML version deleted]]

__
R-help@r-project.org 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.


__
R-help@r-project.org 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.


Re: [R] Coding several dummy variables into a single categorical variable

2013-09-22 Thread arun
Hi,

If you don't want to install any package:
colnames(df1)[grep("native",colnames(df1))]<- 
gsub("([[:alpha:]])(\\d+)","\\1_\\2",colnames(df1)[grep("native",colnames(df1))])
  df2<-reshape(df1,direction="long",varying=3:ncol(df1),sep="_")[,-5]
df3<-df2[as.logical(df2$native),-4]
colnames(df3)[3]<- "native"
 row.names(df3)<- 1:nrow(df3)
 identical(df2New,df3)
#[1] TRUE
A.K.



- Original Message -
From: arun 
To: Mosi Ifatunji 
Cc: R help 
Sent: Sunday, September 22, 2013 3:34 PM
Subject: Re: [R] Coding several dummy variables into a single categorical   
variable

Hi,

Not sure this is what you wanted.
df2<- melt(df1,id.vars=c("re","usborn"))
df2New<-df2[df2$value==1,-4]
 df2New$variable<-as.numeric(gsub("[[:alpha:]]","",df2New$variable))
colnames(df2New)[3]<- "native"
 row.names(df2New)<- 1:nrow(df2New)
df2New
#  re usborn native
#1  white    yes  0
#2  white    yes  0
#3  white    yes  0
#4  white    yes  0
#5  white    yes  0
#6   afam    yes  1
#7   afam    yes  1
#8   afam    yes  1
#9  carib    yes  2
#10 carib    yes  2
#11 carib    yes  2
#12 carib    yes  2
#13 carib    yes  2
#14 carib no  3
#15 carib no  3
#16 carib no  3
#17 carib no  3
#18 carib no  3


A.K.



- Original Message -
From: arun 
To: Mosi Ifatunji 
Cc: R help 
Sent: Sunday, September 22, 2013 3:25 PM
Subject: Re: [R] Coding several dummy variables into a single categorical    
variable

Hi,
Try:


set.seed(385)
df<- data.frame(re= sample(c("white","afam","carib"),20,replace=TRUE), usborn= 
sample(c("yes","no"),20,replace=TRUE),stringsAsFactors=FALSE) 

df1<-within(df,{native3<- 1*(re=="carib" & usborn=="no"); native2<- 
1*(re=="carib" & usborn=="yes"); native1<- 1*(re=="afam" & usborn=="yes"); 
native0<- 1*(re=="white" & usborn=="yes")})

library(reshape2)
df2<- melt(df1,id.vars=c("re","usborn"))[,-3]
colnames(df2)[3]<- "native"



A.K.


- Original Message -
From: Mosi Ifatunji 
To: "r-help@r-project.org" 
Cc: 
Sent: Sunday, September 22, 2013 2:40 PM
Subject: [R] Coding several dummy variables into a single categorical    
variable

Colleagues,

I have generated several dummy variables:

n$native0 <- 1 * (n$re=="white" & n$usborn=="yes")
n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes")
n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes")
n$native3 <- 1 * (n$re=="carib" & n$usborn=="no")

I would now like to combine these into a single categorical variable where the 
new variable would be n$native.

And values of native would be 0 through 3, where n$native0 would be a 0 value 
on n$native, n$native1 would be a 1 value on n$native etc.

Any help would be greatly appreciated.

-- Mosi

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Coding several dummy variables into a single categorical variable

2013-09-22 Thread arun
Hi,

Not sure this is what you wanted.
df2<- melt(df1,id.vars=c("re","usborn"))
df2New<-df2[df2$value==1,-4]
 df2New$variable<-as.numeric(gsub("[[:alpha:]]","",df2New$variable))
colnames(df2New)[3]<- "native"
 row.names(df2New)<- 1:nrow(df2New)
df2New
#  re usborn native
#1  white    yes  0
#2  white    yes  0
#3  white    yes  0
#4  white    yes  0
#5  white    yes  0
#6   afam    yes  1
#7   afam    yes  1
#8   afam    yes  1
#9  carib    yes  2
#10 carib    yes  2
#11 carib    yes  2
#12 carib    yes  2
#13 carib    yes  2
#14 carib no  3
#15 carib no  3
#16 carib no  3
#17 carib no  3
#18 carib no  3


A.K.



- Original Message -
From: arun 
To: Mosi Ifatunji 
Cc: R help 
Sent: Sunday, September 22, 2013 3:25 PM
Subject: Re: [R] Coding several dummy variables into a single categorical   
variable

Hi,
Try:


set.seed(385)
df<- data.frame(re= sample(c("white","afam","carib"),20,replace=TRUE), usborn= 
sample(c("yes","no"),20,replace=TRUE),stringsAsFactors=FALSE) 

df1<-within(df,{native3<- 1*(re=="carib" & usborn=="no"); native2<- 
1*(re=="carib" & usborn=="yes"); native1<- 1*(re=="afam" & usborn=="yes"); 
native0<- 1*(re=="white" & usborn=="yes")})

library(reshape2)
df2<- melt(df1,id.vars=c("re","usborn"))[,-3]
colnames(df2)[3]<- "native"



A.K.


- Original Message -
From: Mosi Ifatunji 
To: "r-help@r-project.org" 
Cc: 
Sent: Sunday, September 22, 2013 2:40 PM
Subject: [R] Coding several dummy variables into a single categorical    
variable

Colleagues,

I have generated several dummy variables:

n$native0 <- 1 * (n$re=="white" & n$usborn=="yes")
n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes")
n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes")
n$native3 <- 1 * (n$re=="carib" & n$usborn=="no")

I would now like to combine these into a single categorical variable where the 
new variable would be n$native.

And values of native would be 0 through 3, where n$native0 would be a 0 value 
on n$native, n$native1 would be a 1 value on n$native etc.

Any help would be greatly appreciated.

-- Mosi

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Coding several dummy variables into a single categorical variable

2013-09-22 Thread arun
Hi,
Try:


set.seed(385)
df<- data.frame(re= sample(c("white","afam","carib"),20,replace=TRUE), usborn= 
sample(c("yes","no"),20,replace=TRUE),stringsAsFactors=FALSE) 

df1<-within(df,{native3<- 1*(re=="carib" & usborn=="no"); native2<- 
1*(re=="carib" & usborn=="yes"); native1<- 1*(re=="afam" & usborn=="yes"); 
native0<- 1*(re=="white" & usborn=="yes")})

library(reshape2)
df2<- melt(df1,id.vars=c("re","usborn"))[,-3]
colnames(df2)[3]<- "native"
df2

A.K.


- Original Message -
From: Mosi Ifatunji 
To: "r-help@r-project.org" 
Cc: 
Sent: Sunday, September 22, 2013 2:40 PM
Subject: [R] Coding several dummy variables into a single categorical   variable

Colleagues,

I have generated several dummy variables:

n$native0 <- 1 * (n$re=="white" & n$usborn=="yes")
n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes")
n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes")
n$native3 <- 1 * (n$re=="carib" & n$usborn=="no")

I would now like to combine these into a single categorical variable where the 
new variable would be n$native.

And values of native would be 0 through 3, where n$native0 would be a 0 value 
on n$native, n$native1 would be a 1 value on n$native etc.

Any help would be greatly appreciated.

-- Mosi

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


[R] Coding several dummy variables into a single categorical variable

2013-09-22 Thread Mosi Ifatunji
Colleagues,

I have generated several dummy variables:

n$native0 <- 1 * (n$re=="white" & n$usborn=="yes")
n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes")
n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes")
n$native3 <- 1 * (n$re=="carib" & n$usborn=="no")

I would now like to combine these into a single categorical variable where the 
new variable would be n$native.

And values of native would be 0 through 3, where n$native0 would be a 0 value 
on n$native, n$native1 would be a 1 value on n$native etc.

Any help would be greatly appreciated.

-- Mosi

__
R-help@r-project.org 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.


Re: [R] PCA

2013-09-22 Thread Vojtěch Zeisek
Hi,
see tutorial for Adegenet, http://adegenet.r-forge.r-project.org/ |
Documents | adegenet-basics.pdf | section 6. It should help You.
Vojtěch



-
Vojtěch Zeisek

Department of Botany, Faculty of Science, Charles Uni., Prague, CZ
Institute of Botany, Academy of Science, Czech Republic
Community of the openSUSE GNU/Linux

https://www.natur.cuni.cz/faculty-en?set_language=en
http://www.ibot.cas.cz/?p=index&site=en
http://www.opensuse.org/
http://trapa.cz/
--
View this message in context: 
http://r.789695.n4.nabble.com/PCA-tp4676674p4676691.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org 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.


Re: [R] Obtaining data from a different row of data frame

2013-09-22 Thread arun
Ira,
No problem.
If you change the ?for() loop to ?lapply, there should be increase in speed 
(Usually, it is not the case.  Here it is does, but still not as good in terms 
of speed as the method I showed).

df1<- structure(list(Dates = structure(c(13151, 13152, 13153, 13154,
 13157, 13158, 13159, 13160, 13161, 13164), class = "Date"), P1 = c(10,
 13, 16, 19, 22, 25, 28, 31, 34, 37), P2 = c(100, 102, 104, 106,
 108, 110, 112, 114, 116, 118), P3 = c(90, 94, 98, 102, 106, 110,
 114, 118, 122, 126), P4 = c(70, 75, 80, 85, 90, 95, 100, 105,
 110, 115), OF1 = c(3, 3, 4, 5, 2, 2, 2, 1, 1, 5), OF2 = c(5,
 3, 4, 2, 1, 2, 2, 1, 1, 0), OF3 = c(4, 3, 4, 1, 3, 2, 2, 1, 1,
 0), OF4 = c(3, 5, 4, 2, 3, 1, 2, 1, 1, 0)), .Names = c("Dates",
 "P1", "P2", "P3", "P4", "OF1", "OF2", "OF3", "OF4"), row.names = c(NA,
 -10L), class = "data.frame")
df1$OF2[9]<-4

df2<- df1
 df2[,10:13]<- NA
colnames(df2)[10:13]<- paste0("newPrice",1:4)

##your code

for(j in 2:5) {
 df2[j+8] = df2[df2[,j+4] + row(df2)[,j], j]
 }

#using ?lapply()
 df1[,10:13]<-lapply(2:5,function(j) {df1[df1[,j+4]+row(df2)[,j],j]})
colnames(df1)[10:13]<- colnames(df2)[10:13]
 identical(df1,df2)
#[1] TRUE

###Speed check:
set.seed(29)
 df2<- data.frame(Dates=seq(as.Date("2006-01-03"),length.out=2000,by="1 
day"),cbind(matrix(sample(10:120,2000*300,replace=TRUE),ncol=300),matrix(sample(0:6,2000*300,replace=TRUE),ncol=300)))
 colnames(df2)[2:301]<- paste0("P",1:300)
 colnames(df2)[302:601]<- paste0("OF",1:300)
 df3<- df2


df2[,602:901]<-NA
 colnames(df2)[602:901]<- paste0("newPrice",1:300)

system.time({
 for(j in grep("^P",colnames(df2))) {
  df2[j+600] = df2[df2[,j+300] + row(df2)[,j], j]
  }
 })
 # user  system elapsed 
 #11.652   0.148  11.822 

system.time({df3[,602:901]<-lapply(2:301,function(j) 
{df3[df3[,j+300]+row(df3)[,j],j]}) })
#  user  system elapsed 
#  2.960   0.000   2.962 
colnames(df3)[602:901]<- colnames(df2)[602:901]
 identical(df2,df3)
#[1] TRUE

A.K.
 







From: Ira Sharenow 
To: arun  
Sent: Sunday, September 22, 2013 10:49 AM
Subject: Re: [R] Obtaining data from a different row of data frame



Arun,

Thanks for the time you spent helping me.

I always learned to use the apply family (but maybe your strategies are 
faster), and now I think I am going to learn Hadley Wickham’s methods. Right 
now I need to do other parts of the project. In a few days I will take another 
look at your code to see if I can get more out of my code.

For my current project once I am finished my boss will use my code and possibly 
modify it, so speed is just one factor. Transparency and his future coding time 
is another consideration. I need to balance things off. 

I need tolerable speed and relatively easy to understand code. It is an 
interesting trade off.

Thank again for your help. I’ll get back to you when I take another look at the 
details of what you wrote.

Ira 
On 9/21/2013 11:27 PM, arun wrote:

HI, A modified code to avoid the ?sapply()
df1<- structure(list(Dates = structure(c(13151, 13152, 13153, 13154,
 13157, 13158, 13159, 13160, 13161, 13164), class = "Date"), P1 = c(10,
 13, 16, 19, 22, 25, 28, 31, 34, 37), P2 = c(100, 102, 104, 106,
 108, 110, 112, 114, 116, 118), P3 = c(90, 94, 98, 102, 106, 110,
 114, 118, 122, 126), P4 = c(70, 75, 80, 85, 90, 95, 100, 105,
 110, 115), OF1 = c(3, 3, 4, 5, 2, 2, 2, 1, 1, 5), OF2 = c(5,
 3, 4, 2, 1, 2, 2, 1, 1, 0), OF3 = c(4, 3, 4, 1, 3, 2, 2, 1, 1,
 0), OF4 = c(3, 5, 4, 2, 3, 1, 2, 1, 1, 0)), .Names = c("Dates",
 "P1", "P2", "P3", "P4", "OF1", "OF2", "OF3", "OF4"), row.names = c(NA,
 -10L), class = "data.frame")
df1$OF2[9]<-4 df2<- df1
 df2[,10:13]<- NA
colnames(df2)[10:13]<- paste0("newPrice",1:4) ##your code for(j in 2:5) {
 df2[j+8] = df2[df2[,j+4] + row(df2)[,j], j]
 }
indx1<- unlist(df1[,grep("OF",colnames(df1))],use.names=FALSE)
 indx1[rep(seq(nrow(df1)),4)%in% 6:10][indx1[rep(seq(nrow(df1)),4)%in% 6:10]- 
rep(5:1,4)>=0]<- NA val1<- unlist(df1[,grep("P",colnames(df1))],use.names=FALSE)
 df1[,10:13]<- val1[indx1+seq_along(indx1)]
 colnames(df1)[10:13]<- colnames(df2)[10:13]
identical(df1[,10:13],df2[,10:13])
#[1] TRUE ###On a bigger dataset:
set.seed(29)
 df2<- data.frame(Dates=seq(as.Date("2006-01-03"),length.out=2000,by="1 
day"),cbind(matrix(sample(10:120,2000*300,replace=TRUE),ncol=300),matrix(sample(0:6,2000*300,replace=TRUE),ncol=300)))
 colnames(df2)[2:301]<- paste0("P",1:300)
 colnames(df2)[302:601]<- paste0("OF",1:300)
 df3<- df2 df2[,602:901]<-NA
 colnames(df2)[602:901]<- paste0("newPrice",1:300)
 system.time({
 for(j in grep("^P",colnames(df2))) {
  df2[j+600] = df2[df2[,j+300] + row(df2)[,j], j]
  }
 })
#   user  system elapsed
 #  8.508   0.000   8.523  colN_OF<- ncol(df3[,grep("OF",colnames(df3))])
system.time({
 indx1<- unlist(df3[,grep("OF",colnames(df3))],use.names=FALSE)
 indx1[rep(seq(nrow(df3)),colN_OF) %in% 
1995:2000][indx1[rep(seq(nrow(df3)),colN_OF) %in% 1995:2000] - 
rep(6:1,colN_OF)>=0] <-NA
  val1<- unlist(df3[,grep("P",colnames(df3))],use

Re: [R] Creating rectangular plots with x and y coordinates and treatments from a matrix for a randomized block design

2013-09-22 Thread jim holtman
Is this what you were after:


> ## A function to generate a RCB design
>
> rcbd<-function(b,g,rb,cb,r,c)
+ {
+# b =number of blocks
+# g = a vector of treatments
+# rb = number of rows per blocks
+# cb =number of columns per block
+# r = total rows
+# c = total columns
+ library(foreach)
+ genotypes<-times(b) %do% sample(g,length(g))
+ block<-rep(1:b,each=length(g))
+ genotypes<-factor(genotypes)
+ block<-factor(block)
+ ### generate the base design
+ k<-c/cb # number of blocks on the x-axis
+ x<-rep(rep(1:r,each=cb),k)  # X-coordinate
+ l<-cb
+ p<-r/rb
+ m<-l+1
+ d<-l*b/p
+ y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
+ data.frame(x,y,block,genotypes)
+ }
> set.seed(100)
> ans <- rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)
>
> result <- matrix(nrow = max(ans$x), ncol = max(ans$y))
> result[cbind(ans$y, ans$x)] <- ans$genotypes
> result
 [,1] [,2] [,3] [,4]
[1,]2423
[2,]1341
[3,]3223
[4,]1441
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Sep 22, 2013 at 1:07 AM, Laz  wrote:
> Dear R users,
>
> I have a function I created to generate randomized block designs given
> below. Once  I generate the design, I would like to plot it in a
> rectangular form to mimic the excel style where the blocks are
> represented with the treatment values placed exactly where their
> appropriate X and Y coordinates are.
>
> ## A function to generate a RCB design
>
> rcbd<-function(b,g,rb,cb,r,c)
> {
># b =number of blocks
># g = a vector of treatments
># rb = number of rows per blocks
># cb =number of columns per block
># r = total rows
># c = total columns
> library(foreach)
> genotypes<-times(b) %do% sample(g,length(g))
> block<-rep(1:b,each=length(g))
> genotypes<-factor(genotypes)
> block<-factor(block)
> ### generate the base design
> k<-c/cb # number of blocks on the x-axis
> x<-rep(rep(1:r,each=cb),k)  # X-coordinate
> l<-cb
> p<-r/rb
> m<-l+1
> d<-l*b/p
> y<-c(rep(1:l,r),rep(m:d,r)) # Y-coordinate
> data.frame(x,y,block,genotypes)
> }
> set.seed(100)
> rcbd(b=4,g=1:4,rb=2,cb=2,r=4,c=4)
>
> x y block genotypes
> 1  1 1 1 2
> 2  1 2 1 1
> 3  2 1 1 4
> 4  2 2 1 3
> 5  3 1 2 2
> 6  3 2 2 4
> 7  4 1 2 3
> 8  4 2 2 1
> 9  1 3 3 3
> 10 1 4 3 1
> 11 2 3 3 2
> 12 2 4 3 4
> 13 3 3 4 2
> 14 3 4 4 4
> 15 4 3 4 3
> 16 4 4 4 1
>
> How can I produce a diagram like this one below or a better one for any run 
> of my function?
>
>
> 2   4   2   3
> 1   3   4   1
> 3   2   2   3
> 1   4   4   1
>
>
> Regards,
> Laz
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Translating recoding syntax from SPSS to R

2013-09-22 Thread Mosi Ifatunji
Thanks!

This worked wonderfully!

Very exciting!

-- Mosi


On Sep 22, 2013, at 3:20 AM, Joshua Wiley  wrote:

Just a slight addition to Arun's answer: within() saves typing and
makes life a bit easier

#
set.seed(429)
dat1 <- data.frame(
 race=sample(1:3,20,replace=TRUE),
 usborn=sample(0:2,20,replace=TRUE))

# within is a nice way to add variables
# and make modifications within a dataset
# saves typing lots of dat1$

dat1 <- within(dat1, {
confused <- 1 * ((race==1 | race==2) & usborn==0)
})

head(dat1)

# only confused 0
dat1 <- subset(dat1, confused == 0)
#


On Sat, Sep 21, 2013 at 11:47 PM, arun  wrote:
> Hi,
> Try:
> set.seed(429)
> dat1<- 
> data.frame(race=sample(1:3,20,replace=TRUE),usborn=sample(0:2,20,replace=TRUE))
> dat1$confused<- 1*((dat1$race==1|dat1$race==2) & dat1$usborn==0)
> head(dat1)
> #  race usborn confused
> #13  20
> #21  01
> #32  10
> #43  20
> #51  20
> #61  10
> A.K.
> 
> 
> 
> - Original Message -
> From: Mosi Ifatunji 
> To: "r-help@r-project.org" 
> Cc:
> Sent: Saturday, September 21, 2013 6:03 PM
> Subject: [R] Translating recoding syntax from SPSS to R
> 
> Colleagues,
> 
> I am in the process of learning R. I've been able to import my dataset (from 
> Stata) and do some simple coding. I have now come to coding situation that 
> requires some assistance. This is some code in SPSS that I would like to be 
> able to execute in R:
> 
> if (race eq 1 and usborn=0) confused=1 .
> if (race eq 2 and usborn=0) confused=1 .
> if (race eq 1 and usborn=1) confused=0 .
> if (race eq 2 and usborn=1) confused=0 .
> if (race eq 3 and usborn=1) confused=0 .
> if (race eq 3 and cohort=1) confused=0 .
> if (race eq 3 and cohort=2) confused=0 .
> variable labels confused "R claims to be both an African American and foriegn 
> born" .
> value labels confused
>1 "Both AfAm and Foreign"
>2 "Not" .
> select if (confused eq 0) .
> 
> Any assistance would be greatly appreciated.
> 
> -- Mosi
> __
> R-help@r-project.org 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.
> 
> 
> __
> R-help@r-project.org 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://joshuawiley.com/
Senior Analyst - Elkhart Group Ltd.
http://elkhartgroup.com

__
R-help@r-project.org 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.


Re: [R] time zones from longitude, latitude, and date

2013-09-22 Thread Gabor Grothendieck
On Sat, Sep 21, 2013 at 10:25 AM, David Winsemius
 wrote:
>
> On Sep 21, 2013, at 3:13 AM, Prof Brian Ripley wrote:
>
>> On 21/09/2013 08:17, Gabor Grothendieck wrote:
>>>
>>> On Fri, Sep 20, 2013 at 4:31 PM, carlisle thacker
>>>  wrote:

 I was looking for something like shown on the map:

 http://upload.wikimedia.org/wikipedia/commons/8/88/World_Time_Zones_Map.png

 Information about local daylight savings times would also help.

 The data are from ships, supposedly in local time, but no time-zone info
 is
 given.  A function that would return time zone and whether or not
 daylight
 savings time applies at given date would would help.  I'm trying to
 track
 down more information about the data and whether they can be referenced
 to
 UTC.
>>>
>>>
>>> The zone.tab file has this information.  See the Examples section at
>>> the end of ?Sys.timezone for info on its whereabouts.
>>
>>
>> On some OSes only.
>
>
> This is a couple of snippets of that file from a Mac:
> /usr/share/zoneinfo/zone.tab
> #-
> # This file contains a table with the following columns:
> # 1.  ISO 3166 2-character country code.  See the file `iso3166.tab'.
> # 2.  Latitude and longitude of the zone's principal location
> # in ISO 6709 sign-degrees-minutes-seconds format,
> # either +-DDMM+-DDDMM or +-DDMMSS+-DDDMMSS,
> # first latitude (+ is north), then longitude (+ is east).
> # 3.  Zone name used in value of TZ environment variable.
> # 4.  Comments; present if and only if the country has multiple rows.
> #-
>
> US  +340308-1181434 America/Los_Angeles Pacific Time
> US  +611305-1495401 America/Anchorage   Alaska Time
> US  +581807-1342511 America/Juneau  Alaska Time - Alaska panhandle
> US  +593249-1394338 America/Yakutat Alaska Time - Alaska panhandle neck
> US  +643004-1652423 America/NomeAlaska Time - west Alaska
> US  +515248-1763929 America/AdakAleutian Islands
> US  +211825-1575130 Pacific/HonoluluHawaii
> UY  -3453-05611 America/Montevideo
> UZ  +3940+06648 Asia/Samarkand  west Uzbekistan
> UZ  +4120+06918 Asia/Tashkent   east Uzbekistan
> VA  +415408+0122711 Europe/Vatican
>
>
> After looking at it I'm not sure it will be of much additional value for the
> purposes outlined. It does not provide boundaries of time zones.

The idea is to take the nearest lat/long found. If the input data is
lat/longs of major cities then it would likely work exactly.  If not
then it would only be an approximation but that might be sufficient
depending on one's purpose.  The nice thing is that the time zones you
get out is precisely the ones that R can use.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-help@r-project.org 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.


Re: [R] Setting a new method for generic function to satisfy "R CMD check"

2013-09-22 Thread Duncan Murdoch

On 13-09-21 3:30 PM, Mikhail Beketov wrote:

Dear All,

Can somebody explain me how to correctly set a new class-specific method
for generic functions (e.g. for "plot", "summary" and "print"). I simply
programmed these functions with the class names and it works perfectly.
E.g.:

x<-2
y<-3
class(x)<-"newclass"
class(y)<-"newclass"
print.newclass<-function(x){ cat(x*10) }
print(x)
print(y)

However, I need the new methods for a new package, and the "R CMD check"
gives me warnings that my approach is not correct. I have to define
"methods" and do not use full names as "print.newclass".
I read the "Writing R Extensions" and it is not explained there (at least
to such level that I could get it) and I didn't get anything understandable
by googling.

Maybe someone can help me?


You need to declare methods in the NAMESPACE file, and there is special 
markup for them in Rd files.


Duncan Murdoch

__
R-help@r-project.org 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.


Re: [R] Re : Re: Re : Privacy rights of an old user of this list

2013-09-22 Thread Uwe Ligges

On 21.09.2013 22:40, John Gonzalez wrote:

As I said previously, I'm only concerned about what was published in the
archive of stat.ethz.ch.


And it is OK to habe your post on Nabble (and many others) after you 
removed it from stat.ethz.ch? Are the swiss that bad?




I visited https://stat.ethz.ch/mailman/listinfo/r-help to find out where
I had to send my request to remove those messages and I read:

"R-help list run by maechler at stat.math.ethz.ch, bates at r-project.org"

So I emailed these addresses and r-help-owner at r-project.org.



Right, Martin Maechler may be on vacations, and he will probably answer 
that he does not remove posts in general.





Only David got back to me, denying my request, so this is why I'm
discussing it here.

If anybody could provide me a contact of who is controlling this
archive, I would definitely appreciate it.

Regards,

John


- Message d'origine -

De : Uwe Ligges

Envoyés : 21.09.13 10:26

À : Adan Leobardo Martinez Cruz

Objet : Re: [R] Re : Privacy rights of an old user of this list

The locations where the mailing list are archived are not even
controlled by any member of R-core or the R foundation. Everybody,
including John, could collect mails from the list and publish them.
This is a well known property of a public mailing list.

Uwe Ligges




On 17.09.2013 13:29, Adan Leobardo Martinez Cruz wrote:
> Dear all,
>
> I will express my opinion without knowing the details of the posts John would 
like to be removed.
>
> In the current state, people posting on this and other servers have no clear 
way to go when trying to remove their posts.
> It is a likely event that the number of people attempting the removal of 
their past posts will increase. Their reasons will vary and may or not may be 
reasonable to us.
> It seems that a discussion on how the R-server will handle this likely 
situation is needed (including the possibility of keeping the current policy, of 
course)
> Once the decision has been taken, a warning note would be helpful for 
newcomers (something in big, black letters saying that whatever we post will not 
be removed or something like that).
>
> Best regards to all,
>
> adan
>
>
>
> 
> From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] on behalf 
of John Gonzalez [john.gonza...@gmx.fr]
> Sent: Monday, September 16, 2013 5:14 PM
> To: David Winsemius; Albin Blaschka; Duncan Murdoch; Jeff Newmiller; S  
Ellison; Jim Lemon
> Cc: r-help@r-project.org
> Subject: [R] Re :  Privacy rights of an old user of this list
>
> I would like to thank David for letting me publish this and discuss it 
openly. I must acknowledge from the answers that I received to my post, that the 
administrators of this list are doing what seems to be fair to me: what most 
people demand or understand that is right.
> However I don't share your views and I honestly think you are making a 
mistake which may hurt you just as much as it is hurting me now) in the long term. 
Let me develop my point.
> First of all let me clarify for those who accuse me of being desinformed or 
innocent about my request, I'm not asking for your collaboration to remove what I 
published from the internet, its google records or any of the infinite copies that 
may be lying around. I'm asking for a very simple thing:
> There are 7 messages (sorry it wasn't 3...) written by me and hosted at the 
server stat.ethz.ch https://stat.ethz.ch/pipermail/r-help/2009-March/190367.html  
which I would like to have removed.
> Now, Steve E makes a good point: "I am also of the opinion that the list owner was 
not showing disrespect by describing the state of affairs you agreed to on signing up, or by 
declining to act beyond the requirements of the conditions applicable to the list. " 
Steve
> Fair enough. But that doesn't mean that those conditions are right and should never be 
modified. I'm probably something similar to an unhappy customer who has bought a product 
with no money-back policy but with an important distinction: I'm going to be wearing this 
product for the rest of my life. So that makes me, if anything, a "very unhappy 
customer".
> Now let me explain why in this world I'm spending time on requesting the 
removal of these 7 messages in that server.
> I have a MS in Computer Science and a 5 years long Telecommunications degree, 
I know quite well how the internet works. This is not the first time that I 
request this. I already requested it in another mailing list, where they were kind 
enough to aprove it after I verified my identity and they checked that they 
weren't removing anything critical. The result was that that piece information was 
obviously not erased from the entire internet but was not showing up in the first 
12 pages of google when you would look up my name (when it was on the first page 
previously). It took me 5 requests to different servers but I managed. There is 
nothing impossible about it and it made a differen

Re: [R] Translating recoding syntax from SPSS to R

2013-09-22 Thread Joshua Wiley
Just a slight addition to Arun's answer: within() saves typing and
makes life a bit easier

#
set.seed(429)
dat1 <- data.frame(
  race=sample(1:3,20,replace=TRUE),
  usborn=sample(0:2,20,replace=TRUE))

# within is a nice way to add variables
# and make modifications within a dataset
# saves typing lots of dat1$

dat1 <- within(dat1, {
 confused <- 1 * ((race==1 | race==2) & usborn==0)
})

head(dat1)

# only confused 0
dat1 <- subset(dat1, confused == 0)
#


On Sat, Sep 21, 2013 at 11:47 PM, arun  wrote:
> Hi,
> Try:
> set.seed(429)
> dat1<- 
> data.frame(race=sample(1:3,20,replace=TRUE),usborn=sample(0:2,20,replace=TRUE))
>  dat1$confused<- 1*((dat1$race==1|dat1$race==2) & dat1$usborn==0)
> head(dat1)
> #  race usborn confused
> #13  20
> #21  01
> #32  10
> #43  20
> #51  20
> #61  10
> A.K.
>
>
>
> - Original Message -
> From: Mosi Ifatunji 
> To: "r-help@r-project.org" 
> Cc:
> Sent: Saturday, September 21, 2013 6:03 PM
> Subject: [R] Translating recoding syntax from SPSS to R
>
> Colleagues,
>
> I am in the process of learning R. I've been able to import my dataset (from 
> Stata) and do some simple coding. I have now come to coding situation that 
> requires some assistance. This is some code in SPSS that I would like to be 
> able to execute in R:
>
> if (race eq 1 and usborn=0) confused=1 .
> if (race eq 2 and usborn=0) confused=1 .
> if (race eq 1 and usborn=1) confused=0 .
> if (race eq 2 and usborn=1) confused=0 .
> if (race eq 3 and usborn=1) confused=0 .
> if (race eq 3 and cohort=1) confused=0 .
> if (race eq 3 and cohort=2) confused=0 .
> variable labels confused "R claims to be both an African American and foriegn 
> born" .
> value labels confused
> 1 "Both AfAm and Foreign"
> 2 "Not" .
> select if (confused eq 0) .
>
> Any assistance would be greatly appreciated.
>
> -- Mosi
> __
> R-help@r-project.org 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.
>
>
> __
> R-help@r-project.org 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://joshuawiley.com/
Senior Analyst - Elkhart Group Ltd.
http://elkhartgroup.com

__
R-help@r-project.org 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.