Re: [R] PCA on stacked raster (multiple bands/ layers) in R

2014-10-30 Thread Gustavo Bediaga
Hi,

You have to transform it to a Data Frame.

Try:

files <- stack(rasterlist)

filesdf<-as.data.frame(files)

pca <- princomp(formula = ~., data = filesdf, cor = TRUE, 
na.action=na.exclude)


hope it helps

Gustavo

Em quinta-feira, 30 de outubro de 2014 14h38min56s UTC-2, John Wasige 
escreveu:
>
> Hello community, I need help on how I can perform PCA on stacked raster
> (multiple bands/ layers) in R. Does any body have an idea or script? 
> Thanks
> John
>
> [[alternative HTML version deleted]]
>
> __
> r-h...@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] help for an R automated procedures

2013-02-28 Thread Gustavo Vieira

Dear, I would like to post the following question to the r-help on Nabble 
(thanks in advance for the attention, Gustavo Vieira):
Hi there.
I have a data set on hands with 5,220 cases and I'd like to automate some
procedures (but I have almost no programming knowledge). The data has some
continuous variables that are grouped by 2 others: the name of species and
the locality where they were collected. So, the samples are defined as 'each
species on each locality'. For every sample I'd like to do multiple
imputation (when applicable), test for the presence of outliers, standardize
the variables, correct some species abundances, save individual samples to
tab delimited text file, and assemble each individual sample (now, without
NAs and outliers, corrected abundances, and with the new standardized
variables) into a single data set. That task is pretty complex to me, since
my programming knowledge is poor (and my free time to learn R programming is
sparse). Could someone help me with that (I could provide you the data set
and the script I have written to do that, sample by sample [ouch!])?
Thanks in advance for your attention and all the best (g...@hotmail.com).

[Bellow is an example is the codes I've used to accomplish my goals, sample
by sample, which can exemplify the complexity of the procedures:

#Subsetting the data (v1-v11 are continuous "predictors"): species 1 at
locality 1 (all data [5520 cases] are on a vector called 'morfo')
sp1.loc1<-morfo[which(spps=="sp1" & taxoc=="loc1"),] #getting only the
observations of sp1 (species 1) at loc1 (locality 1)
str(sp1.loc1) #abundance -> 19 cases and the abundance variable ('abund')
says 18…
sp1.loc1$abund<-rep(19,19)
summary(sp1.loc1) #missing values present; abundance for sp1 at loc1
corrected
attach(sp1.loc1)

#Dealing with NAs:
install.packages("mice", dependencies = T) #ok (R at: home & work)
library(mice)
imp <- mice(sp1.loc1)
sp1.loc1 <- complete(imp)
summary(sp1.loc1) #jaust checking... No more Nas!
attach(sp1.loc1)


#Detecting univariate outliers
z.crit <- qnorm(0.)

subset(sp1.loc1, select = id, subset = abs(scale(v1)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v2)) > z.crit)
morfo[47,6]
sort(v2[taxoc=="loc1"]) #the nearest observation close to 32.00 is 25.10
sp1.loc1[,6][sp1.loc1[,6]==32.00]<-25.10
subset(sp1.loc1, select = id, subset = abs(scale(v2)) > z.crit) #Rechecking
for outliers (now, it's ok)

subset(sp1.loc1, select = id, subset = abs(scale(v3)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v4)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v5)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v6)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v7)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v8)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v9)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v10)) > z.crit)

subset(sp1.loc1, select = id, subset = abs(scale(v11)) > z.crit)

#Standardizing variables
v1.std<-with(sp1.loc1,(scale(v1)))
v1.pad<-v1.std[,1]

v2.std<-with(sp1.loc1,(scale(v2)))
v2.pad<-v2.std[,1]

v3.std<-with(sp1.loc1,(scale(v3)))
v3.pad<-v3.std[,1]

v4.std<-with(sp1.loc1,(scale(v4)))
v4.pad<-v4.std[,1]

v5.std<-with(sp1.loc1,(scale(v5)))
v5.pad<-v5.std[,1]

v6.std<-with(sp1.loc1,(scale(v6)))
v6.pad<-v6.std[,1]

v7.std<-with(sp1.loc1,(scale(v7)))
v7.pad<-v7.std[,1]

v8.std<-with(sp1.loc1,(scale(v8)))
v8.pad<-v8.std[,1]

v9.std<-with(sp1.loc1,(scale(v9)))
v9.pad<-v9.std[,1]

v10.std<-with(sp1.loc1,(scale(v10)))
v10.pad<-v10.std[,1]

v11.std<-with(sp1.loc1,(scale(v11)))
v11.pad<-v1.std[,1]


#Joining the new standardized variables to the sp1.loc1 data set

sp1.loc1<-data.frame(sp1.loc1,v1.pad,v2.pad,v3.pad,v4.pad,v5.pad,v6.pad,v7.pad,v8.pad,v9.pad,v10.pad,v11.pad)

attach(sp1.loc1)

write.table(sp1.loc1,"sp1.at.loc1.txt",quote=F,row.names=F,
col.names=T,sep="\t")

detach(sp1.loc1)

#Subsetting the data (v1-v11 are continuous "predictors"): species 2 at
locality 1...]--

