[R] persp() problem

2007-09-10 Thread Economics Guy
I am having some trouble getting the persp() package to change the x
and y axis on a 3d plot. It defaults to the [0,1] interval and when I
try to change it I get errors.

Example:

This works:

D <- c(1,2,3,4,5,6,7,8,9,10)
M <- c(11,12,13,14,15,16,17,18,19,20)

DM <- cbind(D,M)

persp(DM, theta = 40, phi = 30, expand = 0.5, col = "lightblue",
  ltheta = 120, shade = 0.75, ticktype = "detailed",
  xlab = "X", ylab = "Y", zlab = "Z")
-


But I want the axis to count 1 by ones. So I try:
-
D <- c(1,2,3,4,5,6,7,8,9,10)
M <- c(11,12,13,14,15,16,17,18,19,20)

DM <- cbind(D,M)

x <- 1*0:10
y <- 1*0:20
persp(x,y,DM, theta = 40, phi = 30, expand = 0.5, col = "lightblue",
  ltheta = 120, shade = 0.75, ticktype = "detailed",
  xlab = "X", ylab = "Y", zlab = "Z")
-

I get:

Error in persp(x, y, z, xlim, ylim, zlim, theta, phi, r, d, scale,
expand,  : invalid 'z' argument

but the z was fine in the first version so I am not sure what the deal is.

Any ideas?

-Econ Guy

__
R-help@stat.math.ethz.ch 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] generating a new variable based on results of a by command

2007-06-21 Thread Economics Guy
Perfect!

EG

On 6/21/07, Dimitris Rizopoulos <[EMAIL PROTECTED]> wrote:
>
> maybe you want to use ave(), e.g.,
>
> f$sums <- ave(f$b, f$e, FUN = sum)
>
>
> I hope it helps.
>
> Best,
> Dimitris
>
> 
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/(0)16/336899
> Fax: +32/(0)16/337015
> Web: http://med.kuleuven.be/biostat/
>  http://www.student.kuleuven.be/~m0390867/dimitris.htm
>
>
> - Original Message -
> From: "Economics Guy" <[EMAIL PROTECTED]>
> To: 
> Sent: Thursday, June 21, 2007 3:53 PM
> Subject: [R] generating a new variable based on results of a by
> command
>
>
> >I have a matrix with a set of variables one of which is a factor.
> >Using by()
> > I have calculated something about each group (say the sum). Now I
> > want to
> > create a new variable in the original matrix that contains the
> > results of
> > the by() for each observation that is in the corresponding group.
> >
> > For example I have:
> >
> > -
> >
> > a <-c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
> >
> > b <-c(7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
> >
> > e
> > <-c("A","B","C","D","E","F","A","B","C","D","E","F","D","E","F","A")
> >
> > f <-data.frame(e,a,b)
> >
> > # Calculate sum by group
> >
> > sums <- by(f, e, function(x) sum(x$b))
> >
> > ---
> >
> > Now I would like to assign each observation in f a new variable
> > based on the
> > results of the by(). I converted sums into a matrix and then tried
> > using
> > match() and ifthen() but could not get it to work.
> >
> > Thanks,
> >
> > EG
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@stat.math.ethz.ch 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.
> >
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
>

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] generating a new variable based on results of a by command

2007-06-21 Thread Economics Guy
I have a matrix with a set of variables one of which is a factor. Using by()
I have calculated something about each group (say the sum). Now I want to
create a new variable in the original matrix that contains the results of
the by() for each observation that is in the corresponding group.

For example I have:

-

a <-c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)

b <-c(7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)

e <-c("A","B","C","D","E","F","A","B","C","D","E","F","D","E","F","A")

f <-data.frame(e,a,b)

# Calculate sum by group

sums <- by(f, e, function(x) sum(x$b))

---

Now I would like to assign each observation in f a new variable based on the
results of the by(). I converted sums into a matrix and then tried using
match() and ifthen() but could not get it to work.

Thanks,

EG

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Storing output vector form a loop as a matrix

2007-06-17 Thread Economics Guy
Thanks so much to both of you. I did not exactly use the code from either of
you but your post help me figure out the things I was doing wrong.

For anyone who stumbles upon this thread later, here is the code I used that
made it work:

Thanks Again.


a <-c("A","C","B","A","B","C")

b <-c(10,20,30,40,50,60)

d <-data.frame(a,b)

N=10 #Number of Loops

output.matrix <- matrix(0.0,3,N)

