Re: [R] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread Joshua Wiley
Hi,

Here is a little function that will do what you want and return a nice output:

#Function To calculate top two values and return
my.finder - function(mydata) {
  my.fun - function(data) {
strongest - which.max(data)
secondstrongest - which.max(data[-strongest])
strongestantenna - names(data)[strongest]
secondstrongantenna - names(data[-strongest])[secondstrongest]
value - matrix(c(data[strongest], data[secondstrongest],
  strongestantenna, secondstrongantenna), ncol =4)
return(value)
  }
  dat - apply(mydata, 1, my.fun)
  dat - t(dat)
  dat - as.data.frame(dat, stringsAsFactors = FALSE)
  colnames(dat) - c(strongest, secondstrongest,
 strongestantenna, secondstrongantenna)
  dat[ , strongest] - as.numeric(dat[ , strongest])
  dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
  return(dat)
}


#Using your example data:

yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L, -12805L,
-12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 = c(-11707L,
-13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,
-11736L, -12873L), value120 = c(-11072L, -11176L, -3L, -11071L,
-11067L, -12430L, -11092L, -11061L, -11137L, -12973L), value180 = c(-12471L,
-11799L, -12439L, -12385L, -11638L, -12430L, -11709L, -12373L,
-12570L, -12537L), value240 = c(-12838L, -13210L, -13089L, -11561L,
-13527L, -12430L, -11607L, -11426L, -13467L, -12973L), value300 = c(-13357L,
-13845L, -13880L, -13317L, -13873L, -12814L, -13025L, -12805L,
-13739L, -11146L)), .Names = c(value0, value60, value120,
value180, value240, value300), class = data.frame, row.names = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10))

my.finder(yourdata) #and what you want is in a nicely labeled data frame

#A potential problem is that it is not very efficient

#Here is a test using a matrix of 100,000 rows
#sampled from the same range as your data
#with the same number of columns

data.test - matrix(
 sample(seq(min(yourdata),max(yourdata)), size = 50, replace = TRUE),
 ncol = 5)

system.time(my.finder(data.test))

#On my system I get

 system.time(my.finder(data.test))
   user  system elapsed
   2.890.002.89

Hope that helps,

Josh