"Time will tell"
--

  
[[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] forecast ARMA(1,1)/GARCH(1,1) using fGarch library

2013-02-17 Thread GUSTAVO SANTA ROSA GARCIA

Hi, i am working in the forecast   of the daily  price crude .

The last prices of this data are the following:

 100.60 101.47 100.20 100.06  98.68 101.28 101.05 102.13 101.70  98.27

  101.00 100.50 100.03 102.23 102.68 103.32 102.67 102.23 102.14 101.25

  101.11  99.90  98.53  96.76  96.12  96.54  96.30  95.92  95.92  93.45

  93.71  96.42  93.99  93.76  95.24  95.63  95.95  95.83  95.65  96.61

  91.30  91.66  96.23  94.44  94.50  96.52  97.07  97.37  95.31  96.10

  94.35  93.34  93.68  93.65  95.16  94.32  94.82  94.93  95.72  96.41 

  96.70  95.87  95.46  96.83  96.49  96.70  99.61 100.84  99.90  99.65

  99.22  98.84  99.08  97.53  98.51  99.17 100.07 101.49 102.40 103.24

102.36 100.70 100.93 104.43 105.67 106.23 109.98 108.80 109.10 108.86 108.68 
109.59 110.41

The data consist of 2973 observations.


For the analisys i considered the returns, the last ones are:

0.0066998270  0.0090753250  0.0141900670  0.0089664010  0.0082031250

-0.0085238280 -0.0162172720  0.0022840120  0.0346774990  0.0118739830

  0.0052995170  0.0353007620 -0.0107292230  0.0027573530 -0.0021998170

 -0.0016535000  0.0083732060  0.0074824350


For modelling the mean i fit an ARMA(1,1) and fot the volatility
 i fit a GARCH(1,1) , i used a t-student as conditional distribution, 
for this i used the fGarch librray, the code is the following:


h<-garchFit(~arma(1,1)+garch(2,2),data=R,cond.dist="std",TRACE=F)


On the other hand, for the prediction i use the function "predict".

predict(h,10)

   meanForecast  meanError standardDeviation

1   0.001451401 0.015316820.01531682

2   0.001265062 0.015400830.01539350

3   0.001263344 0.015496280.01548892

4   0.001263328 0.015573060.01556565

5   0.001263328 0.015664200.01565676

6   0.001263328 0.015740620.01573312

7   0.001263328 0.015828000.01582047

8   0.001263328 0.015903720.01589614

9   0.001263328 0.015987790.01598018

10  0.001263328 0.016062580.01605493


I am modelling this Y_t-mean=e_t=sigma_t*Z_t

however, my question is ,the prediction for the return itself  is the mean 
forecast?

if this is the case my prediction for the price would be   equal to 
(1+.001451401)*110.41 =110.57

but i think this is not a good prediction, because   the volatility 
is not affecting so much  , in addition the predicted prices are growing
 up nevertheless i would expect that at some point these ones decrease .


So i would expect that the prediction for the return would be different. but i 
certanly dont know which is.


I would apreciate if you could help with this.


Greeting

Gustavo
  
[[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] Chemical Names in Data Frames

2011-09-02 Thread Gustavo Carvalho
?make.names perhaps.

On Fri, Sep 2, 2011 at 4:13 PM, Durant, James T. (ATSDR/DTEM/PRMSB)
 wrote:
> Greetings -
>
> I am working on some data that contain chemical names with air 
> concentrations, and I am creating a data frame with date/time and each 
> chemical having its own column. However, these are organic chemicals (e.g. 
> 1-butene, 2,3,4-trimethylbenzene etc). The package I am going to be using the 
> data with is openair, and many of the great functions require you to specify 
> a column name which does not seem to work with improper column names- e.g. 
> smoothTrend(mydata, pollutant="1-Butene" and smoothTrend(mydata, 
> pollutant=mydata[,"1-Butene"])
>
> I was wondering if there was a function to automatically convert these 
> chemical names (with all sorts of numbers and minuses in the beginning) to 
> something openair can handle?  Or am I going to be stuck recoding several 
> hundred chemical names in my database?
>
> VR
>
> Jim
>
> James T. Durant, MSPH CIH
> Emergency Response Coordinator
> US Agency for Toxic Substances and Disease Registry
> Atlanta, GA 30341
> 770-378-1695
>
>
>
>
>
>        [[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] Help: extrac the first entry for each component of a list

2011-08-23 Thread Gustavo Carvalho
sapply(a, `[`, 1)

On Wed, Aug 24, 2011 at 12:18 AM, Chee Chen  wrote:
> Dear All,
> I would like to know, beside writing a function and then apply it to a list, 
> or using a for loop, whether there is a one-line command to do the following.
> Suppose we have a list, each of whose components are numeric:
>> a <- vector("list",3)
>> a[[1]] <-c(1,2)
>> a[[2]] <-c(3,4)
>> a[[3]] <- c(5,6)
>> a
> [[1]]
> [1] 1 2
>
> [[2]]
> [1] 3 4
>
> [[3]]
> [1] 5 6
>
> Target: I would like to extract from each of its components the first entry 
> and store them into a vector, ie, to extract 1 from a[[1]], 3 from a[[2]], 5 
> from a[[3]], and store 1,3,5 into a vector without using for loops.
> Thank you,
> Chee
>        [[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] How to call an external program/web page under R for Mac OS?

2011-05-24 Thread Gustavo Carvalho
To open a website on the default browser:

system("open http://www.google.com";)

Gustavo.

On Tue, May 24, 2011 at 7:56 PM, jbrezmes  wrote:
> I would like to be able to call external programs such as Java scripts (*.jar
> files) or bring up the browser to a given direction. Can that be done from
> R?
>
> I am running R on a mac OS X system.
>
> Thanks again for any suggestions or solutions.
>
> Best regards,
>
> Jesus Brezmes
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/How-to-call-an-external-program-web-page-under-R-for-Mac-OS-tp3548479p3548479.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Find number of elements less than some number: Elegant/fast solution needed

2011-04-14 Thread Gustavo Carvalho
This might be a bit quicker with larger vectors:

f <- function(x, y) sum(x > y)
vf <- Vectorize(f, "x")
vf(x, y)

On Thu, Apr 14, 2011 at 5:37 PM, Marc Schwartz  wrote:
> On Apr 14, 2011, at 2:34 PM, Kevin Ummel wrote:
>
>> Take vector x and a subset y:
>>
>> x=1:10
>>
>> y=c(4,5,7,9)
>>
>> For each value in 'x', I want to know how many elements in 'y' are less than 
>> 'x'.
>>
>> An example would be:
>>
>> sapply(x,FUN=function(i) {length(which(y> [1] 0 0 0 0 1 2 2 3 3 4
>>
>> But this solution is far too slow when x and y have lengths in the millions.
>>
>> I'm certain an elegant (and computationally efficient) solution exists, but 
>> I'm in the weeds at this point.
>>
>> Any help is much appreciated.
>>
>> Kevin
>>
>> University of Manchester
>>
>
>
> I started working on a solution to your problem above and then noted the one 
> below.
>
> Here is one approach to the above:
>
>> colSums(outer(y, x, "<"))
>  [1] 0 0 0 0 1 2 2 3 3 4
>
>
>
>>
>> Take two vectors x and y, where y is a subset of x:
>>
>> x=1:10
>>
>> y=c(2,5,6,9)
>>
>> If y is removed from x, the original x values now have a new placement 
>> (index) in the resulting vector (new):
>>
>> new=x[-y]
>>
>> index=1:length(new)
>>
>> The challenge is: How can I *quickly* and *efficiently* deduce the new 
>> 'index' value directly from the original 'x' value -- using only 'y' as an 
>> input?
>>
>> In practice, I have very large matrices containing the 'x' values, and I 
>> need to convert them to the corresponding 'index' if the 'y' values are 
>> removed.
>
>
> Something like the following might work, if I correctly understand the 
> problem:
>
>> match(x, x[-y])
>  [1]  1 NA  2  3 NA NA  4  5 NA  6
>
>
> HTH,
>
> Marc Schwartz
>
> __
> 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] confirmatory factor analysis program in R

2011-03-20 Thread Gustavo Carvalho
Search for lavaan, sem, and OpenMx.

On Mon, Mar 21, 2011 at 12:34 AM, rvohen  wrote:
> thank you !  I will try it !
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/confirmatory-factor-analysis-program-in-R-tp3386133p3392279.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Incorrect degrees of freedom in SEM model using lavaan

2011-03-17 Thread Gustavo Carvalho
Your model is saturated.

I think lavaan calculates the number of degrees of freedom this way:

DF = n*(n + 1)/2 - t - n.fix*(n.fix + 1)/2

n = number of variables
t = number of free parameters
n.fix = number of fixed exogenous variables

So, if you fix the exogenous variables, as in mimic = "Mplus":

DF = 28 - 13 - 15 = 0

If you don't, as in mimic = "EQS":

DF = 28 - 28 - 0 = 0

You are probably not considering the variances and covariances of the
exogenous variables to arrive at 15 degrees of freedom:

model = '

Internet + Stress3 ~ HHincome + Race + Age + Gender + Stress1

Stress3 ~ Internet

HHincome ~~ 0*Race + 0*Age + 0*Gender + 0*Stress1
Race ~~  0*Age + 0*Gender + 0*Stress1
Age ~~ 0*Gender + 0*Stress1
Gender ~~  0*Stress1

HHincome ~~ 1*HHincome
Race ~~ 1*Race
Age ~~ 1*Age
Gender ~~ 1*Gender
Stress1 ~~ 1*Stress1
'

fit = lavaan(model, sample.nobs=161, sample.cov=myCov, int.ov.free =
T, int.lv.free = F, auto.fix.first = T, auto.fix.single = T,
auto.resid.var = T, auto.cov.lv.x = T, auto.cov.y = T,
mimic = "EQS")
summary(fit)

Cheers,

Gustavo.



On Thu, Mar 17, 2011 at 9:50 AM, Andrew Miles  wrote:
> I have been trying to use lavaan (version 0.4-7) for a simple path model,
> but the program seems to be computing far less degrees of freedom for my
> model then it should have.  I have 7 variables, which should give (7)(8)/2 =
> 28 covariances, and hence 28 DF.  The model seems to only think I have 13
> DF.  The code to reproduce the problem is below.  Have I done something
> wrong, or is this something I should take to the developer?
>
>
> library(lavaan)
>
> myCov = matrix(c(24.40, 0, 0, 0, 0, 0, 0, .03, .03, 0, 0, 0, 0, 0, 6.75, -
> .08, 519.38, 0, 0, 0, 0, .36, .01, 2.74, .18, 0, 0, 0, .51, .0, -.31, .02,
> .2, .0, 0, -.17, .0, -1.6, -.04, .01, .25, 0, -.1, .02, -.03, .0, -.01, .01
> , .02), nrow=7, byrow=TRUE, dimnames=list(c("Internet", "Stress3",
> "HHincome", "Race", "Age", "Gender", "Stress1"), c("Internet", "Stress3",
> "HHincome", "Race", "Age", "Gender", "Stress1")))
>
>
> model = '
>
> Internet ~ HHincome + Race + Age + Gender + Stress1
>
> Stress3 ~ Internet + HHincome + Race + Age + Gender + Stress1
>
> '
>
>
> fit=sem(model, sample.nobs=161, sample.cov=myCov, mimic.Mplus=FALSE)
>
>
> #check the number of parameters being estimated
>
> inspect(fit, what="free")
>
>
> #Note the DF for the Chi-square is 0, when it should be 28-13 = 15
>
> summary(fit)
>
>
> Andrew Miles
>
>        [[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] Is there an implementation for "URL Encoding" (/format) in R?

2010-11-25 Thread Gustavo Carvalho
?URLencode

On Thu, Nov 25, 2010 at 3:53 PM, Tal Galili  wrote:
> Hello all,
>
> I would like some R function that can translate a string to a "URL encoding"
> (see here: http://www.w3schools.com/tags/ref_urlencode.asp)
>
> Is it implemented? (I wasn't able to find any reference to it)
>
> Thanks,
> Tal
>
>
>
>
>
> Contact
> Details:---
> Contact me: tal.gal...@gmail.com |  972-52-7275845
> Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) |
> www.r-statistics.com (English)
> --
>
>        [[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] the first. from SAS in R

2010-11-23 Thread Gustavo Carvalho
Perhaps something like this:

a$d <- ifelse(duplicated(a$a), 0, 1)

On Tue, Nov 23, 2010 at 1:33 PM, Joel  wrote:
>
> Is there any similar function in R to the first. in SAS?
>
> What it dose is:
>
> Lets say we have this table:
>
>  a b  c
>  1 1  5
>  1 0  2
>  2 0  2
>  2 0 NA
>  2 9  2
>  3 1  3
>
>
> and then I want do to do one thing the first time the number 1 appers in a
> and something else the secund time 1 appers in a and so on.
>
> so
>
> something similar to:
>
> if first.a {
>  a$d<-1
> }else{
>  a$d<-0
> }
>
> This would give me
>
>  a b  c b
>  1 1  5 1
>  1 0  2 0
>  2 0  2 1
>  2 0 NA 0
>  2 9  2 0
>  3 1  3 1
>
> Is there such a function in R or anything similar?
>
>
> thx
>
> //Joel
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/the-first-from-SAS-in-R-tp3055417p3055417.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] looking for a faster way to compare two columns of a matrix

2010-09-23 Thread Gustavo Carvalho
Please consider this matrix:

x <- structure(c(5, 4, 3, 2, 1, 6, 3, 2, 1, 0, 3, 2, 1, 0, 0, 2, 1,
1, 0, 0, 2, 0, 0, 0, 0), .Dim = c(5L, 5L))

For each pair of columns, I want to calculate the proportion of entries
different than 0 in column j (i > j) that have lower values than the entries
in the same row in column i:

x[, 1:2]
sum((x[,1] > x[,2]) & (x[,2] > 0))/sum(x[,2] > 0)

Thus, for this pair, 3 of the 4 entries in the second column are
lower than the entries in the same row in the first column.

When both columns of a given pair have the same number of cells different than
0, the value of the metric is 0.

x[, 3:4]
colSums(x[, 3:4] > 0)

The same if column j has more valid (> 0) entries.

I've been doing this using this idea:

combinations <- combn(1:ncol(x), 2)
values <- numeric(ncol(combinations))

for (i in 1:ncol(combinations)) {
  pair <- combinations[,i]
  first <- x[, pair[1]]
  second <- x[, pair[2]]
  if (sum(first > 0) <= sum(second > 0)) next
  values[i] <- sum(first - second > 0 & second > 0) / sum(second > 0)
}
values

Anyway, I was wondering if there is a faster/better way. I've tried
putting the code from
the for loop into a function and passing it to combn but, as expected, it didn't
help much. Any pointers to functions that I should be looking into will be
greatly appreciated.

Thank you very much,

Gustavo.

__
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] Finding (Ordered Subvectors)

2010-09-21 Thread Gustavo Carvalho
This function might be helpful:

bleh <- function(a, b) {
  where <- list()
  matches <- 0
  first <- which(a == b[1])
  for (i in first) {
seq.to.match <- seq(i, length = length(b))
if (identical(a[seq.to.match], b)) {
  matches <- matches + 1
  where[[matches]] <- seq.to.match
}
  }
  return(where)
}

a<-c(3,4,3,0,4,5,6,9,3,4)
b<-c(0,4,5)
c<-c(5,4,0)
d<-c(3,4)
bleh(a, b)
bleh(a, c)
bleh(a, d)

Cheers,

Gustavo.

On Tue, Sep 21, 2010 at 11:31 AM, Lorenzo Isella
 wrote:
> Dear All,
> Consider a simple example
>
> a<-c(1,4,3,0,4,5,6,9,3,4)
> b<-c(0,4,5)
> c<-c(5,4,0)
>
> I would like to be able to tell whether a sequence is contained (the order
> of the elements does matter) in another one e.g. in the example above, b is
> a subsequence of a, whereas c is not. Since the order matters, I cannot
> treat the sequences above as sets (also, elements are repeated).
> Does anyone know a smart way of achieving that?
> Many thanks
>
> Lorenzo
>
> __
> 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] problems trying to reproduce structural equation model using the sem package

2010-09-16 Thread Gustavo Carvalho
Hello,

I've been unsuccessfully trying to reproduce a sem from Grace et al.
(2010) published in Ecological Monographs:

http://www.esajournals.org/doi/pdf/10.1890/09-0464.1

The model in question is presented in Figure 8, page 81. The errors
that I've been getting are:

1. Using a correlation matrix:

res.grace <- sem(grace.model, S = grace, N = 190)
Warning message:
In sem.default(ram = ram, S = S, N = N, param.names = pars, var.names = vars,  :
 Could not compute QR decomposition of Hessian.
Optimization probably did not converge.

2. Using a variances/covariances matrix:

res.grace <- sem(grace.model, S = grace.cov, N = 190)
Error in solve.default(C) :
 Lapack routine dgesv: system is exactly singular
In addition: Warning messages:
1: In log(det(C)) : NaNs produced
2: In sem.default(ram = ram, S = S, N = N, param.names = pars,
var.names = vars,  :
  singular Hessian: model is probably underidentified.
(...)

So far I've tried:

1. Fixing the variances of the latent variables
2. Allowing the exogenous indicators to covary (fixed.x parameter in sem())
3. Manually inserting the published parameter estimates during model
specification (specify.model()) to see if the starting parameters
passed to nlm were the problem
4. Extensively looking for typing mistakes

Anyway, there seems to be a problem either with the way I specified
the model or with the model itself as it has been published. I can see
that the number of degrees of freedom in  the model that I've
specified is 21, as in the published model.

Any light you could shed on this would be greatly appreciated. The
code to reproduce all steps is presented below.

Thank you very much,

Gustavo.

##

library(sem)

grace <- matrix(ncol = 10, nrow = 10)

variables <- c("lightlog", "light", "dstb", "species_count", "masslog",
              "soil_carbon", "soil_organic", "soil_low_flooding",
              "soil_high_flooding", "soil_salinity")

rownames(grace) <- colnames(grace) <- variables

diag(grace) <- 1

## Coefficients from the paper.

grace.coefficients <- c(0.858, 0.667, -0.251, -0.699, 0.06, 0.012, 0.552, 0.547,
                       0.327, 0.776, -0.404, -0.794, 0.157, 0.120,
0.439, 0.462,
                       0.321, -0.228, -0.686, 0.218, 0.186, 0.249,
0.290, 0.216,
                       0.291, 0.119, 0.132, -0.374, -0.406, -0.292,
-0.096, -0.071,
                       -0.426, -0.466, -0.138, 0.973, -0.170, -0.150,
0.249, -0.211,
                       -0.188, 0.244, 0.959, 0.073, 0.052)

grace.sds <- c(1.11, 0.285, 3.29, 3.33, 1.44, 0.605, 1.23, 1.33, 1.27, 1.68)

grace[lower.tri(grace, diag = F)] <- grace.coefficients
grace[upper.tri(grace, diag = F)] <- t(grace)[upper.tri(grace, diag = F)]

## Covariances matrix

grace.cov <- outer(grace.sds, grace.sds) * grace

## Specifying the model

grace.model <- specify.model()
salinity -> soil_salinity, NA, 1
flooding -> soil_high_flooding, NA, 1
flooding -> soil_low_flooding, flooding_low_flooding, NA
infertility -> soil_organic, NA, -1
infertility -> soil_carbon, inf_carbon, NA
disturbance -> dstb, NA, 1
biomass -> masslog, NA, 1
light -> light_effect, NA, 1
lightlog -> light_effect, lightlog_light_effect, NA
richness -> species_count, NA, 1
salinity -> richness, salinity_richness, NA
salinity -> light, salinity_light, NA
flooding -> richness, flooding_richness, NA
flooding -> biomass, flooding_biomass, NA
infertility -> richness, infertility_richness, NA
disturbance -> biomass, disturbance_biomass, NA
disturbance -> light, disturbance_light, NA
biomass -> light, biomass_light, NA
light_effect -> richness, light_effect_richness, NA
salinity <-> flooding, salinity_flooding, NA
salinity <-> infertility, salinity_infertility, NA
salinity <-> disturbance, salinity_disturbance, NA
flooding <-> infertility, flooding_infertility, NA
flooding <-> disturbance, flooding_disturbance, NA
infertility <-> disturbance, infertility_disturbance, NA
biomass <-> biomass, biomass, NA
masslog <-> masslog, masslog, NA
light <-> lightlog, light_lightlog, NA
light_effect <-> light_effect, NA, 0
richness <-> richness, richness, NA
species_count <-> species_count, species_count, NA
light <-> light, light, NA
lightlog <-> lightlog, NA, 1
salinity <-> salinity, salinity, NA
disturbance <-> disturbance, disturbance, NA
flooding <-> flooding, flooding, NA
infertility <-> infertility, infertiliy, NA
soil_salinity <-> soil_salinity, soil_salinity, NA
soil_high_flooding <-> soil_high_flooding, soil_high, NA
soil_low_flooding <-> soil_low_flooding, soil_low, NA
soil_organic <-> soil_organic, soil_organic, NA
soil_carbon <-> soil_carbon, soil_carbon, NA
dstb <-> dstb, dstb, NA

res.grace <- sem(grace.model, S = grace.cov, N = 190)

__
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] testing hipotheses using GAMLSS package

2010-03-15 Thread Gustavo Pereira

  Hi all.
  In a GLM in which g(mu) = b0 + b1X1 + b2X2 + b3X3 + b4X4 + b5X5 + b6X6 + 
b7X7, if I want to test if b1 + b5 = b2 + b6, I can use the contrast package or 
multicomp package. How can I do a similar test if I am fitting a GAMLSS using 
the gamlss package? Thank you for your help. Gustavo

__
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] Logical function to turn missing values to 0's

2009-01-14 Thread Gustavo Carvalho
Hello rafamoral,

Try this:

ifelse(is.na(x),0,x)

On Wed, Jan 14, 2009 at 8:32 PM, rafamoral  wrote:
>
> I have a dataset which contains some missing values, and I need to replace
> them with zeros. I tried using the following:
>
> x <- matrix(data=rep(c(1,2,3,NA),6), ncol=6, nrow=6)
>
> y <- matrix(data=0, ncol=ncol(x), nrow=nrow(x))
>
> for(i in 1:nrow(x)) {
>
> for(j in 1:ncol(x)) {
>
> y[i,j] <- ifelse(x[i,j]==NA, 0, x[i,j])
>
> }}
>
> But y returns an NA matrix.
> I'd appreciate any help.
> --
> View this message in context: 
> http://www.nabble.com/Logical-function-to-turn-missing-values-to-0%27s-tp21466785p21466785.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] remove columns containing all zeros (or other value)

2009-01-14 Thread Gustavo Carvalho
Sorry for the double post, but this is probably faster:

x[, colSums(x) != 0]

On Wed, Jan 14, 2009 at 8:22 PM, Gustavo Carvalho
 wrote:
> You can also try this:
>
> x[,-(which(colSums(x) == 0))]
>
> Cheers,
>
> Gustavo.
>
> On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick  wrote:
>> Hello-
>>
>> I would like to remove the columns of a matrix that contain all zeros. For
>> example, from
>> x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3)
>>
>> I would like to remove the third column. However, because this is in a loop
>> I need a way to first determine which columns are all zeros, and only then
>> remove them. I.e., I don't know which column of x contains all zeros until
>> after x is created.
>>
>> Thanks!
>>
>> Anthony
>>
>> --
>> Anthony Steven Dick, Ph.D.
>> Post-Doctoral Fellow
>> Human Neuroscience Laboratory
>> Department of Neurology
>> The University of Chicago
>> 5841 S. Maryland Ave. MC-2030
>> Chicago, IL 60637
>> Phone: (773)-834-7770
>> Email: ad...@uchicago.edu
>> Web: http://home.uchicago.edu/~adick/
>>
>> __
>> 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] remove columns containing all zeros (or other value)

2009-01-14 Thread Gustavo Carvalho
You can also try this:

x[,-(which(colSums(x) == 0))]

Cheers,

Gustavo.

On Wed, Jan 14, 2009 at 8:01 PM, Anthony Dick  wrote:
> Hello-
>
> I would like to remove the columns of a matrix that contain all zeros. For
> example, from
> x<-matrix(c(1,5,3,2,1,4,0,0,0), ncol=3,nrow=3)
>
> I would like to remove the third column. However, because this is in a loop
> I need a way to first determine which columns are all zeros, and only then
> remove them. I.e., I don't know which column of x contains all zeros until
> after x is created.
>
> Thanks!
>
> Anthony
>
> --
> Anthony Steven Dick, Ph.D.
> Post-Doctoral Fellow
> Human Neuroscience Laboratory
> Department of Neurology
> The University of Chicago
> 5841 S. Maryland Ave. MC-2030
> Chicago, IL 60637
> Phone: (773)-834-7770
> Email: ad...@uchicago.edu
> Web: http://home.uchicago.edu/~adick/
>
> __
> 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] Conditional Counting with Table

2008-12-23 Thread Gustavo Carvalho
Hello,

Something like this should work:

table(test$V1[!test$V2 %in% c("NM","QC")])

Cheers,

Gustavo.

On Wed, Dec 24, 2008 at 3:06 AM, Gundala Viswanath  wrote:
> Dear all,
>
> I have the following data frame:
>
> V1 V2
> aaachr1
> aaachr2
> aaaNM
> aaaQC
> aaachr10
> att  NM
> att  chr7
>
> What I want to do is to count the string (V1).
> But the condition of counting is: if the V2 of the string
> is "NM" or "QC"  then the count is not increased.
>
> Hence the contigency table will look like this:
>
> #tag   count
> aaa  3
> att1
>
> Is there a compact way to achieve that in R?
> I am thinking of using "table" but can't see
> how to impose such condition into it.
>
>
> - Gundala Viswanath
> Jakarta - Indonesia
>
> __
> 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] Reading from Google Docs

2008-12-15 Thread Gustavo Carvalho
Hello,

You can probably extract a .tar.gz using 7zip on Windows.

Regards,

Gustavo.

On Mon, Dec 15, 2008 at 8:07 PM, Farrel Buchinsky  wrote:
> I saw a thread from September 24 in which Duncan Temple Lang told us:
> - The package currently has no Rd files, but there is a brief "user's
> guide". The package is available from
> http://www.omegahat.org/RGoogleDocs
>
> I could not find it by using Tinn-R or RGui's package install tool.
> Then when I went to the website I saw that package is only available
> as
> http://www.omegahat.org/RGoogleDocs/RGoogleDocs_0.1-0.tar.gz
>
> To my knowledge tar.gz is only for Linux. Does this mean that I cannot
> run it on a windows machine. Please tell me that there is a way to run
> it on a Windows machine.
>
> Farrel Buchinsky
> GrandCentral Tel: (412) 567-7870
>
> __
> 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] Logical "in" test

2008-12-11 Thread Gustavo Carvalho
Take a look at ?any.

On Thu, Dec 11, 2008 at 3:11 PM, David B. Thompson, Ph.D., P.E.,
D.WRE, CFM <[EMAIL PROTECTED]> wrote:
> OK, this should be trivial but I'm not finding it. I want to compress the
> test,
>
> if (i==7 | i==10 | i==30 | i==50) {}
>
> into something like
>
> if (i in c(7,10,30,50)) {}
>
> so I can build a "excludes" vector
>
> excludes <- c(7,10,30,50)
>
> and test
>
> if (i in excludes) {}
>
> However, I'm not finding a clue on how to accomplish this, if it can be
> done. Would someone with more R experience lend a helping hand please? A
> reference (so I can continue learning) would also be appreciated.
>
> Thanks...
>
> -=d
>
> David Thompson, Ph.D., P.E., D.WRE, CFM
> Civil Engineer/Hydrologist
>
> __
> 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] Logical inconsistency

2008-12-10 Thread Gustavo Carvalho
Hello,

An alternative to round():

isTRUE(all.equal((2.3-1.3),1))

Regards,

Gustavo.

On Wed, Dec 10, 2008 at 3:12 PM, Stephan Kolassa <[EMAIL PROTECTED]> wrote:
> Hi Emma,
>
> unfortunately, rounding variables before taking the difference will not
> solve your problem, because the *rounded* variables are subject to the same
> (effectively) random internal representation. Examples:
>
>> round(8.3,20)-round(7.3,20)>=1
> [1] TRUE
>> round(2.3,20)-round(1.3,20)>=1
> [1] FALSE
>> round(8.3,1)-round(7.3,1)>=1
> [1] TRUE
>> round(2.3,1)-round(1.3,1)>=1
> [1] FALSE
>
> I'm afraid you will need to think about what you really need your comparison
> for and whether this problem really affects your results. For example, if
> you are doing a large number of such comparisons, the effect may only affect
> a few of them and basically average out (sometimes going one way, sometimes
> the other), so the end results may be stable.
>
> Good luck!
> Stephan
>
>
> emma jane schrieb:
>>
>> Thanks Greg, that does make sense.  And I've solved the problem by
>> rounding the variables before taking the difference between them.
>>
>> Thanks to all who replied.
>>
>> Emma JaneÂ
>>
>>
>>
>> 
>> From: Greg Snow <[EMAIL PROTECTED]>
>>
>> .com.br>; Wacek Kusnierczyk <[EMAIL PROTECTED]>; Chuck
>> Cleland <[EMAIL PROTECTED]>
>> Cc: R help <[EMAIL PROTECTED]>
>> Sent: Tuesday, 9 December, 2008 16:30:08
>> Subject: RE: [R] Logical inconsistency
>>
>> Some (possibly all) of those numbers cannot be represented exactly, so
>> there is a chance of round off error whenever you do some arithmetic,
>> sometimes the errors cancel out, sometimes they don't.  Consider:
>>
>>> print(8.3-7.3, digits=20)
>>
>> [1] 1.001
>>>
>>> print(11.3-10.3, digits=20)
>>
>> [1] 1
>>
>> So in the first case the rounding error gives a value that is slightly
>> greater than 1, so the greater than test returns true (if you round the
>> result before comparing to 1, then it will return false).  In the second
>> case the uncertainties cancelled out so that you get exactly 1 which is not
>> greater than 1 an so the comparison returns false.
>>
>> Hope this helps,
>>
>> --
>> Gregory (Greg) L. Snow Ph.D.
>> Statistical Data Center
>> Intermountain Healthcare
>> [EMAIL PROTECTED]
>> 801.408.8111
>>
>>
>>> -Original Message-
>>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
>>> project.org] On Behalf Of emma jane
>>> Sent: Tuesday, December 09, 2008 7:02 AM
>>> To: Bernardo Rangel Tura; Wacek Kusnierczyk; Chuck Cleland
>>> Cc: R help
>>> Subject: Re: [R] Logical inconsistency
>>>
>>> Many thanks for your help, perhaps I should have set my query in
>>> context  !
>>>
>>> I'm simply calculating an indicator variable [0,1] based on the whether
>>> the difference between two measured variables is > 1 or <=1.
>>>
>>> I understand the FAQ about floating point arithmetic, but am still
>>> puzzled that it only apparently applies to certain elements, as
>>> follows:
>>>
>>> 8.8 - 7.8 > 1
>>>>
>>>> TRUE
>>>
>>> 8.3 - 7.3 > 1
>>>>
>>>> TRUE
>>>
>>> However,
>>>
>>> 10.2 - 9.2 > 1
>>>>
>>>> FALSE
>>>
>>> 11.3 - 10.3>1
>>>>
>>>> Â FALSE
>>>
>>> Emma Jane
>>>
>>>
>>>
>>>
>>> 
>>> From: Bernardo Rangel Tura <[EMAIL PROTECTED]>
>>> To: Wacek Kusnierczyk <[EMAIL PROTECTED]>
>>> Cc: R help <[EMAIL PROTECTED]>
>>> Sent: Saturday, 6 December, 2008 10:00:48
>>> Subject: Re: [R] Logical inconsistency
>>>
>>> On Fri, 2008-12-05 at 14:18 +0100, Wacek Kusnierczyk wrote:
>>>>
>>>> Berwin A Turlach wrote:
>>>>>
>>>>> Dear Emma,
>>>>>
>>>>> On Fri, 5 Dec 2008 04:23:53 -0800 (PST)
>>>>>
>>>>>> Please could someone kindly explain the following inconsistencies
>>>>>> I've discovered__when performing logical calculations in R:
>>>>>>
>>>>>> 8.8 - 7.8 > 1
>>>>>>
>>>>>

Re: [R] == operand

2008-12-09 Thread Gustavo Carvalho
Hello,

You assigned 53 to c, not cc. Also, take a look at this:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f


On Tue, Dec 9, 2008 at 9:36 PM, Renny Li <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am trying to compare two values using "==" operand, please take a look of 
> the following example (I copied ALL what I did here without deleting any line)
>
>>bb<-1
>> cc<-50
>> cc==abs(bb+52)
> [1] FALSE
>> C<-53
>> C<-53
>> c<-53
>> cc==abs(bb+52)
> [1] FALSE
>
> I am expecting to see a TRUE here. Then I tried another way,
>
>> abs(1+52)
> [1] 53
>> cc==abs(1+52)
> [1] FALSE
>
> Why it is FALSE. Then I tried
>
>
>> d<-abs(b+52)
> Error: object "b" not found
>> d<-abs(bb+52)
>> d
> [1] 53
>> c==d
> [1] TRUE
>
> Now it is TRUE, but a wired thing is as following,
>> c==abs(bb+52)
> [1] TRUE
>
> The coding "c==abs(bb+52)" is just the same from the 3line in the top, why 2 
> different results? HOWEVER, it is not repeatable, I am glad I did not close 
> the window.
>
> Also,
>> c<-0.5-0.1
>> n<-0.6-0.2
>> n==c
> [1] FALSE
>
> So, is "==" comparing formula or result?
>
> Thanks,
> Renny
>
>
>
>[[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] extract the digits of a number

2008-12-09 Thread Gustavo Carvalho
Hello,

Anyone knows how can I do this in a cleaner way?

mynumber = 1001
as.numeric(unlist(strsplit(as.character(mynumber),"")))
[1] 1 0 0 1

Thanks in advance,

Gustavo

__
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] Running R Script on a Sequence of Files

2008-12-06 Thread Gustavo Carvalho
Thanks a lot!

On Fri, Dec 5, 2008 at 5:54 PM, Gabor Grothendieck
<[EMAIL PROTECTED]> wrote:
> Try this:
>
> dir()[!file.info(dir())$isdir]
>
>
> On Fri, Dec 5, 2008 at 2:30 PM, Gustavo Carvalho
> <[EMAIL PROTECTED]> wrote:
>> Is there a way to list only the files in a given directory without
>> passing pattern="..." to list.files()?
>>
>> On Fri, Dec 5, 2008 at 5:10 PM, Kyle. <[EMAIL PROTECTED]> wrote:
>>> Thanks, Barry. I'll use that in the future.
>>>
>>>
>>> ---Kyle.
>>>
>>> On Fri, Dec 5, 2008 at 11:01 AM, Barry Rowlingson <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>>> 2008/12/5 Chris Poliquin <[EMAIL PROTECTED]>:
>>>> > Hi,
>>>> >
>>>> > I have about 900 files that I need to run the same R script on.  I looked
>>>> > over the R Data Import/Export Manual and  couldn't come up with a way to
>>>> > read in a sequence of files.
>>>> >
>>>> > The files all have unique names and are in the same directory.  What I
>>>> want
>>>> > to do is:
>>>> > 1) Create a list of the file names in the directory (this is really what
>>>> I
>>>> > need help with)
>>>> > 2) For each item in the list...
>>>> >a) open the file with read.table
>>>> >b) perform some analysis
>>>> >c) append some results to an array or save them to another file
>>>> > 3) Next File
>>>> >
>>>> > My initial instinct is to use Python to rename all the files with numbers
>>>> > 1:900 and then read them all, but the file names contain some information
>>>> > that I would like to keep intact and having to keep a separate database
>>>> of
>>>> > original names and numbers seems inefficient.  Is there a way to have R
>>>> read
>>>> > all the files in a directory one at a time?
>>>>
>>>>  I can't believe the two 'solutions' already posted. It's easy:
>>>>
>>>>  ?list.files
>>>>
>>>> Barry
>>>>
>>>> __
>>>> 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.
>>>
>>
>> __
>> 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] Running R Script on a Sequence of Files