#I need to START LOOP HERE

for (i in 1:N){

a <- sample(a, replace=FALSE)

d <-data.frame(b,a)

output.vector <- by(d, a, function(x) sum(x$b))

output.vector <- as.vector(output.vector)

output.vector <- data.frame(output.vector)

output.vector <- as.vector(output.vector) #test

output.matrix[,i] <- t(output.vector)

}




On 6/17/07, jim holtman <[EMAIL PROTECTED]> wrote:
>
> I think this does it for you:
>
> > a <-c("A","C","B","A","B","C")
> >
> > b <-c(10,20,30,40,50,60)
> >
> > C <-data.frame(a,b)  # don't use 'c' as it is a primitive function -
> canbe confusing
> >
> > N=10 #Number of Loops
> >
> >
> >
> > output.matix <- replicate(N, function(x){
> + .samp <- C[sample(nrow(C), replace=TRUE), ]
> + tapply(.samp$b, .samp$a, sum)
> + })
> > # transpose if you want samples in columns
> > output.matrix <- t(output.matrix)
> > output.matrix
>   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> A   10  130  100   40   50   40   30   50   5050
> B  130   60   50  110  100  160  110  130   60   150
> C   40   NA   60   80  120   60   NA   60   8020
> >
>
>
>
> On 6/17/07, Economics Guy <[EMAIL PROTECTED]> wrote:
>
> > Based on help files and searching the archives and help from the
> > listserv I
> > have managed to build my monte carlo program.
> >
> > However I cannot get the proper syntax for running the loop and storing
> > the
> > output of each loop (which is a vector) into a matrix.
> >
> > I took out some of the crazy code I was writing, but here is what I
> > have:
> >
> > 
> >
> > rm(list = ls(all = TRUE))
> > # removes everything
> >
> > a <-c("A","C","B","A","B","C")
> >
> > b <-c(10,20,30,40,50,60)
> >
> > c <-data.frame(a,b)
> >
> > N=10 #Number of Loops
> >
> > output.matrix <- matrix(0.0,3,N)
> >
> > #I need to START LOOP HERE
> >
> > a <- sample(a, replace=TRUE)
> >
> > c <-data.frame(b,a)
> >
> > output.vector <- by(c, a, function(x) sum(x$b))
> >
> > output.vector <- as.vector(output)
> >
> > output.vector <- data.frame(output)
> >
> > #END LOOP here
> >
> >
> > --
> >
> > What I would like to have at the end is the " output.matrix" contain as
> > a
> > column the  "output.vector" from  each iteration. The actual data frame
> > I
> > will be running has 60,000 observations and I am going to run 2
> > iterations so speed is important too.
> >
> > Thanks so much
> >
> > EG
> >
> >[[alternative HTML version deleted]]
> >
> > __
> > R-help@stat.math.ethz.ch 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.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Storing output vector form a loop as a matrix

2007-06-17 Thread Economics Guy
That gives:

Error in output.matrix[, i] <- as.vector(output.vector) :
number of items to replace is not a multiple of replacement length

But I think that is close.

Thanks,

EG

On 6/17/07, David Barron <[EMAIL PROTECTED]> wrote:
>
> I'm not sure I've completely understood this, but would this do what you
> want:
>
>
> for (i in 1:N){
>
> aa <- sample(a, replace=TRUE)
>
> c <-data.frame(b,aa)
>
> output <- by(c, aa, function(x) sum(x$b))
>
> output.matrix[,i] <- as.vector(output)
> }
>
>
>
> On 17/06/07, Economics Guy <[EMAIL PROTECTED]> wrote:
> > Based on help files and searching the archives and help from the
> listserv I
> > have managed to build my monte carlo program.
> >
> > However I cannot get the proper syntax for running the loop and storing
> the
> > output of each loop (which is a vector) into a matrix.
> >
> > I took out some of the crazy code I was writing, but here is what I
> have:
> >
> > 
> >
> > rm(list = ls(all = TRUE))
> > # removes everything
> >
> > a <-c("A","C","B","A","B","C")
> >
> > b <-c(10,20,30,40,50,60)
> >
> > c <-data.frame(a,b)
> >
> > N=10 #Number of Loops
> >
> > output.matrix <- matrix(0.0,3,N)
> >
> > #I need to START LOOP HERE
> >
> > a <- sample(a, replace=TRUE)
> >
> > c <-data.frame(b,a)
> >
> > output.vector <- by(c, a, function(x) sum(x$b))
> >
> > output.vector <- as.vector(output)
> >
> > output.vector <- data.frame(output)
> >
> > #END LOOP here
> >
> >
> > --
> >
> > What I would like to have at the end is the "output.matrix" contain as a
> > column the  "output.vector" from  each iteration. The actual data frame
> I
> > will be running has 60,000 observations and I am going to run 2
> > iterations so speed is important too.
> >
> > Thanks so much
> >
> > EG
> >
> > [[alternative HTML version deleted]]
> >
> > __
> > R-help@stat.math.ethz.ch 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.
> >
>
>
> --
> =
> David Barron
> Said Business School
> University of Oxford
> Park End Street
> Oxford OX1 1HP
>

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Storing output vector form a loop as a matrix