On Fri, Jul 23, 2010 at 6:20 PM,  mpw...@illinois.edu wrote:
 I have a data frame with a couple million lines and want to retrieve the 
 largest and second largest values in each row, along with the label of the 
 column these values are in. For example

 row 1
 strongest=-11072
 secondstrongest=-11707
 strongestantenna=value120
 secondstrongantenna=value60

 Below is the code I am using and a truncated data.frame.  Retrieving the 
 largest value was easy, but I have been getting errors every way I have tried 
 to retrieve the second largest value.  I have not even tried to retrieve the 
 labels for the value yet.

 Any help would be appreciated
 Mike


 data-data.frame(value0,value60,value120,value180,value240,value300)
 data
   value0 value60 value120 value180 value240 value300
 1  -13007  -11707   -11072   -12471   -12838   -13357
 2  -12838  -13210   -11176   -11799   -13210   -13845
 3  -12880  -11778   -3   -12439   -13089   -13880
 4  -12805  -11653   -11071   -12385   -11561   -13317
 5  -12834  -13527   -11067   -11638   -13527   -13873
 6  -11068  -11698   -12430   -12430   -12430   -12814
 7  -12807  -14068   -11092   -11709   -11607   -13025
 8  -12770  -11665   -11061   -12373   -11426   -12805
 9  -12988  -11736   -11137   -12570   -13467   -13739
 10 -11779  -12873   -12973   -12537   -12973   -11146
 #largest value in the row
 strongest-apply(data,1,max)


 #second largest value in the row
 n-function(data)(1/(min(1/(data[1,]-max(data[1,]+ (max(data[1,])))
 secondstrongest-apply(data,1,n)
 Error in data[1, ] : incorrect number of dimensions


 __
 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://www.joshuawiley.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] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread Joshua Wiley
Maybe efficiency is less of an issue than I thought, on 2 million
rows, it only took a bit over a minute, and my system only jumped up ~
600 MB of memory so it wasn't much of a strain there either.

 data.test - matrix(
+  sample(seq(min(yourdata),max(yourdata)), size = 1000, replace = TRUE),
+  ncol = 5)
 nrow(data.test)
[1] 200
 system.time(my.finder(data.test))
   user  system elapsed
  74.540.20   75.20

On Fri, Jul 23, 2010 at 11:01 PM, Joshua Wiley jwiley.ps...@gmail.com wrote:
 Hi,

 Here is a little function that will do what you want and return a nice output:

 #Function To calculate top two values and return
 my.finder - function(mydata) {
  my.fun - function(data) {
    strongest - which.max(data)
    secondstrongest - which.max(data[-strongest])
    strongestantenna - names(data)[strongest]
    secondstrongantenna - names(data[-strongest])[secondstrongest]
    value - matrix(c(data[strongest], data[secondstrongest],
                      strongestantenna, secondstrongantenna), ncol =4)
    return(value)
  }
  dat - apply(mydata, 1, my.fun)
  dat - t(dat)
  dat - as.data.frame(dat, stringsAsFactors = FALSE)
  colnames(dat) - c(strongest, secondstrongest,
                     strongestantenna, secondstrongantenna)
  dat[ , strongest] - as.numeric(dat[ , strongest])
  dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
  return(dat)
 }


 #Using your example data:

 yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L, -12805L,
 -12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 = c(-11707L,
 -13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,
 -11736L, -12873L), value120 = c(-11072L, -11176L, -3L, -11071L,
 -11067L, -12430L, -11092L, -11061L, -11137L, -12973L), value180 = c(-12471L,
 -11799L, -12439L, -12385L, -11638L, -12430L, -11709L, -12373L,
 -12570L, -12537L), value240 = c(-12838L, -13210L, -13089L, -11561L,
 -13527L, -12430L, -11607L, -11426L, -13467L, -12973L), value300 = c(-13357L,
 -13845L, -13880L, -13317L, -13873L, -12814L, -13025L, -12805L,
 -13739L, -11146L)), .Names = c(value0, value60, value120,
 value180, value240, value300), class = data.frame, row.names = c(1,
 2, 3, 4, 5, 6, 7, 8, 9, 10))

 my.finder(yourdata) #and what you want is in a nicely labeled data frame

 #A potential problem is that it is not very efficient

 #Here is a test using a matrix of 100,000 rows
 #sampled from the same range as your data
 #with the same number of columns

 data.test - matrix(
  sample(seq(min(yourdata),max(yourdata)), size = 50, replace = TRUE),
  ncol = 5)

 system.time(my.finder(data.test))

 #On my system I get

 system.time(my.finder(data.test))
   user  system elapsed
   2.89    0.00    2.89

 Hope that helps,

 Josh



 On Fri, Jul 23, 2010 at 6:20 PM,  mpw...@illinois.edu wrote:
 I have a data frame with a couple million lines and want to retrieve the 
 largest and second largest values in each row, along with the label of the 
 column these values are in. For example

 row 1
 strongest=-11072
 secondstrongest=-11707
 strongestantenna=value120
 secondstrongantenna=value60

 Below is the code I am using and a truncated data.frame.  Retrieving the 
 largest value was easy, but I have been getting errors every way I have 
 tried to retrieve the second largest value.  I have not even tried to 
 retrieve the labels for the value yet.

 Any help would be appreciated
 Mike


 data-data.frame(value0,value60,value120,value180,value240,value300)
 data
   value0 value60 value120 value180 value240 value300
 1  -13007  -11707   -11072   -12471   -12838   -13357
 2  -12838  -13210   -11176   -11799   -13210   -13845
 3  -12880  -11778   -3   -12439   -13089   -13880
 4  -12805  -11653   -11071   -12385   -11561   -13317
 5  -12834  -13527   -11067   -11638   -13527   -13873
 6  -11068  -11698   -12430   -12430   -12430   -12814
 7  -12807  -14068   -11092   -11709   -11607   -13025
 8  -12770  -11665   -11061   -12373   -11426   -12805
 9  -12988  -11736   -11137   -12570   -13467   -13739
 10 -11779  -12873   -12973   -12537   -12973   -11146
 #largest value in the row
 strongest-apply(data,1,max)


 #second largest value in the row
 n-function(data)(1/(min(1/(data[1,]-max(data[1,]+ (max(data[1,])))
 secondstrongest-apply(data,1,n)
 Error in data[1, ] : incorrect number of dimensions


 __
 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://www.joshuawiley.com/




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

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

Re: [R] , Updating Table

2010-07-24 Thread Linlin Yan
If I am not wrong, it seems that you want to get factor counts in the
whole scale of data.raw. Maybe you can do that just like this:
table(data.raw)

On Sat, Jul 24, 2010 at 1:44 AM, Marcus Liu marcusliu...@yahoo.com wrote:
 Hi everyone,

 Is there any command for updating table withing a loop?  For instance, at i, 
 I have a table as ZZ = table(data.raw[1:ind[i]]) where ind = c(10, 20, 30, 
 ...).  Then , ZZ will be as follow

 A B C
  3    10   2

 At (i + 1), ZZ = table(data.raw[(ind[i]+1):ind[i+1]])

 A B D
  4    7    8

 Is there any command that can update the table ZZ for each time so that in 
 the above example, ZZ will be

 A B C D
  7    17   2    8

 Thanks.

 liu




        [[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] union data in column

2010-07-24 Thread Jeff Newmiller

Fahim Md wrote:

Is there any function/way to merge/unite the following data

  GENEID  col1  col2 col3col4
  G234064 1 0  0   0
  G234064 1 0  0   0
  G234064 1 0  0   0
  G234064 0 1  0   0
  G234065 0 1  0   0
  G234065 0 1  0   0
  G234065 0 1  0   0
  G234065 0 0  1   0
  G234065 0 0  1   0
  G234065 0 0  0   1


into
GENEID  col1  col2 col3col4
  G234064 1 1  0   0
// 1 appears in col1 and col2 above, rest are zero
  G234065 0 1  1   1
// 1 appears in col2 , 3 and 4 above.


Thank


Warning on terminology: there is a merge function in R that lines up 
rows from different tables to make a new set of longer rows (more 
columns). The usual term for combining column values from multiple rows 
is aggregation.


In addition to the example offered by Jim Holtzman, here are some other 
options in no particular order:


x - read.table(textConnection( GENEID col1 col2 col3 col4
G234064 1 0 0 0
G234064 1 0 0 0
G234064 1 0 0 0
G234064 0 1 0 0
G234065 0 1 0 0
G234065 0 1 0 0
G234065 0 1 0 0
G234065 0 0 1 0
G234065 0 0 1 0
G234065 0 0 0 1
), header=TRUE, as.is=TRUE, row.names=NULL)
closeAllConnections()

# syntactic repackaging of Jim's basic approach
library(plyr)
ddply( x, .(GENEID), function(df) 
{with(as.integer(c(col1=any(col1),col2=any(col2),col3=any(col3),col4=any(col4} 
)


# if you are familiar with SQL, this approach allows you merge data 
frames and aggregate rows in the same step, but has a somewhat limited 
range of aggregating functions available

library(sqldf)
sqldf(SELECT GENEID, max(col1) AS col1, max(col2) AS col2, max(col3) AS 
col3, max(col4) AS col4 FROM x GROUP BY GENEID)


# when the data you want to crunch is separate from the key(s) or you 
can separate them yourself

crunch - function(tmp){as.integer(any(tmp))}
aggregate( x[,c(col1,col2,col3,col4)], by=list(x$GENEID), 
FUN=crunch )


# this is typically used if you want to aggregate many columns with the 
same set of operations

library(doBy)
summaryBy( .~GENEID, data=x, FUN=c(sum,crunch) )

__
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] How to import simple java/mathematica expression to R

2010-07-24 Thread Andrey Siver
Hello,

2010/7/24 jim holtman jholt...@gmail.com:
 Well, I took you equation and put the following at the start:

 Power - function(x,y) x^y
 EE - function(x) x
 alp - 2

 x - 900*Power(-0.2030178326474623 + 0.23024073983368956*(1 - alp) +
...

Actually, there are many functions like this (about 18*2), for each
situation with input data.

So there is an idea to put them in different files (via Export from
Mathematica) and then use them somehow (without modifications) in R
program.

Thank You for the answer.

 --
Regards,
    -Andrey

__
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] How to import simple java/mathematica expression to R

2010-07-24 Thread Andrey Siver
2010/7/24 David Winsemius dwinsem...@comcast.net:

 On Jul 23, 2010, at 10:29 PM, jim holtman wrote:

 It has some interesting properties with that identity definition of EE(x).
 Local maxima at 1 and 0, blows up beyond -1 and 2, and several local
 minima nearby:

 Math.Fn - function(alp) { big-long-expression }
 plot( seq(-.3,1.5,by=0.01), Math.Fn(seq(-.3,1.5,by=0.01) ), cex=0.2)


Indeed, EE(x) is not a function with real x, but a symbol with
subscript - i.e. EE(1) and EE(2) are two variables e1 and e2.


Thanks for Your interest.

-- 
Regards,

   -Andrey

__
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] Understanding Prototype of class

2010-07-24 Thread Christofer Bogaso
Hi R gurus, here I am trying to understand how R define and work with the
prototype property in class definition. Here I have created a class:

 setClass(a, representation=list(x=numeric, y=character))
[1] a


As I did not explicitly define the prototype in above code, R is supposed to
generate default. I wanted to see what R generated:

 prototype(a)
An object of class classPrototypeDef
Slot object:
[1] a

Slot slots:
character(0)

Slot dataPart:
[1] TRUE

I really could not understand the meaning which R returned. I understood
part of above (please correct me if I am wrong):

An object of class classPrototypeDef : I understood R treats the
prototype of any class, as an object of class classPrototypeDef
Slot object: : I believe under this heading, R shows the name of the
underlying class, whose prototype I am looking for
Slot slots: : I really could not understand what this meant for
Slot dataPart: : again I really could not understand what this meant for

Can anyone please help me to understand above clearly?

Thanks and regards,

[[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] interpretation of stress in NMDS

2010-07-24 Thread Thomas Petzoldt

On 7/22/2010 2:21 PM, Graves, Gregory wrote:

Among those users of Primer, stress values greater than 0.3 are
interpreted as questionable.  Using both isoMDS and metaMDS (vegan
package), the stress values returned are much higher using my own data
and using examples provided in R Help.  For example Rstress = 8.3, and
the stressplot r2 = 0.99 indicating (to me) that the ordination is OK.
I am guessing that the stress values across packages are not the same,
and googling about has not returned a satisfactory answer ... thus this
posting.  My concern being that reporting a stress value of 8 for what I
consider a satisfactory result may raise a few Primer-user's eyebrows.


Dear Gregory,

as the help file of ?isoMDS tells us:

stress   The final stress achieved (in percent)

So 8.3 is in reality 0.083.

Thomas Petzoldt

__
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] how to calculate the product of every two elements in two vectors

2010-07-24 Thread Dennis Murphy
as.vector(t(outer(A, B)))
[1]  9 10 11 12 18 20 22 24 27 30 33 36

HTH,
Dennis

On Fri, Jul 23, 2010 at 8:11 AM, aegea gche...@gmail.com wrote:


 Thanks in advance!

 A=c(1, 2,3)
 B=c (9, 10, 11, 12)

 I want to get C=c(1*9, 1*10, 1*11, 1*12, ., 3*9, 3*10, 3*11, 3*12)?
 C is still a vector with 12 elements
 Is there a way to do that?
 --
 View this message in context:
 http://r.789695.n4.nabble.com/how-to-calculate-the-product-of-every-two-elements-in-two-vectors-tp2300299p2300299.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.


[[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] Question regarding panel data diagnostic

2010-07-24 Thread Setlhare Lekgatlhamang
My thought is this:
It depends on what you have in the panel. Are your data cross-section data 
observed over ten years for, say, 3 countries (or regions within the same 
country)? If so, yes you can perform integration properties (what people 
usually call unit root test) and then test for cointegration. But if the data 
are quarterly or monthly, these techniques are not relevant.

Hope this helps.
Lexi

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of amatoallah ouchen
Sent: Friday, July 23, 2010 12:18 AM
To: r-help@r-project.org
Subject: [R] Question regarding panel data diagnostic

Good day R-listers,
I'm currently working on a panel data analysis (N=17, T=5), in order
to check for the spurious regression problem, i have to  test for
stationarity but i've read somewhere  that i needn't to test for it as
 my T10 , what do you think? if yes  is there any other test  i have
to  perform in such case (a kind of cointegration test for small T?)

Any hint would be highly appreciated.

Ama.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

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



DISCLAIMER:\ Sample Disclaimer added in a VBScript.\ ...{{dropped:3}}

__
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] Question regarding panel data diagnostic

2010-07-24 Thread Setlhare Lekgatlhamang
Let me correct an omission in my response below. The last sentence
should read But if the data are 10 quarterly or monthly values, these
techniques are not relevant.

Cheers
Lexi

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Setlhare Lekgatlhamang
Sent: Saturday, July 24, 2010 12:54 PM
To: amatoallah ouchen; r-help@r-project.org
Subject: Re: [R] Question regarding panel data diagnostic

My thought is this:
It depends on what you have in the panel. Are your data cross-section
data observed over ten years for, say, 3 countries (or regions within
the same country)? If so, yes you can perform integration properties
(what people usually call unit root test) and then test for
cointegration. But if the data are quarterly or monthly, these
techniques are not relevant.

Hope this helps.
Lexi

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of amatoallah ouchen
Sent: Friday, July 23, 2010 12:18 AM
To: r-help@r-project.org
Subject: [R] Question regarding panel data diagnostic

Good day R-listers,
I'm currently working on a panel data analysis (N=17, T=5), in order
to check for the spurious regression problem, i have to  test for
stationarity but i've read somewhere  that i needn't to test for it as
 my T10 , what do you think? if yes  is there any other test  i have
to  perform in such case (a kind of cointegration test for small T?)

Any hint would be highly appreciated.

Ama.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

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



DISCLAIMER:\ Sample Disclaimer added in a VBScript.\ ..{{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] Question regarding panel data diagnostic

2010-07-24 Thread Setlhare Lekgatlhamang

Let me correct an omission in my response below. The last sentence
should read But if the data are 10 quarterly or monthly values, these
techniques are not relevant.

Cheers
Lexi

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Setlhare Lekgatlhamang
Sent: Saturday, July 24, 2010 12:54 PM
To: amatoallah ouchen; r-help@r-project.org
Subject: Re: [R] Question regarding panel data diagnostic

My thought is this:
It depends on what you have in the panel. Are your data cross-section
data observed over ten years for, say, 3 countries (or regions within
the same country)? If so, yes you can perform integration properties
(what people usually call unit root test) and then test for
cointegration. But if the data are quarterly or monthly, these
techniques are not relevant.

Hope this helps.
Lexi

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of amatoallah ouchen
Sent: Friday, July 23, 2010 12:18 AM
To: r-help@r-project.org
Subject: [R] Question regarding panel data diagnostic

Good day R-listers,
I'm currently working on a panel data analysis (N=17, T=5), in order
to check for the spurious regression problem, i have to  test for
stationarity but i've read somewhere  that i needn't to test for it as
 my T10 , what do you think? if yes  is there any other test  i have
to  perform in such case (a kind of cointegration test for small T?)

Any hint would be highly appreciated.

Ama.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

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



DISCLAIMER:\ Sample Disclaimer added in a VBScript.\ ..{{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] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread David Winsemius


On Jul 23, 2010, at 9:20 PM, mpw...@illinois.edu wrote:

I have a data frame with a couple million lines and want to retrieve  
the largest and second largest values in each row, along with the  
label of the column these values are in. For example


row 1
strongest=-11072
secondstrongest=-11707
strongestantenna=value120
secondstrongantenna=value60

Below is the code I am using and a truncated data.frame.  Retrieving  
the largest value was easy, but I have been getting errors every way  
I have tried to retrieve the second largest value.  I have not even  
tried to retrieve the labels for the value yet.


Any help would be appreciated
Mike
Using Holtman's extract of your data with x as the name and the order  
function to generate an index to names of the dataframe:

 t(apply(x, 1, sort, decreasing=TRUE)[1:3, ])
 [,1]   [,2]   [,3]
1  -11072 -11707 -12471
2  -11176 -11799 -12838
3  -3 -11778 -12439
4  -11071 -11561 -11653
5  -11067 -11638 -12834
6  -11068 -11698 -12430
7  -11092 -11607 -11709
8  -11061 -11426 -11665
9  -11137 -11736 -12570
10 -11146 -11779 -12537

Putting it all together:

 matrix( paste( t(apply(x, 1, sort, decreasing=TRUE)[1:3, ]),
names(x)[ t(apply(x, 1, order, decreasing=TRUE) 
[1:3, ]) ]),

 ncol=3)

  [,1]  [,2]  [,3]
 [1,] -11072 value120 -11707 value60  -12471 value180
 [2,] -11176 value120 -11799 value180 -12838 value0
 [3,] -3 value120 -11778 value60  -12439 value180
 [4,] -11071 value120 -11561 value240 -11653 value60
 [5,] -11067 value120 -11638 value180 -12834 value0
 [6,] -11068 value0   -11698 value60  -12430 value120
 [7,] -11092 value120 -11607 value240 -11709 value180
 [8,] -11061 value120 -11426 value240 -11665 value60
 [9,] -11137 value120 -11736 value60  -12570 value180
[10,] -11146 value300 -11779 value0   -12537 value180

--
David.





data-data.frame(value0,value60,value120,value180,value240,value300)
data

  value0 value60 value120 value180 value240 value300
1  -13007  -11707   -11072   -12471   -12838   -13357
2  -12838  -13210   -11176   -11799   -13210   -13845
3  -12880  -11778   -3   -12439   -13089   -13880
4  -12805  -11653   -11071   -12385   -11561   -13317
5  -12834  -13527   -11067   -11638   -13527   -13873
6  -11068  -11698   -12430   -12430   -12430   -12814
7  -12807  -14068   -11092   -11709   -11607   -13025
8  -12770  -11665   -11061   -12373   -11426   -12805
9  -12988  -11736   -11137   -12570   -13467   -13739
10 -11779  -12873   -12973   -12537   -12973   -11146

#largest value in the row
strongest-apply(data,1,max)


#second largest value in the row
n-function(data)(1/(min(1/(data[1,]-max(data[1,]+  
(max(data[1,])))

secondstrongest-apply(data,1,n)

Error in data[1, ] : incorrect number of dimensions




__
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] local polynomial with differnt kernal functions

2010-07-24 Thread assaedi76 assaedi76
Hi, R users
 
I need to use the function (locpoly) to fit a local poynomial regression model, 
The defult for kernal function is  normal , but  I need to use different 
kernal functions such as :Uniform,Triangular,Epanechnikov,..
Could someone help me define these functions to fit local polynomial regression 
model?.
Email:assaed...@yahoo.com
 
 
Thanks alot


  
[[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] Weights in mixed models

2010-07-24 Thread David R.
Hello everyone,

I wonder if sample size can be used as weight  in a weighted mixed model. Or 
should I use just the inverse of the variance?

For example, the class'lmer' in the 'lme4' package have an option 'weights'
(as in the class 'lm' of 'stats'). In the help of lme4 there is an example 
using 
'herd size' as weight in a mixed model.
 
 So, if I have a measurement data (eg height) of 10 groups (sample size ranging 
from 30 to 3000 individuals for each group) can I  use this number (N, sample 
size) in the 'weights' option? Is this wrong? 

 
Finally, what to do if the results (coefficients) of weighing by 'inverse of 
the 
variance' or by 'sample size' are very different, even opposite?


Thank you very much in advance
David

__
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] Odp: Help me with prediction in linear model

2010-07-24 Thread Research student

Thanks Murphy and pikal,

I need another help,for fitting first fourier transformation ,i used
following thing .Please advise on this


beer_monthl has 400+ records

EXample:
 head(beer_monthly)
  beer
1 93.2
2 96.0
3 95.2
4 77.1
5 70.9
6 64.8




time-seq(1956,1995.2,length=length(beer_monthly))
sin.t-sin(2*pi*time)
cos.t-cos(2*pi*time)
beer_fit_fourier=lm(beer_monthly[,1]~poly(time,2)+sin.t+cos.t) #this is not
working
beer_fit_fourier=lm(beer_monthly[,1]~time+time2+sin.t+cos.t) #it is working


#prediction is not workinng

tpred_four - data.frame(time = seq(1995, 1998, length = 20))
predict(beer_fit_fourier, newdata = tpred_four)

Is there any way to fit first fourier  frequency ,

Please assist.

Thanks in advance





-- 
View this message in context: 
http://r.789695.n4.nabble.com/Help-me-with-prediction-in-linear-model-tp2297313p2300991.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] union data in column

2010-07-24 Thread Hadley Wickham
On Sat, Jul 24, 2010 at 2:23 AM, Jeff Newmiller
jdnew...@dcn.davis.ca.us wrote:
 Fahim Md wrote:

 Is there any function/way to merge/unite the following data

  GENEID      col1          col2             col3                col4
  G234064         1             0                  0                   0
  G234064         1             0                  0                   0
  G234064         1             0                  0                   0
  G234064         0             1                  0                   0
  G234065         0             1                  0                   0
  G234065         0             1                  0                   0
  G234065         0             1                  0                   0
  G234065         0             0                  1                   0
  G234065         0             0                  1                   0
  G234065         0             0                  0                   1


 into
 GENEID      col1          col2             col3                col4
  G234064         1             1                  0                   0
 // 1 appears in col1 and col2 above, rest are zero
  G234065         0             1                  1                   1
 // 1 appears in col2 , 3 and 4 above.


 Thank

 Warning on terminology: there is a merge function in R that lines up rows
 from different tables to make a new set of longer rows (more columns). The
 usual term for combining column values from multiple rows is aggregation.

 In addition to the example offered by Jim Holtzman, here are some other
 options in no particular order:

 x - read.table(textConnection( GENEID col1 col2 col3 col4
 G234064 1 0 0 0
 G234064 1 0 0 0
 G234064 1 0 0 0
 G234064 0 1 0 0
 G234065 0 1 0 0
 G234065 0 1 0 0
 G234065 0 1 0 0
 G234065 0 0 1 0
 G234065 0 0 1 0
 G234065 0 0 0 1
 ), header=TRUE, as.is=TRUE, row.names=NULL)
 closeAllConnections()

 # syntactic repackaging of Jim's basic approach
 library(plyr)
 ddply( x, .(GENEID), function(df)
 {with(as.integer(c(col1=any(col1),col2=any(col2),col3=any(col3),col4=any(col4}
 )

You can do this a little more succinctly with colwise:

any_1 - function(x) as.integer(any(x))
ddply(x, GENEID, numcolwise(any_1))

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
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] UseR! 2010 - my impressions

2010-07-24 Thread Frank E Harrell Jr

On 07/23/2010 06:50 PM, Ravi Varadhan wrote:

Dear UseRs!,

Everything about UseR! 2010 was terrific!  I really mean everything - the 
tutorials, invited talks, kaleidoscope sessions, focus sessions, breakfast, snacks, 
lunch, conference dinner, shuttle services, and the participants. The organization was 
fabulous.  NIST were gracious hosts, and provided top notch facilities.  The rousing 
speech by Antonio Possolo, who is the chief of Statistical Engineering Division at NIST, 
set the tempo for the entire conference.  Excellent invited lectures by Luke Tierney, 
Frank Harrell, Mark Handcock, Diethelm Wurtz, Uwe Ligges, and Fritz Leisch.  All the 
sessions that I attended had many interesting ideas and useful contributions.  During the 
whole time that I was there, I could not help but get the feeling that I am a part of 
something great.

Before I end, let me add a few words about a special person.  This conference 
would not have been as great as it was without the tireless efforts of Kate 
Mullen.  The great thing about Kate is that she did so much without ever 
hogging the limelight.  Thank you, Kate and thank you NIST!

I cannot wait for UseR!2011!

Best,
Ravi.



Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology
School of Medicine
Johns Hopkins University

Ph. (410) 502-2619
email: rvarad...@jhmi.edu


I want to echo what Ravi said.  The talks were terrific (thanks to the 
program committee and the speakers) and Kate Mullen and her team did an 
extraordinary job in putting the conference together and running it.  I 
am proud to have been a part of it.  Thank you all!


Frank

--
Frank E Harrell Jr   Professor and ChairmanSchool of Medicine
 Department of Biostatistics   Vanderbilt University

__
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] , Updating Table

2010-07-24 Thread Charles C. Berry

On Fri, 23 Jul 2010, Marcus Liu wrote:


Hi everyone,

Is there any command for updating table withing a loop??


Loops? We don't need no stinking loops!
 (From 'The Good, the Bad, and the Rgly')

tab - table(data.raw, findInterval(seq(along=data.raw), ind+1 ) )
tab %*% upper.tri(tab,diag=T)

or

tab2 - tapply( factor(data.raw), findInterval(seq(along=data.raw), ind+1 ), 
table)
Reduce( +, tab2, accum=TRUE )

HTH,

Chuck

p.s. See the posting guide re including a reproducible example with 
requests like yours.


For instance, at i, I have a table as ZZ = table(data.raw[1:ind[i]]) 
where ind = c(10, 20, 30, ...).?Then , ZZ will be as follow


A B C
?3??? 10?? 2

At (i + 1), ZZ = table(data.raw[(ind[i]+1):ind[i+1]])

A B D
?4 ?? 7??? 8

Is there any command that can update the table ZZ for each time so that in the 
above example, ZZ will be

A B C D
?7??? 17?? 2??? 8

Thanks.

liu




[[alternative HTML version deleted]]




Charles C. Berry(858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
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] , Updating Table

2010-07-24 Thread Duncan Murdoch

On 24/07/2010 11:25 AM, Charles C. Berry wrote:

On Fri, 23 Jul 2010, Marcus Liu wrote:


Hi everyone,

Is there any command for updating table withing a loop?�


Loops? We don't need no stinking loops!
 (From 'The Good, the Bad, and the Rgly')


Actually, that quote comes from the TreasR of the SieRa MadRe.

Duncan Murdoch


tab - table(data.raw, findInterval(seq(along=data.raw), ind+1 ) )
tab %*% upper.tri(tab,diag=T)

or

tab2 - tapply( factor(data.raw), findInterval(seq(along=data.raw), ind+1 ), 
table)
Reduce( +, tab2, accum=TRUE )

HTH,

Chuck

p.s. See the posting guide re including a reproducible example with 
requests like yours.


For instance, at i, I have a table as ZZ = table(data.raw[1:ind[i]]) 
where ind = c(10, 20, 30, ...).�Then , ZZ will be as follow


A B C
�3��� 10�� 2

At (i + 1), ZZ = table(data.raw[(ind[i]+1):ind[i+1]])

A B D
�4 �� 7��� 8

Is there any command that can update the table ZZ for each time so that in the 
above example, ZZ will be

A B C D
�7��� 17�� 2��� 8

Thanks.

liu




[[alternative HTML version deleted]]




Charles C. Berry(858) 534-2098
 Dept of Family/Preventive Medicine
E mailto:cbe...@tajo.ucsd.edu   UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901





__
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] glm - prediction of a factor with several levels

2010-07-24 Thread Ben Bolker
blackscorpio olivier.collignon at live.fr writes:

 I'm currently attempting to predict the occurence of an event (factor)
 having more than 2 levels with several continuous predictors. The model
 being ordinal, I was waiting the glm function to return several intercepts,
 which is not the case when looking to my results (I only have one
 intercept). I finally managed to perform an ordinal polytomous logisitc
 regression with the polr function, which gives several intercepts.
 But does anyone know what was the model performed with glm and why only one
 intercept was given ?

  It's not sufficiently clear (to me at least) what you're trying to
do.  Please provide a minimal reproducible example ... As far as I know,
polr is the right way to do ordinal regression; it's not clear how you
were trying to use glm to do it.

  Ben Bolker

__
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] Doubt about a population competition function

2010-07-24 Thread Bruno Bastos Gonçalves
Hi,
I'm doing a function that describe two populations in competition.
that's the function that i wrote:

 exclusao-function(n10, n20, k1, k2, alfa, beta, t){
n1-k1-(alfa*n20)
n2-k2-(beta*n10)
if(t==0){plot(t, n10, type='b', xlim=range(c(1:t),c

(1:t)), ylim=range(n10, n20), xlab='tempo', 

ylab='tamanho populacional')
points(t, n20, type='b', col=red)
points(t,n10,type=b, col=black)
legend(topleft, c(Pop1,Pop2), cex=0.8, col=c

(black,red), pch=21:21, lty=1:1);
}
if(t0){
for (i in 1:t){
n1[i==1]-n1
n2[i==1]-n2
n1[i+1]-k1-alfa*n2[i]
n2[i+1]-k2-beta*n1[i]

if(n1[i]==0){n1[i:t]==0}

if(n2[i]==0){n2[i:t]==0}

}
plot(c(1:t), n1[1:t], type='b', xlim=range(c(1:t),c

(1:t)), ylim=range(n1[1:t], n2[1:t]), xlab='tempo', 

ylab='tamanho populacional')
points(c(1:t), n2[1:t], type='b', col=red)
legend(topleft, c(Pop1,Pop2), cex=0.8, col=c

(black,red), pch=21:21, lty=1:1);
}}

Where n10: size population in time 0, n20: size population in time 0, k1: 
carrying capacity of the population 1, k2: carrying capacity of the population 
2, alfa: competition coefficient of population 2 in population 1, beta: 
competition coefficient of population 1 in population 2, t: time.

and when some population becomes 0 (ZERO), i want that population still 0 
(ZERO) until the end of t. i have tried to put  if(n1[i]==0){n1[i:t]==0} 
if(n2[i]==0){n2[i:t]==0} after n2[i+1]-k2-beta*n1[i] in the for function, 
but nothing happens. What may i do ?
Thanks
Bruno

[[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] Doubt about a population competition function

2010-07-24 Thread Gmail
Hi,
I'm doing a function that describe two populations in competition.
that's the function that i wrote:

 exclusao-function(n10, n20, k1, k2, alfa, beta, t){
n1-k1-(alfa*n20)
n2-k2-(beta*n10)
if(t==0){plot(t, n10, type='b', xlim=range(c(1:t),c

(1:t)), ylim=range(n10, n20), xlab='tempo', 

ylab='tamanho populacional')
points(t, n20, type='b', col=red)
points(t,n10,type=b, col=black)
legend(topleft, c(Pop1,Pop2), cex=0.8, col=c

(black,red), pch=21:21, lty=1:1);
}
if(t0){
for (i in 1:t){
n1[i==1]-n1
n2[i==1]-n2
n1[i+1]-k1-alfa*n2[i]
n2[i+1]-k2-beta*n1[i]

if(n1[i]==0){n1[i:t]==0}

if(n2[i]==0){n2[i:t]==0}

}
plot(c(1:t), n1[1:t], type='b', xlim=range(c(1:t),c

(1:t)), ylim=range(n1[1:t], n2[1:t]), xlab='tempo', 

ylab='tamanho populacional')
points(c(1:t), n2[1:t], type='b', col=red)
legend(topleft, c(Pop1,Pop2), cex=0.8, col=c

(black,red), pch=21:21, lty=1:1);
}}

Where n10: size population in time 0, n20: size population in time 0, k1: 
carrying capacity of the population 1, k2: carrying capacity of the population 
2, alfa: competition coefficient of population 2 in population 1, beta: 
competition coefficient of population 1 in population 2, t: time.

and when some population becomes 0 (ZERO), i want that population still 0 
(ZERO) until the end of t. i have tried to put  if(n1[i]==0){n1[i:t]==0} 
if(n2[i]==0){n2[i:t]==0} after n2[i+1]-k2-beta*n1[i] in the for function, 
but nothing happens. What may i do ?
Thanks
Bruno

[[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] Book on R's Programming Language

2010-07-24 Thread Matt Stati
Can someone please recommend to me a book on the programming language that R is 
based on? I'm looking for a foundational book that teaches the logic of the S 
language. It seems that knowing the underpinnings of the language can only make 
using R a bit easier. 

Any leads are greatly appreciated . . .

Matt. 



  
[[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] how to calculate the product of every two elements in two vectors

2010-07-24 Thread Henrique Dallazuanna
Try this:

 c(as.matrix(B) %*% A)

On Fri, Jul 23, 2010 at 12:11 PM, aegea gche...@gmail.com wrote:


 Thanks in advance!

 A=c(1, 2,3)
 B=c (9, 10, 11, 12)

 I want to get C=c(1*9, 1*10, 1*11, 1*12, ., 3*9, 3*10, 3*11, 3*12)?
 C is still a vector with 12 elements
 Is there a way to do that?
 --
 View this message in context:
 http://r.789695.n4.nabble.com/how-to-calculate-the-product-of-every-two-elements-in-two-vectors-tp2300299p2300299.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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] Book on R's Programming Language

2010-07-24 Thread Joshua Wiley
Hi Matt,

http://www.r-project.org/doc/bib/R-books.html

Lists a variety of books, and seems to include most  (i.e., my
searches through google, amazon, and barnes and noble, didn't really
turn up others) books dedicated to R.  I have always been under the
impression that Programming with Data (the Green Book) is a classic.

http://cran.r-project.org/manuals.html

has the official manuals

Similar questions have been asked several times on this list so you
can also search for previous threads (e.g.,
http://tolstoy.newcastle.edu.au/R/help/04/06/0063.html )

Best regards,

Josh

On Sat, Jul 24, 2010 at 9:39 AM, Matt Stati mattst...@yahoo.com wrote:
 Can someone please recommend to me a book on the programming language that R 
 is based on? I'm looking for a foundational book that teaches the logic of 
 the S language. It seems that knowing the underpinnings of the language can 
 only make using R a bit easier.

 Any leads are greatly appreciated . . .

 Matt.




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




-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.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] how to calculate the product of every two elements in two vectors

2010-07-24 Thread Gabor Grothendieck
On Fri, Jul 23, 2010 at 11:11 AM, aegea gche...@gmail.com wrote:

 Thanks in advance!

 A=c(1, 2,3)
 B=c (9, 10, 11, 12)

 I want to get C=c(1*9, 1*10, 1*11, 1*12, ., 3*9, 3*10, 3*11, 3*12)?
 C is still a vector with 12 elements
 Is there a way to do that?

Here are yet a few more.  The first one is the only one so far that
uses a single function and the last two are slight variations of ones
already posted.

kronecker(A, B)

c(tcrossprod(B, A))

c(outer(B, A))

c(B %o% A)

Here is a speed comparison.  The fastest are as.matrix, %outer% and
%o% .  They are so close that random fluctuations might easily change
their order and since %o% involves the least keystrokes that one might
be a good overall choice.  Although not among the fastest the
kronecker solution is the simplest since it only involves a single
function call so it might be preferred on that count.

 A - B - 1:400
 out - benchmark(
+ as.matrix =  c(as.matrix(B) %*% A),
+ crossprod = c(tcrossprod(B, A)),
+ outer = c(outer(B, A)),
+ o = c(B %o% A),
+ kronecker = kronecker(A, B),
+ touter = as.vector(t(outer(A, B
 out[order(out$relative), ]
   test replications elapsed relative user.self sys.self
user.child sys.child
1 as.matrix  1000.92 1.00  0.62 0.28
NANA
3 outer  1000.93 1.010870  0.59 0.35
NANA
4 o  1000.94 1.021739  0.66 0.28
NANA
2 crossprod  1001.11 1.206522  0.67 0.43
NANA
5 kronecker  1001.45 1.576087  1.25 0.21
NANA
6touter  1001.84 2.00  1.40 0.43
NANA

__
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] Book on R's Programming Language

2010-07-24 Thread Joseph Magagnoli
Matt,
you might want to check out programming with data by John Chambers.
http://www.amazon.com/Programming-Data-Guide-S-Language/dp/0387985034/ref=sr_1_1?ie=UTF8s=booksqid=1279990404sr=8-1


Best Joe

On Sat, Jul 24, 2010 at 11:39 AM, Matt Stati mattst...@yahoo.com wrote:

 Can someone please recommend to me a book on the programming language that
 R is based on? I'm looking for a foundational book that teaches the logic of
 the S language. It seems that knowing the underpinnings of the language can
 only make using R a bit easier.

 Any leads are greatly appreciated . . .

 Matt.




[[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.htmlhttp://www.r-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.




-- 
Joseph C. Magagnoli
Doctoral Student
Department of Political Science
University of North Texas
1155 Union Circle #305340
Denton, Texas 76203-5017
Email: jcm0...@unt.edu

[[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] Constrain density to 0 at 0?

2010-07-24 Thread Greg Snow
Look at the logspline package.  This is a different approach to density 
estimation from the kernel densities used by 'density', but does allow you to 
set fixed boundaries.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of Farley, Robert
 Sent: Monday, July 19, 2010 7:57 PM
 To: r-help@r-project.org
 Subject: [R] Constrain density to 0 at 0?
 
 I'm plotting some trip length frequencies using the following code:
 
 plot( density(zTestData$Distance, weights=zTestData$Actual),
 xlim=c(0,10),
 main=Test TLFD,
 xlab=Distance,
col=6  )
 lines(density(zTestData$Distance, weights=zTestData$FlatWeight), col=2)
 lines(density(zTestData$Distance, weights=zTestData$BrdWeight ), col=3)
 
 which works fine except the distances are all positive, but the
 densities don't drop to 0 until around -2 or -3.
 
 Is there a way for me to force the density plot to 0 at 0?
 
 
 
 Thanks
 
 
 
 Robert Farley
 Metro
 1 Gateway Plaza
 Mail Stop 99-23-7
 Los Angeles, CA 90012-2952
 Voice: (213)922-2532
 Fax:(213)922-2868
 www.Metro.net
 
 
 
   [[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.


[R] matest function for multiple factors

2010-07-24 Thread shabnam k
Hi,
   I am using maanova package for analysis. In my dataset, two fixed factors
time and treatment and sample as random factor is there. Am able to get
madata object and fitmaanova object. But, am unable to do f-test with two
factors, but i have done f-test seperately for two factors.


fit.full.mix - fitmaanova(madata, formula = ~Sample+Time+Treatment,
random = ~Sample)

ftest.all = *matest*(madata, fit.full.mix, test.method=c(1,1),
shuffle.method=sample, *term=Time+Treatment*, n.perm= 100)

  Can u please suggest me, how to represent multiple factors in the
above function simultaneously in term.

[[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] How to deal with more than 6GB dataset using R?

2010-07-24 Thread Greg Snow
You may want to look at the biglm package as another way to regression models 
on very large data sets.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.s...@imail.org
801.408.8111


 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-
 project.org] On Behalf Of babyfoxlo...@sina.com
 Sent: Friday, July 23, 2010 10:10 AM
 To: r-help@r-project.org
 Subject: [R] How to deal with more than 6GB dataset using R?
 
 nbsp;Hi there,
 
 Sorry to bother those who are not interested in this problem.
 
 I'm dealing with a large data set, more than 6 GB file, and doing
 regression test with those data. I was wondering are there any
 efficient ways to read those data? Instead of just using read.table()?
 BTW, I'm using a 64bit version desktop and a 64bit version R, and the
 memory for the desktop is enough for me to use.
 Thanks.
 
 
 --Gin
 
   [[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.


[R] Using R to fill ETM+ data gaps?

2010-07-24 Thread Abdi, Abdulhakim
Hi,

I was wondering if anyone knows of a method (or if it's possible) to use R in 
interpolating Landsat ETM+ data gaps? 

Regards,

Hakim Abdi





_
Abdulhakim Abdi, M.Sc.

Conservation GIS/Remote Sensing Lab
Smithsonian Conservation Biology Institute
1500 Remount Road
Front Royal, VA 22630
phone: +1 540 635 6578
mobile: +1 747 224 7006
fax: +1 540 635 6506 (Attn: ABDI/GIS Lab)
email: ab...@si.edu
http://nationalzoo.si.edu/SCBI/ConservationGIS/ 

__
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] How to generate a sequence of dates without hardcoding the year

2010-07-24 Thread Felipe Carrillo
Hi:
I have a dataframe named 'spring' and I am trying to add a new variable named 
'IdDate'
This line of code works fine:
spring$idDate - seq(as.Date(2008-07-01),as.Date(2009-06-30),by=week)

But I don't want to hardcode the year because it will be used again the 
following year
Is it possible to just generate dates with the month and day? 

I tried the code below:
seq(as.Date(7-1,%B%d),as.Date(6-30,%B%d),by=week)

and got this error message:
Error in seq.int(0, to - from, by) : 'to' must be finite
Thanks for any pointers

 
Felipe D. Carrillo
Supervisory Fishery Biologist
Department of the Interior
US Fish  Wildlife Service
California, 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] How to generate a sequence of dates without hardcoding the year

2010-07-24 Thread Henrique Dallazuanna
Try this:

format(seq(as.Date(2008-07-01),as.Date(2009-06-30),by=week), %d/%m)

On Sat, Jul 24, 2010 at 6:07 PM, Felipe Carrillo
mazatlanmex...@yahoo.comwrote:

 Hi:
 I have a dataframe named 'spring' and I am trying to add a new variable
 named
 'IdDate'
 This line of code works fine:
 spring$idDate - seq(as.Date(2008-07-01),as.Date(2009-06-30),by=week)

 But I don't want to hardcode the year because it will be used again the
 following year
 Is it possible to just generate dates with the month and day?

 I tried the code below:
 seq(as.Date(7-1,%B%d),as.Date(6-30,%B%d),by=week)

 and got this error message:
 Error in seq.int(0, to - from, by) : 'to' must be finite
 Thanks for any pointers


 Felipe D. Carrillo
 Supervisory Fishery Biologist
 Department of the Interior
 US Fish  Wildlife Service
 California, 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[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] (no subject)

2010-07-24 Thread Paul Smith
2010/7/23 w 霍 hw_joyce...@hotmail.com:
 I use the constrOptim to maximize a function with four constriants but the 
 answer does not leave from the starting value and there is only one outer 
 iteration. The function is defined as follows:
 tm-function(p){
 p1-p[1]; p2-p[2]; p3-p[3];
 p4-1-p1-p2-p3;
 p1*p2*p3*p4}

 ##the constraints are p1=0; p2=0; p3=0 and p4=0 i.e. p1+p2+p3=1
 start-c(0.991,0.001,0.001)
 dev-rbind(diag(3),-diag(3),rep(-1,3))
 bvec-c(rep(0,3),rep(-1,4))
 constrOptim(start,tm,NULL,ui=dev,ci=bvec,control=list(maxit=1))

 Am i missing something obviously that cause the problem or there is some bugs 
 in constrOptim. Could you please help me out

Wenwen,

I believe that the reason why constrOptim behaves as described is
related to the fact that

(p1, p2, p3) = (1,0,0)

is a stationary point and you use it as a starting point. Try a
different starting point.

If the objective is to maximize, then you should use the following command:

constrOptim(start,tm,NULL,ui=dev,ci=bvec,control=list(maxit=1,fnscale=-1))

(Notice fnscale=-1.)

Finally, whenever you ask something on this list, please use a
meaningful title for your message, as it will dramatically increase
the chances of you getting an answer.

Good luck,

Paul

__
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] How to generate a sequence of dates without hardcoding the year

2010-07-24 Thread jim holtman
Is this what you want if you want to assume that the date without a
year is this year:

 seq(as.Date(7-1,%m-%d),by=week, length=52)
 [1] 2010-07-01 2010-07-08 2010-07-15 2010-07-22 2010-07-29
2010-08-05 2010-08-12 2010-08-19
 [9] 2010-08-26 2010-09-02 2010-09-09 2010-09-16 2010-09-23
2010-09-30 2010-10-07 2010-10-14
[17] 2010-10-21 2010-10-28 2010-11-04 2010-11-11 2010-11-18
2010-11-25 2010-12-02 2010-12-09
[25] 2010-12-16 2010-12-23 2010-12-30 2011-01-06 2011-01-13
2011-01-20 2011-01-27 2011-02-03
[33] 2011-02-10 2011-02-17 2011-02-24 2011-03-03 2011-03-10
2011-03-17 2011-03-24 2011-03-31
[41] 2011-04-07 2011-04-14 2011-04-21 2011-04-28 2011-05-05
2011-05-12 2011-05-19 2011-05-26
[49] 2011-06-02 2011-06-09 2011-06-16 2011-06-23



On Sat, Jul 24, 2010 at 5:07 PM, Felipe Carrillo
mazatlanmex...@yahoo.com wrote:
 Hi:
 I have a dataframe named 'spring' and I am trying to add a new variable named
 'IdDate'
 This line of code works fine:
 spring$idDate - seq(as.Date(2008-07-01),as.Date(2009-06-30),by=week)

 But I don't want to hardcode the year because it will be used again the
 following year
 Is it possible to just generate dates with the month and day?

 I tried the code below:
 seq(as.Date(7-1,%B%d),as.Date(6-30,%B%d),by=week)

 and got this error message:
 Error in seq.int(0, to - from, by) : 'to' must be finite
 Thanks for any pointers


 Felipe D. Carrillo
 Supervisory Fishery Biologist
 Department of the Interior
 US Fish  Wildlife Service
 California, 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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] glm - prediction of a factor with several levels

2010-07-24 Thread zachmohr

As far as I know, glm only works with dichotomous or count data.  polr in
the MASS package works and so does lrm {Design} for ordinal dependent
variables.  I would assume that the model produced by glm is a dichotomous
version of your model but not sure.  Only one intercept would be given
because if you used the log link then it would have produced a dichotomous
model instead of an ordered logistic regression.  My suggestion is to use
polr or lrm.

On Fri, Jul 23, 2010 at 6:15 PM, blackscorpio [via R] 
ml-node+2300793-1751019155-246...@n4.nabble.comml-node%2b2300793-1751019155-246...@n4.nabble.com
 wrote:

 Dear community,
 I'm currently attempting to predict the occurence of an event (factor)
 having more than 2 levels with several continuous predictors. The model
 being ordinal, I was waiting the glm function to return several intercepts,
 which is not the case when looking to my results (I only have one
 intercept). I finally managed to perform an ordinal polytomous logisitc
 regression with the polr function, which gives several intercepts.
 But does anyone know what was the model performed with glm and why only one
 intercept was given ?
 Thanks a lot for your help !


 --
 View message @
 http://r.789695.n4.nabble.com/glm-prediction-of-a-factor-with-several-levels-tp2300793p2300793.html
 To unsubscribe from R, click here (link removed) ==.





-- 
Department of Public Administration
University of Kansas
4060 Wesco Hall
Office W
Lawrence KS 66045-3177

Phone: (785) 813-1384
Email: zachm...@gmail.com

-- 
View this message in context: 
http://r.789695.n4.nabble.com/glm-prediction-of-a-factor-with-several-levels-tp2300793p2301324.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.


Re: [R] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread David Winsemius


On Jul 24, 2010, at 4:54 PM, mpw...@illinois.edu wrote:


THANKS, but I have one issue and one question.

For some reason the secondstrongest value for row 3 and 6 are  
incorrect (they are the strongest) the remaining 10 are correct??


In my run of Wiley's code I instead get identical values for rows  
2,5,6. Holtman's and my solutions did not suffer from that defect,  
although mine suffered from my misreading of your request, thinking  
that you wanted the top 3. The fix is trivial


These data are being used to track radio-tagged birds, they are from  
automated radio telemetry receivers.  I will applying the following  
formula


  diff - ((strongest- secondstrongest)/100)
  bearingdiff -30-(-0.0624*(diff**2))-(2.8346*diff)


vals - c(value0, value60, value120, value180, value240,  
value300)

value.str2 - (match(yourdata$secondstrongestantenna, vals)-1)*60
value.str1 - (match(yourdata$strongestantenna, vals)-1)*60
change.ind - abs(match(yourdata, vals) - which(match(yourdata, vals) )



A) Then the bearing diff is added to strongestantenna (value0 =  
0degrees) if the secondstrongestatenna is greater (eg value0 and  
value60),


B) or if the secondstrongestantenna is smaller than the  
strongestantenna,

then the bearingdiff is substracted from the strongestantenna.




C) The only exception is that if value0 (0degrees) is strongest and  
value300(360degrees) is the secondstrongestantenna then the bearing  
is 360-bearingdiff.



D) Also the strongestantenna and secondstrongestantenna have to be  
next to each other (e.g. value0 with value60, value240 with  
value300, value0 with value300) or the results should be NA.


After setting finalbearing with A, B, and C then:
yourdata$finalbearing - with(yourdata, ifelse (
change.ind 5  change.ind  1 ,
 NA, finalbearing) )

I have been trying to use a series of if,else statements to produce  
these bearing, but all I am producing is errors. Any suggestion  
would be appreciated.





Again THANKS for you efforts.

Mike

 Original message 

Date: Fri, 23 Jul 2010 23:01:56 -0700
From: Joshua Wiley jwiley.ps...@gmail.com
Subject: Re: [R] Trouble retrieving the second largest value from  
each row of  a data.frame

To: mpw...@illinois.edu
Cc: r-help@r-project.org

Hi,

Here is a little function that will do what you want and return a  
nice output:


#Function To calculate top two values and return
my.finder - function(mydata) {
my.fun - function(data) {
  strongest - which.max(data)
  secondstrongest - which.max(data[-strongest])
  strongestantenna - names(data)[strongest]
  secondstrongantenna - names(data[-strongest])[secondstrongest]
  value - matrix(c(data[strongest], data[secondstrongest],
strongestantenna, secondstrongantenna), ncol =4)
  return(value)
}
dat - apply(mydata, 1, my.fun)
dat - t(dat)
dat - as.data.frame(dat, stringsAsFactors = FALSE)
colnames(dat) - c(strongest, secondstrongest,
   strongestantenna, secondstrongantenna)
dat[ , strongest] - as.numeric(dat[ , strongest])
dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
return(dat)
}


#Using your example data:

yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L,  
-12805L,
-12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 =  
c(-11707L,

-13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,
-11736L, -12873L), value120 = c(-11072L, -11176L, -3L, -11071L,
-11067L, -12430L, -11092L, -11061L, -11137L, -12973L), value180 =  
c(-12471L,

-11799L, -12439L, -12385L, -11638L, -12430L, -11709L, -12373L,
-12570L, -12537L), value240 = c(-12838L, -13210L, -13089L, -11561L,
-13527L, -12430L, -11607L, -11426L, -13467L, -12973L), value300 =  
c(-13357L,

-13845L, -13880L, -13317L, -13873L, -12814L, -13025L, -12805L,
-13739L, -11146L)), .Names = c(value0, value60, value120,
value180, value240, value300), class = data.frame,  
row.names = c(1,

2, 3, 4, 5, 6, 7, 8, 9, 10))

my.finder(yourdata) #and what you want is in a nicely labeled data  
frame


#A potential problem is that it is not very efficient

#Here is a test using a matrix of 100,000 rows
#sampled from the same range as your data
#with the same number of columns

data.test - matrix(
sample(seq(min(yourdata),max(yourdata)), size = 50, replace =  
TRUE),

ncol = 5)

system.time(my.finder(data.test))

#On my system I get


system.time(my.finder(data.test))

 user  system elapsed
 2.890.002.89

Hope that helps,

Josh



On Fri, Jul 23, 2010 at 6:20 PM,  mpw...@illinois.edu wrote:
I have a data frame with a couple million lines and want to  
retrieve the largest and second largest values in each row, along  
with the label of the column these values are in. For example


row 1
strongest=-11072
secondstrongest=-11707
strongestantenna=value120
secondstrongantenna=value60

Below is the code I am using and a truncated data.frame.   
Retrieving the largest value was easy, but 

Re: [R] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread Joshua Wiley
On Sat, Jul 24, 2010 at 5:09 PM, David Winsemius dwinsem...@comcast.net wrote:

 On Jul 24, 2010, at 4:54 PM, mpw...@illinois.edu wrote:

 THANKS, but I have one issue and one question.

 For some reason the secondstrongest value for row 3 and 6 are incorrect
 (they are the strongest) the remaining 10 are correct??

 In my run of Wiley's code I instead get identical values for rows 2,5,6.

Yes, my apologies; I neglected a [-strongest] when extracting the
second highest value.  I included a corrected form below; however,
Winsemius' code is cleaner, not to mention easier to generalize, so I
see no reason not to use that option.  You might consider using a
different object name than 'diff' since it is also the name of a
function.

Josh

###
my.finder - function(mydata) {
  my.fun - function(data) {
strongest - which.max(data)
secondstrongest - which.max(data[-strongest])
strongestantenna - names(data)[strongest]
secondstrongantenna - names(data[-strongest])[secondstrongest]
value - matrix(c(data[strongest], data[-strongest][secondstrongest],
  strongestantenna, secondstrongantenna), ncol =4)
return(value)
  }
  dat - apply(mydata, 1, my.fun)
  dat - t(dat)
  dat - as.data.frame(dat, stringsAsFactors = FALSE)
  colnames(dat) - c(strongest, secondstrongest,
 strongestantenna, secondstrongantenna)
  dat[ , strongest] - as.numeric(dat[ , strongest])
  dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
  return(dat)
}




 Holtman's and my solutions did not suffer from that defect, although mine
 suffered from my misreading of your request, thinking that you wanted the
 top 3. The fix is trivial

 These data are being used to track radio-tagged birds, they are from
 automated radio telemetry receivers.  I will applying the following formula

  diff - ((strongest- secondstrongest)/100)
  bearingdiff -30-(-0.0624*(diff**2))-(2.8346*diff)

 vals - c(value0, value60, value120, value180, value240,
 value300)
 value.str2 - (match(yourdata$secondstrongestantenna, vals)-1)*60
 value.str1 - (match(yourdata$strongestantenna, vals)-1)*60
 change.ind - abs(match(yourdata, vals) - which(match(yourdata, vals) )


 A) Then the bearing diff is added to strongestantenna (value0 = 0degrees)
 if the secondstrongestatenna is greater (eg value0 and value60),

 B) or if the secondstrongestantenna is smaller than the strongestantenna,
 then the bearingdiff is substracted from the strongestantenna.


 C) The only exception is that if value0 (0degrees) is strongest and
 value300(360degrees) is the secondstrongestantenna then the bearing is
 360-bearingdiff.


 D) Also the strongestantenna and secondstrongestantenna have to be next to
 each other (e.g. value0 with value60, value240 with value300, value0 with
 value300) or the results should be NA.

 After setting finalbearing with A, B, and C then:
 yourdata$finalbearing - with(yourdata, ifelse (
                                change.ind 5  change.ind  1 ,
                                             NA, finalbearing) )

 I have been trying to use a series of if,else statements to produce these
 bearing, but all I am producing is errors. Any suggestion would be
 appreciated.



 Again THANKS for you efforts.

 Mike

  Original message 

 Date: Fri, 23 Jul 2010 23:01:56 -0700
 From: Joshua Wiley jwiley.ps...@gmail.com
 Subject: Re: [R] Trouble retrieving the second largest value from each
 row of  a data.frame
 To: mpw...@illinois.edu
 Cc: r-help@r-project.org

 Hi,

 Here is a little function that will do what you want and return a nice
 output:

 #Function To calculate top two values and return
 my.finder - function(mydata) {
 my.fun - function(data) {
  strongest - which.max(data)
  secondstrongest - which.max(data[-strongest])
  strongestantenna - names(data)[strongest]
  secondstrongantenna - names(data[-strongest])[secondstrongest]
  value - matrix(c(data[strongest], data[secondstrongest],
                    strongestantenna, secondstrongantenna), ncol =4)
  return(value)
 }
 dat - apply(mydata, 1, my.fun)
 dat - t(dat)
 dat - as.data.frame(dat, stringsAsFactors = FALSE)
 colnames(dat) - c(strongest, secondstrongest,
                   strongestantenna, secondstrongantenna)
 dat[ , strongest] - as.numeric(dat[ , strongest])
 dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
 return(dat)
 }


 #Using your example data:

 yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L, -12805L,
 -12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 =
 c(-11707L,
 -13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,
 -11736L, -12873L), value120 = c(-11072L, -11176L, -3L, -11071L,
 -11067L, -12430L, -11092L, -11061L, -11137L, -12973L), value180 =
 c(-12471L,
 -11799L, -12439L, -12385L, -11638L, -12430L, -11709L, -12373L,
 -12570L, -12537L), value240 = c(-12838L, -13210L, -13089L, -11561L,
 -13527L, -12430L, -11607L, -11426L, -13467L, -12973L), value300 =
 c(-13357L,
 

Re: [R] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread David Winsemius


On Jul 24, 2010, at 8:09 PM, David Winsemius wrote:



On Jul 24, 2010, at 4:54 PM, mpw...@illinois.edu wrote:


THANKS, but I have one issue and one question.

For some reason the secondstrongest value for row 3 and 6 are  
incorrect (they are the strongest) the remaining 10 are correct??


In my run of Wiley's code I instead get identical values for rows  
2,5,6. Holtman's and my solutions did not suffer from that defect,  
although mine suffered from my misreading of your request, thinking  
that you wanted the top 3. The fix is trivial


These data are being used to track radio-tagged birds, they are  
from automated radio telemetry receivers.  I will applying the  
following formula


 diff - ((strongest- secondstrongest)/100)
 bearingdiff -30-(-0.0624*(diff**2))-(2.8346*diff)


vals - c(value0, value60, value120, value180, value240,  
value300)

value.str2 - (match(yourdata$secondstrongestantenna, vals)-1)*60
value.str1 - (match(yourdata$strongestantenna, vals)-1)*60
change.ind - abs(match(yourdata, vals) - which(match(yourdata,  
vals) )


OOOPs should have been

 change.ind - abs(match(yourdata, vals) - match(yourdata, vals) )






A) Then the bearing diff is added to strongestantenna (value0 =  
0degrees) if the secondstrongestatenna is greater (eg value0 and  
value60),


B) or if the secondstrongestantenna is smaller than the  
strongestantenna,

then the bearingdiff is substracted from the strongestantenna.


yourdata$finalbearing - with(yourdata, ifelse (value.str2value.str1,  
bearingdiff+value.str1, value.str1-bearingdiff) )







C) The only exception is that if value0 (0degrees) is strongest and  
value300(360degrees) is the secondstrongestantenna then the bearing  
is 360-bearingdiff.




yourdata$finalbearing - with(yourdata, ifelse (strongestantenna ==  
value0  secondstrongantenna == value300, 360- bearingdiff,  
finalbearing) );



D) Also the strongestantenna and secondstrongestantenna have to be  
next to each other (e.g. value0 with value60, value240 with  
value300, value0 with value300) or the results should be NA.


After setting finalbearing with A, B, and C then:
yourdata$finalbearing - with(yourdata, ifelse (
   change.ind 5  change.ind  1 ,
NA, finalbearing) )


 yourdata
   strongest secondstrongest strongestantenna secondstrongantenna  
finalbearing
1 -11072  -11707 value120 value60 
-11086.52
2 -11176  -11799 value120value180 
-11190.76
3 -3  -11778 value120 value60 
-11126.91
4 -11071  -11561 value120 
value240   NA
5 -11067  -11638 value120value180 
-11082.85
6 -11068  -11698   value0 value60 
-11082.62
7 -11092  -11607 value120 
value240   NA
8 -11061  -11426 value120 
value240   NA
9 -11137  -11736 value120 value60 
-11152.26
10-11146  -11779 value300  value0 
-11160.56





I have been trying to use a series of if,else statements to produce  
these bearing,


ifelse is the correct construct for processing vectors

--
David.
but all I am producing is errors. Any suggestion would be  
appreciated.





Again THANKS for you efforts.

Mike

 Original message 

Date: Fri, 23 Jul 2010 23:01:56 -0700
From: Joshua Wiley jwiley.ps...@gmail.com
Subject: Re: [R] Trouble retrieving the second largest value from  
each row of  a data.frame

To: mpw...@illinois.edu
Cc: r-help@r-project.org

Hi,

Here is a little function that will do what you want and return a  
nice output:


#Function To calculate top two values and return
my.finder - function(mydata) {
my.fun - function(data) {
 strongest - which.max(data)
 secondstrongest - which.max(data[-strongest])
 strongestantenna - names(data)[strongest]
 secondstrongantenna - names(data[-strongest])[secondstrongest]
 value - matrix(c(data[strongest], data[secondstrongest],
   strongestantenna, secondstrongantenna), ncol =4)
 return(value)
}
dat - apply(mydata, 1, my.fun)
dat - t(dat)
dat - as.data.frame(dat, stringsAsFactors = FALSE)
colnames(dat) - c(strongest, secondstrongest,
  strongestantenna, secondstrongantenna)
dat[ , strongest] - as.numeric(dat[ , strongest])
dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
return(dat)
}


#Using your example data:

yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L,  
-12805L,
-12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 =  
c(-11707L,

-13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,
-11736L, -12873L), value120 = c(-11072L, -11176L, -3L, -11071L,
-11067L, -12430L, -11092L, -11061L, -11137L, -12973L), value180 =  
c(-12471L,

-11799L, -12439L, 

Re: [R] Trouble retrieving the second largest value from each row of a data.frame

2010-07-24 Thread David Winsemius


On Jul 24, 2010, at 9:27 PM, David Winsemius wrote:



On Jul 24, 2010, at 8:09 PM, David Winsemius wrote:



On Jul 24, 2010, at 4:54 PM, mpw...@illinois.edu wrote:


THANKS, but I have one issue and one question.

For some reason the secondstrongest value for row 3 and 6 are  
incorrect (they are the strongest) the remaining 10 are correct??


In my run of Wiley's code I instead get identical values for rows  
2,5,6. Holtman's and my solutions did not suffer from that defect,  
although mine suffered from my misreading of your request, thinking  
that you wanted the top 3. The fix is trivial


These data are being used to track radio-tagged birds, they are  
from automated radio telemetry receivers.  I will applying the  
following formula


diff - ((strongest- secondstrongest)/100)
bearingdiff -30-(-0.0624*(diff**2))-(2.8346*diff)


vals - c(value0, value60, value120, value180, value240,  
value300)

value.str2 - (match(yourdata$secondstrongestantenna, vals)-1)*60


Had a misspelling ... rather:

match(yourdata$secondstrongantenna, vals)



value.str1 - (match(yourdata$strongestantenna, vals)-1)*60
change.ind - abs(match(yourdata, vals) - which(match(yourdata,  
vals) )


OOOPs should have been

change.ind - abs(match(yourdata, vals) - match(yourdata, vals) )






A) Then the bearing diff is added to strongestantenna (value0 =  
0degrees) if the secondstrongestatenna is greater (eg value0 and  
value60),


B) or if the secondstrongestantenna is smaller than the  
strongestantenna,

then the bearingdiff is substracted from the strongestantenna.


yourdata$finalbearing - with(yourdata, ifelse  
(value.str2value.str1, bearingdiff+value.str1, value.str1- 
bearingdiff) )







C) The only exception is that if value0 (0degrees) is strongest  
and value300(360degrees) is the secondstrongestantenna then the  
bearing is 360-bearingdiff.




yourdata$finalbearing - with(yourdata, ifelse (strongestantenna ==  
value0  secondstrongantenna == value300, 360- bearingdiff,  
finalbearing) );



D) Also the strongestantenna and secondstrongestantenna have to be  
next to each other (e.g. value0 with value60, value240 with  
value300, value0 with value300) or the results should be NA.


After setting finalbearing with A, B, and C then:
yourdata$finalbearing - with(yourdata, ifelse (
  change.ind 5  change.ind  1 ,
   NA, finalbearing) )




Better result with proper creation of value.str2:
yourdata
   strongest secondstrongest strongestantenna secondstrongantenna  
finalbearing
1 -11072  -11707 value120 value60 
105.48359
2 -11176  -11799 value120value180 
134.76237
3 -3  -11778 value120 value60 
106.09061
4 -11071  -11561 value120 
value240   NA
5 -11067  -11638 value120value180 
135.84893
6 -11068  -11698   value0 value60  
14.61868
7 -11092  -11607 value120 
value240   NA
8 -11061  -11426 value120 
value240   NA
9 -11137  -11736 value120 value60 
104.74034
10-11146  -11779 value300  value0 
285.44272




I have been trying to use a series of if,else statements to  
produce these bearing,


ifelse is the correct construct for processing vectors

--
David.
but all I am producing is errors. Any suggestion would be  
appreciated.





Again THANKS for you efforts.

Mike

 Original message 

Date: Fri, 23 Jul 2010 23:01:56 -0700
From: Joshua Wiley jwiley.ps...@gmail.com
Subject: Re: [R] Trouble retrieving the second largest value from  
each row of  a data.frame

To: mpw...@illinois.edu
Cc: r-help@r-project.org

Hi,

Here is a little function that will do what you want and return a  
nice output:


#Function To calculate top two values and return
my.finder - function(mydata) {
my.fun - function(data) {
strongest - which.max(data)
secondstrongest - which.max(data[-strongest])
strongestantenna - names(data)[strongest]
secondstrongantenna - names(data[-strongest])[secondstrongest]
value - matrix(c(data[strongest], data[secondstrongest],
  strongestantenna, secondstrongantenna), ncol =4)
return(value)
}
dat - apply(mydata, 1, my.fun)
dat - t(dat)
dat - as.data.frame(dat, stringsAsFactors = FALSE)
colnames(dat) - c(strongest, secondstrongest,
 strongestantenna, secondstrongantenna)
dat[ , strongest] - as.numeric(dat[ , strongest])
dat[ , secondstrongest] - as.numeric(dat[ , secondstrongest])
return(dat)
}


#Using your example data:

yourdata - structure(list(value0 = c(-13007L, -12838L, -12880L,  
-12805L,
-12834L, -11068L, -12807L, -12770L, -12988L, -11779L), value60 =  
c(-11707L,

-13210L, -11778L, -11653L, -13527L, -11698L, -14068L, -11665L,

[R] c-statiscs 95% CI for cox regression model

2010-07-24 Thread paaventhan jeyaganth

Dear all, 
how can i do the calculate the  C-statistics
95% confidences interval for the cox regression model.
 Thanks very much for your any help.
 Paaveenthan  
_
[[elided Hotmail spam]]

[[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] UseR! 2010 - my impressions

2010-07-24 Thread Dirk Eddelbuettel
On Sat, Jul 24, 2010 at 08:55:01AM -0500, Frank E Harrell Jr wrote:
 On 07/23/2010 06:50 PM, Ravi Varadhan wrote:
 I want to echo what Ravi said.  The talks were terrific (thanks to
 the program committee and the speakers) and Kate Mullen and her team
 did an extraordinary job in putting the conference together and
 running it.  I am proud to have been a part of it.  Thank you all!

Not much to add to this, so I just leave it at Yup!.

Thanks for useR! 2010. A job well done, and then some.

-- 
Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.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] c-statiscs 95% CI for cox regression model

2010-07-24 Thread Frank E Harrell Jr

On 07/24/2010 09:51 PM, paaventhan jeyaganth wrote:


Dear all,
how can i do the calculate the  C-statistics
95% confidences interval for the cox regression model.
  Thanks very much for your any help.
  Paaveenthan   


install.packages('Hmisc')
require(Hmisc
?rcorr.cens (there is an example at the bottom)

Frank

--
Frank E Harrell Jr   Professor and ChairmanSchool of Medicine
 Department of Biostatistics   Vanderbilt University

__
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] Equivalent to go-to statement

2010-07-24 Thread Michael Haenlein
Dear all,

I'm working with a code that consists of two parts: In Part 1 I'm generating
a random graph using the igraph library (which represents the relationships
between different nodes) and a vector (which represents a certain
characteristic for each node):

library(igraph)
g - watts.strogatz.game(1,100,5,0.05)
z - rlnorm(100,0,1)

In Part 2 I'm iteratively changing the elements of z in order to reach a
certain value of a certain target variable. I'm doing this using a while
statement:

while (target_variable  threshold) {## adapt z}

The problem is that in some rare cases this iterative procedure can take
very long (a couple of million of iterations), depending on the specific
structure of the graph generated in Part 1. I therefore would like to change
Part 2 of my code in the sense that once a certain threshold number of
iterations has been achieved, the iterative process in Part 2 stops and goes
back to Part 1 to generate a new graph structure. So my idea is as follows:

- Run Part 1 and generate g and z
- Run Part 2 and iteratively modify z to maximize the target variable
- If Part 2 can be obtained in less than X steps, then go to Part 3
- If Part 2 takes more than X steps then go back to Part 1 and start again

I think that R does not have a function like go-to or go-back.

Does anybody know of a convenient way of doing this?

Thanks very much for your help,

Michael

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