2008-12-05 Thread Gustavo Carvalho
Is there a way to list only the files in a given directory without
passing pattern="..." to list.files()?

On Fri, Dec 5, 2008 at 5:10 PM, Kyle. <[EMAIL PROTECTED]> wrote:
> Thanks, Barry. I'll use that in the future.
>
>
> ---Kyle.
>
> On Fri, Dec 5, 2008 at 11:01 AM, Barry Rowlingson <
> [EMAIL PROTECTED]> wrote:
>
>> 2008/12/5 Chris Poliquin <[EMAIL PROTECTED]>:
>> > Hi,
>> >
>> > I have about 900 files that I need to run the same R script on.  I looked
>> > over the R Data Import/Export Manual and  couldn't come up with a way to
>> > read in a sequence of files.
>> >
>> > The files all have unique names and are in the same directory.  What I
>> want
>> > to do is:
>> > 1) Create a list of the file names in the directory (this is really what
>> I
>> > need help with)
>> > 2) For each item in the list...
>> >a) open the file with read.table
>> >b) perform some analysis
>> >c) append some results to an array or save them to another file
>> > 3) Next File
>> >
>> > My initial instinct is to use Python to rename all the files with numbers
>> > 1:900 and then read them all, but the file names contain some information
>> > that I would like to keep intact and having to keep a separate database
>> of
>> > original names and numbers seems inefficient.  Is there a way to have R
>> read
>> > all the files in a directory one at a time?
>>
>>  I can't believe the two 'solutions' already posted. It's easy:
>>
>>  ?list.files
>>
>> Barry
>>
>> __
>> 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.
>