2007-06-17 Thread Economics Guy
Based on help files and searching the archives and help from the listserv I
have managed to build my monte carlo program.

However I cannot get the proper syntax for running the loop and storing the
output of each loop (which is a vector) into a matrix.

I took out some of the crazy code I was writing, but here is what I have:



rm(list = ls(all = TRUE))
# removes everything

a <-c("A","C","B","A","B","C")

b <-c(10,20,30,40,50,60)

c <-data.frame(a,b)

N=10 #Number of Loops

output.matrix <- matrix(0.0,3,N)

#I need to START LOOP HERE

a <- sample(a, replace=TRUE)

c <-data.frame(b,a)

output.vector <- by(c, a, function(x) sum(x$b))

output.vector <- as.vector(output)

output.vector <- data.frame(output)

#END LOOP here


--

What I would like to have at the end is the "output.matrix" contain as a
column the  "output.vector" from  each iteration. The actual data frame I
will be running has 60,000 observations and I am going to run 2
iterations so speed is important too.

Thanks so much

EG

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Use of the "by" command (clarification)

2007-06-16 Thread Economics Guy
Well apparently this has nothing to do with the gini() command.

I cannot get it to work for something as simple as sum()

Here is the little example I am playing with, maybe someone can help me find
my error:

a<-c("A","B","C","A","B","C","A","A","C","B")

b<-c(23,6534,456,234,7,567,345,9,565,345)

c<-cbind(a,b)

by(c, a, function(x) sum(b))

and I get the output

INDICES: A
[1] 9085

INDICES: B
[1] 9085
--
INDICES: C
[1] 9085


Same problem as before. It is summing over the whole b vector rather than by
the groups.

Anybody have any ideas on what I am doing wrong?


Thanks,

EG

On 6/16/07, Economics Guy <[EMAIL PROTECTED]> wrote:
>
> I have a data set that contains income data and a group identifier. Sort
> of like:
>
>
>DATA
>
> Group,Income
> A,2300
> B,6776
> A,6668
> A,6768
> B,9879
> C,5577
> A,7867
> (etc),(etc)
>
> I am trying to compute the gini coefficient for each group.
>
> I have tried the following and none seem to do the trick:
>
> 1)
>
> attach(DATA)
>
> by(DATA, group, function(x) gini(income))
>
>
> 2)
>
> attach(data)
>
> tapply(income, group, function(x) gini(income))
>
> Both of these return the same value for all groups. Like:
>
> group: A
> [1] 0.2422496
> 
> group: B
> [1] 0.2422496
> 
> group: C
> [1] 0.2422496
> 
> group: D
> [1] 0.2422496
>
> Any ideas on how I can make this work? I need the fastest way since I am
> gonna run a monte carlo based on this routine once I get the basics working.
>
>
> Thanks,
>
> EG

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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] Use of the "by" command for gini()

2007-06-16 Thread Economics Guy
I have a data set that contains income data and a group identifier. Sort of
like:


   DATA

Group,Income
A,2300
B,6776
A,6668
A,6768
B,9879
C,5577
A,7867
(etc),(etc)

I am trying to compute the gini coefficient for each group.

I have tried the following and none seem to do the trick:

1)

attach(DATA)

by(DATA, group, function(x) gini(income))


2)

attach(data)

tapply(income, group, function(x) gini(income))

Both of these return the same value for all groups. Like:

group: A
[1] 0.2422496

group: B
[1] 0.2422496

group: C
[1] 0.2422496

group: D
[1] 0.2422496

Any ideas on how I can make this work? I need the fastest way since I am
gonna run a monte carlo based on this routine once I get the basics working.

Thanks,

EG

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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.