__
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] if then statement problem

2008-11-27 Thread Gustavo Carvalho
If I understood your problem correctly, you are just missing a couple of things:

q = which(apply(p.unique,2,function(x)all(x==r)) == TRUE)

Also, you should probably change this line:

if(q>0){c=p.unique[,q]};{c=c(0,0,0)}

To something like this:

if(length(q)>0){c=p.unique[,q]};{c=c(0,0,0)}

Regards,

Gustavo

On Thu, Nov 27, 2008 at 3:02 PM, Salas, Andria Kay <[EMAIL PROTECTED]> wrote:
> I really need help with an if then statement I am having trouble with.  A 
> brief explanation of what I am trying to do: I have a p matrix of all 
> permutations of a vector of length 3 filled with either 1s or -1s (these make 
> up the columns).  The p.unique matrix is the unique columns of the p matrix; 
> here, they are identical but in my real script this will not always be the 
> case.  I want to be able to take a randomly generated vector and see which 
> column in the p.unique matrix, if any, match the random vector.  If there is 
> a match, I get an integer returned that tells me which column is the match.  
> If there is no match, I get the response integer (0).  I want to write an if 
> then statement that allows me to do different things based upon if there is a 
> match to one of the columns in the p.unique matrix or not.  Below is a sample 
> script plucked from bits of my "master script" that show this problem.  The 
> vector "r" was made to not match any columns in p.unique.  If it did, "c" w!
 ou!
>  ld equal the integer representing the matching column, and since it does 
> not, I want c to equal the vector (0,0,0).  My if then statement is not 
> working (I keep getting the error that my argument is of length zero), so I 
> would really appreciate any help anyone can provide me.  Thanks for all of 
> your help previously and in advance for the help with this problem!!  Happy 
> Thanksgiving!
>
> p=as.matrix(do.call(expand.grid,rep(list(c(-1,1)),3)))
> p=t(p)
> p.unique=unique(p,MARGIN=2)
>
> r=c(2,2,2)
> q=which(apply(p.unique,2,function(x)all(x==r)))
> if(q>0){c=p.unique[,q]};{c=c(0,0,0)}
>
> __
> 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] R course in Scotland

2008-11-20 Thread Gustavo Carvalho
Hello,

Take a look at this course:

http://www.r4all.group.shef.ac.uk/index.html

I don't think they teach tools for working with the genome, but it
might be helpful anyway.

On Thu, Nov 20, 2008 at 11:16 AM, Peter Saffrey <[EMAIL PROTECTED]> wrote:
> (apologies if this is the wrong list)
>
> I'm a bioinformatician looking for a course in using R, in particular the
> tools for working with the genome - I've heard they're lightning fast. I'm
> in Glasgow, but I've tried the Robertson centre for biostatistics and they
> use minitab.
>
> If anybody knows of a course, I would be grateful. Glasgow or Edinburgh
> would be preferable, but anywhere in the UK will do if it's a good course.
>
> Thanks,
>
> Peter
>
> __
> 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] Checking collinearity using lmer

2008-11-19 Thread Gustavo Carvalho
Take a look at vif in the package car.

On Wed, Nov 19, 2008 at 10:00 PM, Crystal McRae <[EMAIL PROTECTED]> wrote:
>
>
>  I am running a logistic regression model with a random effect using lmer.  I 
> am uncertain how to check for collinearity between my parameters.  I have 
> already run cor() and linear regression for each combination of parameters, 
> and all Rsqr values were <0.8….but I am analyzing ecological data so a 0.8 
> cutoff may be unrealistic.
>
> -is there a way to check variance inflation factors or tolerance using 
> lmer?-Or is there a better way?
> _
>
>
>[[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] Problem with the Linux R 2.8.0 rpm for 64 bit REL 4

2008-11-18 Thread Gustavo Carvalho
xdg utils is probably not being recognized because you compiled it
from source. The R rpm is looking for the xdg utils package. I'm not
familiar with yum, but I think you can try to force the installation:

rpm -ivh --force (or something like that) /data/R-2.8.0-1.rh4.x86_64.rpm

On Tue, Nov 18, 2008 at 2:21 PM, Peter Tait <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am trying to update my version of R on Centos 4.
>
>  $uname -a
>
>  Linux 2.6.9-78.0.5.ELsmp #1 SMP Wed Oct 8 07:06:30 EDT 2008 x86_64 x86_64
> x86_64 GNU/Linux
>
> I tried to update the current version of R (2.6.2) which was installed
> locally as an rpm
>  $R --version
>  R version 2.6.2 (2008-02-08)
>  Copyright (C) 2008 The R Foundation for Statistical Computing
>  ISBN 3-900051-07-0
>
> I used yum to try to do the update with the following error
>
> $su -c "yum localupdate /data/R-2.8.0-1.rh4.x86_64.rpm"
>  Error: Missing Dependency: xdg-utils is needed by package R
>
> I have installed the xdg-utils manually from
> http://portland.freedesktop.org/download/xdg-utils-1.0.2.tgz
>
> $cd /data/xdg-utils-1.0.2 --> this is a directory
> $./configure --prefix=/usr
> $su -c " make install"
> $cd /usr/bin
> $ ls -al | grep xdg --> shows the xdg utilities
> -rwxr-xr-x   1 root root  14806 Nov 18 10:43 xdg-desktop-icon
> -rwxr-xr-x   1 root root  39267 Nov 18 10:43 xdg-desktop-menu
> -rwxr-xr-x   1 root root  16177 Nov 18 10:43 xdg-email
> -rwxr-xr-x   1 root root  24129 Nov 18 10:43 xdg-icon-resource
> -rwxr-xr-x   1 root root  28015 Nov 18 10:43 xdg-mime
> -rwxr-xr-x   1 root root  10131 Nov 18 10:43 xdg-open
> -rwxr-xr-x   1 root root  19303 Nov 18 10:43 xdg-screensaver
>
> So the xdg-utils are installed but yum or the R rpm are not recognizing
> them? How do I solve this problem?
>
> Thanks
> Peter
>
> __
> 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] Simulate data based on correlation coefficient

2007-10-06 Thread Gustavo
Hello all,



I have a vector with 1000 values and I would like to generate other 
correlated vector, but with different correlation coefficient (for example, 
r = 0.7).

Any ideas how can I do this?



Regards,

Gustavo.






___ 

Experimente já e veja as novidades.

__
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] Editor for R under Fedora 7

2007-09-11 Thread Gustavo Carvalho
Fedora 7 is a linux distribution

On 9/11/07, Paul Smith <[EMAIL PROTECTED]> wrote:
> On 9/11/07, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> wrote:
> > I wanted to ask what will be a good editor to write R scripts in Fedora 7.
>
> Tell us first what is your operating system.
>
> 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.
>

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