[R] How can I find the principal components and run regression/forecasting using dynlm

2012-08-27 Thread jpm miao
Hello,

   I would like to write a program that compute the principal components of
a set of data and  then
   1. Run the dependent variable against the principal components (lagged
value)
   2. Do prediction
   , following Stock and Watson (1999) "Forecasting Inflation". All data
are time series.
   Now I can run the program using single factor (first principal
component), but I don't know how to write a program for muli-factor model
(first several principal component).

   The core subroutine called is dynlm, which runs OLS for time series. I
think the primary problem is that (1) I don't how to run dynlm with matrix
rather than vector as explanatory variables (2)I don't know how to do a
forecast with the estimation results of dynlm properly. In lm model,
function "predict.lm" can do it.

   For the case of first principal component (In order to accentuate the
main problem, I have simplify the codes):

  Mpca<-prcomp(M1, center=TRUE, scale =TRUE)  # M1 is the data matrix of
explanatory variables
  Mpca1st<-Mpca$x[,1]   # first principle component
  X<-as.matrix(Mpca1st)

  model<-dynlm(as.ts(y[(h+1):t]) ~ L(as.ts(X[1:(t-h)]), 0:i) +
L(as.ts(z[1:(t-h)]),0:j))   # y, X, z are a zoo objects defined earlier; i
and j, t, h are given earlier; h is the forecasting horizon; since dynlm
can't work for zoo object, the variables need to be transformed to ts
objects.

Xlast<-as.matrix(X[(t-h):(t-h-i+1)])
zlast<-t(as.matrix(z[(t-h):(t-h-j+1)])) # Make it a column matrix
beta<-as.matrix(model$coefficient)
pi1fore[t]<-pi1[t-h]+c(1, Xlast, zIlast)%*%beta # pi1fore is defined
earlier . I just use the inner product as a vehicle for forecasting. I
don't know if there's a way that the codes can be written neatly?
return(pi1fore)


Then I attempt to program similarly for the multi-factor case:

Mpca<-prcomp(M1, center=TRUE, scale =TRUE)  # M1 is the data matrix of
explanatory variables
Mpca1f<-Mpca$x[,(1:f)]   # first f principle components
X<-as.matrix(Mpca1f)
model<-dynlm(as.ts(y[(h+1):t]) ~ L(as.ts(X[1:(t-h),]), 0:i) +
L(as.ts(z[1:(t-h)]),0:j))  # Since X has more than one column, I add a
comma after 1:(t-h). I don't know if I am right here?
Xlast<-as.matrix(X[(t-h):(t-h-i+1), ])
zlast<-t(as.matrix(z[(t-h):(t-h-j+1)])) # Make it a column matrix
beta<-as.matrix(model$coefficient)
pi1fore[t]<-pi1[t-h]+c(1, Xlast, zIlast)%*%beta   # It does not seem to
work here! How can I modify the program...?.
   return(pi1fore)

[[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] for loop optimization help

2012-08-27 Thread Jinsong Zhao

On 2012-08-27 9:35, David Winsemius wrote:


On Aug 26, 2012, at 5:06 PM, Jinsong Zhao wrote:


Hi there,

In my code, there is a for loop like the following:

  pmatrix <- matrix(NA, nrow = 99, ncol = 1)
  qmatrix <- matrix(NA, nrow = 99, ncol = 3)
  paf <- seq(0.01, 0.99, 0.01)
  for (i in 1:1) {
  p.r.1 <- rnorm(1000, 1, 0.5)
  p.r.2 <- rnorm(1000, 2, 1.5)
  p.r.3 <- rnorm(1000, 3, 1)
  pmatrix[,i] <- quantile(c(p.r.1, p.r.2, p.r.3), paf)
  }
  for (i in 1:99) {
 qmatrix[i,] <- quantile(pmatrix[i,], c(0.05, 0.5, 0.95))
  }

Because of the number of loop is very large, e.g., 1 here, the
code is very slow.


I would think that picking the seq(0.01, 0.99, 0.01) items in the first
case and the 500th, 5000th and the 9500th in the second case,  rather
than asking for what `quantile` would calculate,  would surely be more
"statistical", in the sense of choose order statistics anyway. Likely
much faster.



Yes, you are right. Following your suggestions, the execution time of 
`sort' is much shorter than `quantile' in the following code:


  pmatrix <- matrix(NA, nrow = 99, ncol = 1)
  qmatrix <- matrix(NA, nrow = 99, ncol = 3)
  paf <- seq(0.01, 0.99, 0.01)
  for (i in 1:1) {
  p.r.1 <- rnorm(1000, 1, 0.5)
  p.r.2 <- rnorm(1000, 2, 1.5)
  p.r.3 <- rnorm(1000, 3, 1)
  pmatrix[,i] <- sort(c(p.r.1, p.r.2, p.r.3))[paf*3000]
  }
  qmatrix <- pmatrix[,c(0.05, 0.5, 0.95)*1]

Thanks again.

Regards,
Jinsong

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


Re: [R] if then in R versus SAS

2012-08-27 Thread peter dalgaard

On Aug 24, 2012, at 21:51 , Daniel Nordlund wrote:

>> -Original Message-
>> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
>> On Behalf Of Marc Schwartz
>> Sent: Friday, August 24, 2012 12:06 PM
>> To: ramoss
>> Cc: r-help@r-project.org
>> Subject: Re: [R] if then in R versus SAS
>> 
>> 
>> On Aug 24, 2012, at 1:03 PM, ramoss  wrote:
>> 
>>> I am new to R and I have the following SAS statements:
>>> 
>>> if otype='M' and ocond='1' and entry='a.Prop' then MOC=1;
>>> else MOC=0;
>>> 
>>> How would I translate that into R code?
>>> 
>>> Thanks in advance
>> 
>> 
>> 
>> See ?ifelse and ?Logic, both of which are covered in "An Introduction to
>> R" (http://cran.r-project.org/manuals.html).
>> 
>>  MOC <- ifelse((otype == 'M') & (ocond == '1') & (entry == 'a.Prop'), 1,
>> 0)
>> 
>> 
>> You might also want to think about getting a copy of:
>> 
>> R for SAS and SPSS Users
>> Robert Muenchen
>> http://www.amazon.com/SAS-SPSS-Users-Statistics-Computing/dp/0387094172
>> 
>> Regards,
>> 
>> Marc Schwartz
>> 
> 
> I would second Mark's recommendation to carefully work through "An 
> Introduction to R" and to get Robert Muenchen's book.  If the variables 
> otype, ocond, and entry are scalar values, then the translation from SAS to R 
> is very straight-forward:
> 
> if(otype=='M' && ocond=='1' && entry=='a.Prop') MOC <- 1 else MOC <- 0

It's almost certain that they are not scalar though, so Marc's idea is likely 
right. 

Just let me add that ifelse() is not actually needed:

MOC <- as.numeric((otype == 'M') & (ocond == '1') & (entry == 'a.Prop'))

will do. (And the as.numeric bit is only to convert FALSE/TRUE to 0/1)



> 
> 
> Hope this is helpful,
> 
> Dan
> 
> Daniel Nordlund
> Bothell, WA 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

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


Re: [R] kernlab | ksvm error

2012-08-27 Thread Uwe Ligges



On 26.08.2012 15:33, Reza Salimi-Khorshidi wrote:

Thanks Uwe,
Am I right that in ksvm's internal cross-validation, there is no
guarantee for having *at least one* of each classes in each subset?



That is my guess, but I haven't read the code. Please read it yourself 
in case you want more details - or ask the author of tht function.


Best,
Uwe Ligges




Some randomness is involved, and when you get an unfortunate
subsample (e.g. if in the internal cross-validation one class is not
selected at all) it won't work anymore.



__
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] simplest way (set of functions) to parse a file

2012-08-27 Thread Giovanni Azua
Hello,

What would be the best set of R functions to parse and transform a file?

My file looks as shown below. I would like to plot this data and I need to 
parse it into a single data frame that sorts of "transposes the data" with the 
following structure:

> df <- data.frame(n=c(1,1,2,2),iter=c(1,2,1,2),step=as.factor(c('Step 1', 
> 'Step2', 'Step 1', 'Step 2')),value=c(10, 10, 10, 10))
> str(df)
'data.frame':   4 obs. of  4 variables:
 $ n: num  1 1 2 2
 $ iter : num  1 2 1 2
 $ step : Factor w/ 3 levels "Step 1","Step 2",..: 1 3 1 2
 $ value: num  10 10 10 10

n=extracted from the file name "logdet_two_moons_n10.txt"
iter=iter
step=column Step1, Step2, Step3, Step4
value=value of the specific Step column 

And this is one possible data frame variation to be able to plot the time 
proportions for the different steps of my algorithm. 

TIA,
Best regards,
Giovanni

Iteration   JminError   Elapsed Corral  Duality Gap Step1   Step2   
Step3   Step4
2   2   0.00e+001.912976e-031   0.00e+00
1.779780e-037.214600e-051.243600e-052.246700e-05
../test/genmoons_data/logdet_two_moons_n10.txt,2,2,1.754115e-02,0.00e+00,9.799000e+03,0.00e+00,5.586293e-01,0.00e+00
 
Iteration   JminError   Elapsed Corral  Duality Gap Step1   Step2   
Step3   Step4
4   9   0.00e+001.280841e-032   -7.105427e-15   
9.557570e-042.301610e-041.571100e-052.177300e-05
../test/genmoons_data/logdet_two_moons_n20.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00
 
Iteration   JminError   Elapsed Corral  Duality Gap Step1   Step2   
Step3   Step4
10  32  3.133476e-036.075853e-038   4.057531e-01
1.613035e-033.956920e-033.077200e-054.390900e-05
20  28  5.597685e-044.376530e-0316  4.711146e-03
0.00e+004.390998e-032.229600e-052.517100e-05
30  27  1.148159e-044.357923e-0322  8.408166e-06
0.00e+004.326610e-032.697700e-053.233200e-05
40  27  4.036778e-054.388260e-0329  2.529294e-07
0.00e+004.329726e-032.713000e-053.558400e-05
50  27  1.840383e-054.357373e-0336  1.34e-07
0.00e+004.327526e-033.097000e-053.255700e-05
53  27  0.00e+001.322382e-0336  -2.842171e-14   
0.00e+001.327239e-031.420400e-052.043500e-05
../test/genmoons_data/logdet_two_moons_n64.txt,53,69,3.330987e-02,0.00e+00,2.229830e+07,0.00e+00,6.694201e+02,0.00e+00
 
Iteration   JminError   Elapsed Corral  Duality Gap Step1   Step2   
Step3   Step4
10  70  7.739525e-032.389529e-028   1.494829e+00
2.975209e-031.873082e-024.713600e-055.837200e-05
20  74  3.379192e-032.084753e-0215  3.372041e-01
0.00e+002.084637e-024.302400e-053.907800e-05
30  76  1.322821e-032.093204e-0221  1.018845e-01
0.00e+002.083170e-024.704100e-055.707100e-05
40  78  1.176950e-032.095179e-0228  2.447970e-02
0.00e+002.088284e-024.890700e-054.955100e-05
50  78  2.233669e-042.050571e-0235  1.573952e-02
0.00e+002.045954e-024.046600e-053.899000e-05
60  78  2.167956e-042.095130e-0239  8.362982e-03
0.00e+002.082586e-026.699700e-058.506400e-05
70  78  2.085968e-042.085355e-0246  5.135190e-03
0.00e+002.083204e-025.432900e-054.078600e-05
80  78  2.570800e-042.044932e-0251  5.470225e-04
0.00e+002.033571e-025.334200e-055.318400e-05
81  78  0.00e+002.099610e-0351  1.421085e-14
0.00e+002.100072e-039.147000e-062.324800e-05
[[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] Error while installing gsubfn_0.6-4.tar.gz for R 2.15.1

2012-08-27 Thread Gabor Grothendieck
If the problem is the same then you still have an incomplete version
of R.  As you can see from the August 27th checks gsubfn works fine on
the latest version of R:

http://cran.r-project.org/web/checks/check_results_gsubfn.html

Try installing Rcmdr.   I suspect it won't install either on your version of R.

Regards.

On Sun, Aug 26, 2012 at 11:22 PM, Divya Kakkillaya B
 wrote:
> Hi,
>I have installed the latest version of R (2.15.1). I have downloaded the 
> tar file from the R CRAN website. The below mentioned problem exists with the 
> latest version. Please help.
>
> Regards,
> Divya
> 
> From: Gabor Grothendieck [ggrothendi...@gmail.com]
> Sent: Friday, August 24, 2012 6:31 PM
> To: Divya Kakkillaya B
> Cc: r-help@r-project.org
> Subject: Re: [R] Error while installing gsubfn_0.6-4.tar.gz for R 2.15.1
>
> Sorry, I just double checked and the options statement should be:
>
> options(gsubfn.engine = "R")
>
> Again, I am not sure if that is sufficient given that there is clearly
> something wrong with your version of R.
>
> On Fri, Aug 24, 2012 at 8:59 AM, Gabor Grothendieck
>  wrote:
>> I have never seen that before. It has been independently verified that
>> gsubfn works on Fedora Red Hat as you can see here:
>>
>> http://cran.r-project.org/web/checks/check_results_gsubfn.html
>>
>> The message about Tcl/Tk in your log suggests that there is something
>> wrong with your version of R itself.  See FAQ2:
>> http://code.google.com/p/gsubfn/#FAQs
>>
>> Given that its been independently verified to install properly on
>> fedora red hat and that your log shows that your version of R is
>> broken you may need to get a proper version of R.
>>
>> The following things may not be sufficient given the above but you
>> could try it just in case:
>>
>> options(gsubfn.engine = FALSE)
>> install.packages("gsubfn") # do NOT use dep=TRUE
>>
>> Let me know when you resolve it so we can add something to the FAQ.
>>
>> Regards.
>>
>> On Fri, Aug 24, 2012 at 2:45 AM, Divya Kakkillaya B
>>  wrote:
>>> Hi,
>>>
>>>   I am getting the follwoing error while installing gsubfn_0.6-4.tar.gz 
>>> library for R. R version is 2.15.1 and i am installing on Redhat linux 
>>> version 2.6.18-238.9.1.el5 
>>> (mockbu...@x86-002.build.bos.redhat.com)
>>>  (gcc version 4.1.2 20080704 (Red Hat 4.1.2-50))
>>>
>>>
>>>
>>> * installing to library â/home/mapred/installables/R/libraryâ
>>> * installing *source* package âgsubfnâ ...
>>> ** package âgsubfnâ successfully unpacked and MD5 sums checked
>>> ** R
>>> ** demo
>>> ** inst
>>> ** byte-compile and prepare package for lazy loading
>>> Warning: S3 methods â$.tclvarâ, â$<-.tclvarâ, âas.character.tclObjâ, 
>>> âas.character.tclVarâ, âas.double.tclObjâ, âas.integer.tclObjâ, 
>>> âas.logical.tclObjâ, âas.raw.tclObjâ, âprint.tclObjâ, â[[.tclArrayâ, 
>>> â[[<-.tclArrayâ, â$.tclArrayâ, â$<-.tclArrayâ, ânames.tclArrayâ, 
>>> ânames<-.tclArrayâ, âlength.tclArrayâ, âlength<-.tclArrayâ, 
>>> âtclObj.tclVarâ, âtclObj<-.tclVarâ, âtclvalue.defaultâ, âtclvalue.tclObjâ, 
>>> âtclvalue.tclVarâ, âtclvalue<-.defaultâ, âtclvalue<-.tclVarâ, 
>>> âclose.tkProgressBarâ were declared in NAMESPACE but not found
>>> Error : .onLoad failed in loadNamespace() for 'tcltk', details:
>>>   call: fun(libname, pkgname)
>>>   error: Tcl/Tk support is not available on this system
>>> Error : package/namespace load failed for âtcltkâ
>>> Error : unable to load R code in package âgsubfnâ
>>> ERROR: lazy loading failed for package âgsubfnâ
>>>
>>>
>>>
>>> Regards,
>>> Divya
>>>
>>>  CAUTION - Disclaimer *
>>> This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely
>>> for the use of the addressee(s). If you are not the intended recipient, 
>>> please
>>> notify the sender by e-mail and delete the original message. Further, you 
>>> are not
>>> to copy, disclose, or distribute this e-mail or its contents to any other 
>>> person and
>>> any such actions are unlawful. This e-mail may contain viruses. Infosys has 
>>> taken
>>> every reasonable precaution to minimize this risk, but is not liable for 
>>> any damage
>>> you may sustain as a result of any virus in this e-mail. You should carry 
>>> out your
>>> own virus checks before opening the e-mail or attachment. Infosys reserves 
>>> the
>>> right to monitor and review the content of all messages sent to or from 
>>> this e-mail
>>> address. Messages sent to or from this e-mail address may be stored on the
>>> Infosys e-mail system.
>>> ***INFOSYS End of Disclaimer INFOSYS***
>>>
>>> __
>>> 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.
>>
>>
>>
>> --
>> Statistics & Software Consulting
>> GKX G

[R] littler and rJava

2012-08-27 Thread John Laing
Hello list,

I'm having some difficulty getting rJava to load in littler. Even
after a R CMD javareconf and a reinstall of littler, I get this:
jlaing@xenon:~$ r -e "require(rJava)"
Loading required package: rJava
Loading required package: methods
Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object
'/usr/local/lib/R/site-library/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory

It works fine with Rscript:
jlaing@xenon:~$ Rscript -e "require(rJava)"
Loading required package: rJava
Loading required package: methods

Presumably my load paths are misconfigured, but I can't figure out where:
jlaing@xenon:~$ R CMD javareconf
Java interpreter : /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java
Java version : 1.6.0_24
Java home path   : /usr/lib/jvm/java-6-openjdk-amd64/jre
Java compiler: /usr/lib/jvm/java-6-openjdk-amd64/jre/../bin/javac
Java headers gen.: /usr/lib/jvm/java-6-openjdk-amd64/jre/../bin/javah
Java archive tool: /usr/lib/jvm/java-6-openjdk-amd64/jre/../bin/jar
Java library path:
$(JAVA_HOME)/lib/amd64/server:$(JAVA_HOME)/lib/amd64:$(JAVA_HOME)/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server
-L$(JAVA_HOME)/lib/amd64 -L$(JAVA_HOME)/../lib/amd64
-L/usr/java/packages/lib/amd64 -L/usr/lib/x86_64-linux-gnu/jni
-L/lib/x86_64-linux-gnu -L/usr/lib/x86_64-linux-gnu -L/usr/lib/jni
-L/lib -L/usr/lib -ljvm
JNI cpp flags: -I$(JAVA_HOME)/../include

System info:
jlaing@xenon:~$ R --version
R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

jlaing@xenon:~$ r --version
r ('littler') version 0.1.5
svn revision 185 as of 2011-09-17 09:35:56
built at 15:30:55 on Sep 17 2011
using GNU R Version 2.13.1 (2011-07-08)

Copyright (C) 2006 - 2011 Jeffrey Horner and Dirk Eddelbuettel


Thanks,
John

__
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] Standard deviation from MANOVA??

2012-08-27 Thread Jean V Adams
Not sure why that is.  You can always use manova() to get the multivariate 
summary statistics and then use individual models to get predictions for 
each response.  For example,

mydata <- data.frame(y1=rnorm(50), y2=rnorm(50), x1=rnorm(50), 
x2=rnorm(50), x3=rnorm(50))

myfit <- manova(cbind(y1, y2) ~ x1 + x2 + x3, data=mydata)
summary(myfit)

myfit1 <- lm(y1 ~ x1 + x2 + x3, data=mydata)
myfit2 <- lm(y1 ~ x1 + x2 + x3, data=mydata)
predict(myfit1, se.fit=TRUE)
predict(myfit2, se.fit=TRUE)

Jean


BrutishFruit  wrote on 08/25/2012 04:09:04 PM:
> 
> Hi,
> I have problem getting the standard deviation from the manova output.
> 
> I have used the manova function:  myfit <- manova(cbind(y1, y2) 
~ x1
> + x2 + x3, data=mydata) .
> I tried to get the predicted values and their standard deviation by 
> using: 
> predict(myfit, type="response", se.fit=TRUE)
> 
> But the problem is that I don't get the standard deviation values, I 
only
> get the predicted values for y1 and y2.
> 
> But if I type: predict*.lm*(myfit, type="response", se.fit=TRUE)
> I get the predicted values and standard deviation, but only for y1 (and
> nothing from y2...).
> 
> //BF

[[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] littler and rJava

2012-08-27 Thread Dirk Eddelbuettel

John,

I see this:

edd@max:~$ r -e'require(rJava)'
Loading required package: rJava
Loading required package: methods
Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object 
'/usr/lib/R/site-library/rJava/libs/rJava.so':
  libjvm.so: cannot open shared object file: No such file or directory

but also

edd@max:~$ locate libjvm.so
/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/cacao/libjvm.so
/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server/libjvm.so
edd@max:~$ 
LD_LIBRARY_PATH="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server" r 
-e'require(rJava)'
Loading required package: rJava
Loading required package: methods
edd@max:~$ 

Java is a bit of a moving target. We did add 

edd@max:~/svn/littler$ ./configure --help|grep java
  --with-java-libsLink littler to R's java libraries
edd@max:~/svn/littler$ 

a while back but that may not be sufficient.  Would you have time to poke
around at your end how we could make this better (presuming that you want
something more solid than the LD_LIBRARY_PATH adjustment you could also do on
your system-side).

Dirk

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


[R] How to average time series data around regular intervals

2012-08-27 Thread Jason Gilmore
Hi,

I'm pretty new to R and have run into a task which although I'm certain is
within R's capabilities, falls outside of mine. :-) Consider the following
data set:

2012-07-22 12:12:00, 21
2012-07-22 12:15:00, 22
2012-07-22 12:18:00, 24
2012-07-22 12:39:00, 21
2012-07-22 12:45:00, 25
2012-07-22 12:49:00, 26
2012-07-22 12:53:00, 20
2012-07-22 13:00:00, 18
2012-07-22 13:06:00, 22

My task involves creating a data set which *averages* these values at a
resolution of 15 minutes, meaning that I need to average the values falling
within 7.5 minutes of a 15 minute increment. Therefore given the above data
set I need to treat it as three groups:

2012-07-22 12:12:00, 21
2012-07-22 12:15:00, 22
2012-07-22 12:18:00, 24

2012-07-22 12:39:00, 21
2012-07-22 12:45:00, 25
2012-07-22 12:49:00, 26

2012-07-22 12:53:00, 20
2012-07-22 13:00:00, 18
2012-07-22 13:06:00, 22

The end result should look like this:

2012-07-22 12:15:00, 22.33
2012-07-22 12:30:00, NA <- Because this 15 minute slot did not previously
exist
2012-07-22 12:45:00, 24
2012-07-22 1:00:00, 20

Any help much appreciated. I've been working on this for several hours with
little success. I'm able to identify the missing (NA) value using zoo/xts
but can't seem to sort out the averaging matter.

Thanks so much!
Jason

[[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] kernlab`s custom kernel of ksvm freeze

2012-08-27 Thread okabes
Hello, together

I'm trying to use user defined kernel. I know that kernlab offer user
defined kernel(custom kernel functions) in R. 
I used data spam including package kernlab.

(number of variables=58 number of examples =4061)

i'm user defined kernel's form,

kp=function(d,e){

as=v*d
bs=v*e
cs=as-bs
cs=as.matrix(cs)

exp(-(norm(cs,"F")^2)/2)
}

class(kp)="kernel" 

It is the transformed kernel for gaussian kernel.

v is the continuously changed values that are inverse of standard deviation
vector about each variables. 
(ex: v=(0.167,0.167)`, length(v)= 57)

training set defined 60% of spam data. 
(preserving the proportions of the different classes.)

if data's type is spam, than data`s type = 1 for train svm (else -1)

m=ksvm(xtrain,ytrain,type="C-svc",kernel=kp,C=10) 

But, this step is not working. always Waiting for a response.

So, I ask you this problem, why? 

number of examples are too big? 

Is there any other R package that can train SVMs for user defined kernel?

I want to your answer.

Thanks in advance!




--
View this message in context: 
http://r.789695.n4.nabble.com/kernlab-s-custom-kernel-of-ksvm-freeze-tp4641393.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] String Handling() for Split a word by a letter

2012-08-27 Thread Rantony
Hi,

here im unable to run a string handle function called unpaste().

for eg:- a<- "12345_mydata"
Actually my requirement what is i need to get , only 12345.  Means that , i
need the all letter as a word which is before of  first  " _ " - symbol of 
"a". i tried to do with unpaste, it says function not found.  After that i
tried with "strsplit() ". it ran, but again i need to write another method
to get again after spliting. 

Any other good method is there for this requirement ?

- Thanks
Antony.



--
View this message in context: 
http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.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] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread Fridolin
What is a smart way to change an entry inside a column of a dataframe or
matrix which is of type "factor"?

Here is my script incl. input data:
> #set working directory:
> setwd("K:/R")
> 
> #read in data:
> input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)
> 
> #check data:
> input
   Ind  M1  M2  M3
11   96/98 120/120 0/0
22 102/108 120/124 305/305
33  96/108 120/120 0/0
44 0/0 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305
> str(input)
'data.frame':   10 obs. of  4 variables:
 $ Ind: int  1 2 3 4 5 6 7 8 9 10
 $ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
 $ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2
 $ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4
> 
> #replace 0/0 by 999/999:
> for (r in 1:10)
+   for (c in 2:4)
+ if (input[r,c]=="0/0") input[r,c]<-"999/999"
Warnmeldungen:
1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
  invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
  invalid factor level, NAs generated
3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
  invalid factor level, NAs generated
> input
   Ind  M1  M2  M3
11   96/98 120/120
22 102/108 120/124 305/305
33  96/108 120/120
44 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305


I want to replace all "0/0" by "999/999". My code should work for columns of
type "character" and "integer". But to make it work for a "factor"-column I
would need to add the new level of "999/999" at first, I guess. How do I add
a new level?



--
View this message in context: 
http://r.789695.n4.nabble.com/Changing-entries-of-column-of-type-factor-Adding-a-new-level-to-a-factor-tp4641402.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] Assigning colors on low p-values in table

2012-08-27 Thread Rmillan
Hi all R-users,

I’m trying to assign colors on those p-value in my table output that fall
above a certain critical value, let’s say a p-value >0.05.
My table looks like this:

  Assets ADF-Level P-Value  

ADF-First D  P-Value  ADF-Second DP-Value
[1,]Liabilities-2.3109  0.1988  
-3.162
0.025   -6.0281 0.01
[2,]Long.Bond   -0.2934 0.9172-6.1667 0.01  
-7.897 
0.01
[3,]Stocks -0.5539  0.8503-5.5787   
 
0.01-8.9303 0.01
[4,]CPI-2.1352  0.264 -2.705
 
0.0794  -6.0953 0.01
[5,]Nom.10Y-2.6968  0.0807-1.3854   
 
0.542   -5.846  0.01
[6,]T.Bills-2.0429  0.2982-2.2508   
 
0.2211  -4.761  0.01
[7,]Commodities -0.3973 0.9031-5.0478 0.01  
-6.7784
0.01
[8,]Real.Estate -0.6468 0.8159-4.8601 0.01  
-8.6037
0.01
[9,]Credit.Spread0.4181 0.9816-3.7542 0.01  
-5.9572
0.01
[10,]   Term.Spread -0.6243 0.8242-4.1885 0.01  
-6.4269
0.01
[11,]   Dividend.Yield   2.9316 0.99  -1.6759 0.4343
-5.1065 0.01

What could be nice was, if it is possible to highlight, for instance, all
the p-values in this table that are larger than 0.05 with the color red.
Any ideas on how this can be done?

All the values in this table are stored in vectors that I have combined as a
matrix and thereafter plotted as a matrix. Therefore this problem 
could also be solved if the values that are larger than 0.05 in the vector
“adf.pvalue” are saved as colored values.
After the values are stored in the vectors, adf.tvalue, adf.pvalue,
adf.tvalue1, adf.pvalue1, adf.tvalue2 and adf.pvalue2, the code looks like
this: 

>output<-matrix(c(col.names,adf.tvalue,adf.pvalue,adf.tvalue1,adf.pvalue1,adf.tvalue2,adf.pvalue2),ncol=7)
>colnames(output)<-c("Assets","ADF-Level","P-Value","ADF-First
D","P-Value","ADF-Second D","P-Value")
>output

Thanks in advance.

Sincerely,

Emil




--
View this message in context: 
http://r.789695.n4.nabble.com/Assigning-colors-on-low-p-values-in-table-tp4641395.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] simplest way (set of functions) to parse a file

2012-08-27 Thread arun
Hi,

I guess this helps you.
library(reshape)
dat1<-read.table(text="
Iteration    Jmin    Error    Elapsed    Corral    Duality_Gap    Step1    
Step2    Step3    Step4
10    32    3.133476e-03    6.075853e-03    8    4.057531e-01    
1.613035e-03    3.956920e-03    3.077200e-05    4.390900e-05
20    28    5.597685e-04    4.376530e-03    16    4.711146e-03    
0.00e+00    4.390998e-03    2.229600e-05    2.517100e-05
30    27    1.148159e-04    4.357923e-03    22    8.408166e-06    
0.00e+00    4.326610e-03    2.697700e-05    3.233200e-05
40    27    4.036778e-05    4.388260e-03    29    2.529294e-07    
0.00e+00    4.329726e-03    2.713000e-05    3.558400e-05
50    27    1.840383e-05    4.357373e-03    36    1.34e-07    
0.00e+00    4.327526e-03    3.097000e-05    3.255700e-05
53    27    0.00e+00    1.322382e-03    36    -2.842171e-14    
0.00e+00    1.327239e-03    1.420400e-05    2.043500e-05
",sep="",header=TRUE,stringsAsFactors=FALSE,fill=TRUE)
df2<-melt(dat1,"Iteration",c("Step1","Step2","Step3","Step4"))
df3<-df2[,c(2,1)]
df4<-data.frame(n=rep(c(1:6),times=4),iter=rep(1:4,each=6),df3)
 colnames(df4)[c(3,4)]<-c("step","value")
 head(df4)
  n iter  step value
1 1    1 Step1    10
2 2    1 Step1    20
3 3    1 Step1    30
4 4    1 Step1    40
5 5    1 Step1    50
6 6    1 Step1    53


A.K.



- Original Message -
From: Giovanni Azua 
To: r-help@r-project.org
Cc: 
Sent: Monday, August 27, 2012 6:24 AM
Subject: [R] simplest way (set of functions) to parse a file

Hello,

What would be the best set of R functions to parse and transform a file?

My file looks as shown below. I would like to plot this data and I need to 
parse it into a single data frame that sorts of "transposes the data" with the 
following structure:

> df <- data.frame(n=c(1,1,2,2),iter=c(1,2,1,2),step=as.factor(c('Step 1', 
> 'Step2', 'Step 1', 'Step 2')),value=c(10, 10, 10, 10))
> str(df)
'data.frame':    4 obs. of  4 variables:
$ n    : num  1 1 2 2
$ iter : num  1 2 1 2
$ step : Factor w/ 3 levels "Step 1","Step 2",..: 1 3 1 2
$ value: num  10 10 10 10

n=extracted from the file name "logdet_two_moons_n10.txt"
iter=iter
step=column Step1, Step2, Step3, Step4
value=value of the specific Step column 

And this is one possible data frame variation to be able to plot the time 
proportions for the different steps of my algorithm. 

TIA,
Best regards,
Giovanni

Iteration    Jmin    Error    Elapsed    Corral    Duality Gap    Step1    
Step2    Step3    Step4
2    2    0.00e+00    1.912976e-03    1    0.00e+00    1.779780e-03    
7.214600e-05    1.243600e-05    2.246700e-05
../test/genmoons_data/logdet_two_moons_n10.txt,2,2,1.754115e-02,0.00e+00,9.799000e+03,0.00e+00,5.586293e-01,0.00e+00
 
Iteration    Jmin    Error    Elapsed    Corral    Duality Gap    Step1    
Step2    Step3    Step4
4    9    0.00e+00    1.280841e-03    2    -7.105427e-15    9.557570e-04    
2.301610e-04    1.571100e-05    2.177300e-05
../test/genmoons_data/logdet_two_moons_n20.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00
 
Iteration    Jmin    Error    Elapsed    Corral    Duality Gap    Step1    
Step2    Step3    Step4
10    32    3.133476e-03    6.075853e-03    8    4.057531e-01    
1.613035e-03    3.956920e-03    3.077200e-05    4.390900e-05
20    28    5.597685e-04    4.376530e-03    16    4.711146e-03    
0.00e+00    4.390998e-03    2.229600e-05    2.517100e-05
30    27    1.148159e-04    4.357923e-03    22    8.408166e-06    
0.00e+00    4.326610e-03    2.697700e-05    3.233200e-05
40    27    4.036778e-05    4.388260e-03    29    2.529294e-07    
0.00e+00    4.329726e-03    2.713000e-05    3.558400e-05
50    27    1.840383e-05    4.357373e-03    36    1.34e-07    
0.00e+00    4.327526e-03    3.097000e-05    3.255700e-05
53    27    0.00e+00    1.322382e-03    36    -2.842171e-14    
0.00e+00    1.327239e-03    1.420400e-05    2.043500e-05
../test/genmoons_data/logdet_two_moons_n64.txt,53,69,3.330987e-02,0.00e+00,2.229830e+07,0.00e+00,6.694201e+02,0.00e+00
 
Iteration    Jmin    Error    Elapsed    Corral    Duality Gap    Step1    
Step2    Step3    Step4
10    70    7.739525e-03    2.389529e-02    8    1.494829e+00    
2.975209e-03    1.873082e-02    4.713600e-05    5.837200e-05
20    74    3.379192e-03    2.084753e-02    15    3.372041e-01    
0.00e+00    2.084637e-02    4.302400e-05    3.907800e-05
30    76    1.322821e-03    2.093204e-02    21    1.018845e-01    
0.00e+00    2.083170e-02    4.704100e-05    5.707100e-05
40    78    1.176950e-03    2.095179e-02    28    2.447970e-02    
0.00e+00    2.088284e-02    4.890700e-05    4.955100e-05
50    78    2.233669e-04    2.050571e-02    35    1.573952e-02    
0.00e+00    2.045954e-02    4.046600e-05    3.899000e-05
60    78    2.167956e-04    2.095130e-02    39    8.362982e-03    
0.00e+00    2.082586e-02    6.699700e-05    8.506400e-05
7

Re: [R] simplest way (set of functions) to parse a file

2012-08-27 Thread jim holtman
try this:


> input <- readLines(textConnection("Iteration   JminError
Elapsed Corral  Duality Gap Step1   Step2   Step3   Step4
+ 2   2   0.00e+001.912976e-031   0.00e+00
 1.779780e-037.214600e-051.243600e-052.246700e-05
+
../test/genmoons_data/logdet_two_moons_n10.txt,2,2,1.754115e-02,0.00e+00,9.799000e+03,0.00e+00,5.586293e-01,0.00e+00
+ Iteration   JminError   Elapsed Corral  Duality Gap Step1
Step2   Step3   Step4
+ 4   9   0.00e+001.280841e-032   -7.105427e-15
9.557570e-042.301610e-041.571100e-052.177300e-05
+
../test/genmoons_data/logdet_two_moons_n20.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00
+ Iteration   JminError   Elapsed Corral  Duality Gap Step1
Step2   Step3   Step4
+ 10  32  3.133476e-036.075853e-038   4.057531e-01
 1.613035e-033.956920e-033.077200e-054.390900e-05
+ 20  28  5.597685e-044.376530e-0316  4.711146e-03
 0.00e+004.390998e-032.229600e-052.517100e-05
+ 30  27  1.148159e-044.357923e-0322  8.408166e-06
 0.00e+004.326610e-032.697700e-053.233200e-05
+ 40  27  4.036778e-054.388260e-0329  2.529294e-07
 0.00e+004.329726e-032.713000e-053.558400e-05
+ 50  27  1.840383e-054.357373e-0336  1.34e-07
 0.00e+004.327526e-033.097000e-053.255700e-05
+ 53  27  0.00e+001.322382e-0336  -2.842171e-14
0.00e+001.327239e-031.420400e-052.043500e-05
+
../test/genmoons_data/logdet_two_moons_n64.txt,53,69,3.330987e-02,0.00e+00,2.229830e+07,0.00e+00,6.694201e+02,0.00e+00
+ Iteration   JminError   Elapsed Corral  Duality Gap Step1
Step2   Step3   Step4
+ 10  70  7.739525e-032.389529e-028   1.494829e+00
 2.975209e-031.873082e-024.713600e-055.837200e-05
+ 20  74  3.379192e-032.084753e-0215  3.372041e-01
 0.00e+002.084637e-024.302400e-053.907800e-05
+ 30  76  1.322821e-032.093204e-0221  1.018845e-01
 0.00e+002.083170e-024.704100e-055.707100e-05
+ 40  78  1.176950e-032.095179e-0228  2.447970e-02
 0.00e+002.088284e-024.890700e-054.955100e-05
+ 50  78  2.233669e-042.050571e-0235  1.573952e-02
 0.00e+002.045954e-024.046600e-053.899000e-05
+ 60  78  2.167956e-042.095130e-0239  8.362982e-03
 0.00e+002.082586e-026.699700e-058.506400e-05
+ 70  78  2.085968e-042.085355e-0246  5.135190e-03
 0.00e+002.083204e-025.432900e-054.078600e-05
+ 80  78  2.570800e-042.044932e-0251  5.470225e-04
 0.00e+002.033571e-025.334200e-055.318400e-05
+ 81  78  0.00e+002.099610e-0351  1.421085e-14
 0.00e+002.100072e-039.147000e-062.324800e-05
+
../test/genmoons_data/logdet_two_moons_n.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00"))
> closeAllConnections()
>
> # remove headers
> input <- input[!grepl("^Iteration", input)]
>
> # extract the file names
> files <- input[grepl("^\\.\\.", input)]
>
> # add separation based on file name
> x <- cumsum(grepl("^\\.\\.", input))
>
> # prepend separation onto the input
> input <- paste(x, input)
>
> # remove the file names
> input <- input[!grepl("\\.\\.", input)]
>
> # extract 'n' from the file names
> n <- as.integer(sub(".*_n([0-9]+).*", "\\1", files))
>
> # read the modified data back in
> tempFile <- tempfile()
> writeLines(input, tempFile)
> newInput <- read.table(tempFile)
>
> # replace first column with 'n'
> newInput[[1]] <- n[newInput[[1]] + 1L]
>
> names(newInput) <- c('n', 'iter', 1,2,3,4,5,'Step1', 'Step2', 'Step3',
'Step4')
>
> require(reshape2)
> x <- melt(newInput, id = c('n', 'iter'), measure =
c('Step1','Step2','Step3', 'Step4'))
> x
  n iter variable   value
1102Step1 0.001779780
2204Step1 0.000955757
364   10Step1 0.001613035
464   20Step1 0.0
564   30Step1 0.0
664   40Step1 0.0
764   50Step1 0.0
864   53Step1 0.0
9     10Step1 0.002975209
10    20Step1 0.0
11    30Step1 0.0
12    40Step1 0.0
13    50Step1 0.0
14    60Step1 0.0
15    70Step1 0.0
16    80Step1 0.0
17    81Step1 0.0
18   102Step2 0.72146
19   204Step2 0.000230161
20   64   10Step2 0.003956920
21   64   20Step2 0.004390998
22   64   30Step2 0.004326610
23   64   40Step2 0.004329726
24   64   50Step2 0.004327526
25   64   53Step2 0.001327239
26    10Step2 0.0

Re: [R] String Handling() for Split a word by a letter

2012-08-27 Thread jim holtman
Is this what you want:


> a<- "12345_mydata"
> sub("_.*", "", a)
[1] "12345"


On Mon, Aug 27, 2012 at 5:29 AM, Rantony  wrote:

> Hi,
>
> here im unable to run a string handle function called unpaste().
>
> for eg:- a<- "12345_mydata"
> Actually my requirement what is i need to get , only 12345.  Means that , i
> need the all letter as a word which is before of  first  " _ " - symbol of
> "a". i tried to do with unpaste, it says function not found.  After that i
> tried with "strsplit() ". it ran, but again i need to write another method
> to get again after spliting.
>
> Any other good method is there for this requirement ?
>
> - Thanks
> Antony.
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.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.
>



-- 
Jim Holtman
Data Munger Guru

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

[[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] for loop optimization help

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 1:53 AM, Jinsong Zhao wrote:


On 2012-08-27 9:35, David Winsemius wrote:


On Aug 26, 2012, at 5:06 PM, Jinsong Zhao wrote:


Hi there,

In my code, there is a for loop like the following:

 pmatrix <- matrix(NA, nrow = 99, ncol = 1)
 qmatrix <- matrix(NA, nrow = 99, ncol = 3)
 paf <- seq(0.01, 0.99, 0.01)
 for (i in 1:1) {
 p.r.1 <- rnorm(1000, 1, 0.5)
 p.r.2 <- rnorm(1000, 2, 1.5)
 p.r.3 <- rnorm(1000, 3, 1)
 pmatrix[,i] <- quantile(c(p.r.1, p.r.2, p.r.3), paf)
 }
 for (i in 1:99) {
qmatrix[i,] <- quantile(pmatrix[i,], c(0.05, 0.5, 0.95))
 }

Because of the number of loop is very large, e.g., 1 here, the
code is very slow.


I would think that picking the seq(0.01, 0.99, 0.01) items in the  
first

case and the 500th, 5000th and the 9500th in the second case,  rather
than asking for what `quantile` would calculate,  would surely be  
more

"statistical", in the sense of choose order statistics anyway. Likely
much faster.



Also possible that drawing the normals as 10,000 by 1,000 matrices  
(all at once, outside the loop) would save time (at the cost of added  
space requirements.




Yes, you are right. Following your suggestions, the execution time  
of `sort' is much shorter than `quantile' in the following code:


 pmatrix <- matrix(NA, nrow = 99, ncol = 1)
 qmatrix <- matrix(NA, nrow = 99, ncol = 3)
 paf <- seq(0.01, 0.99, 0.01)
 for (i in 1:1) {
 p.r.1 <- rnorm(1000, 1, 0.5)
 p.r.2 <- rnorm(1000, 2, 1.5)
 p.r.3 <- rnorm(1000, 3, 1)
 pmatrix[,i] <- sort(c(p.r.1, p.r.2, p.r.3))[paf*3000]
 }
 qmatrix <- pmatrix[,c(0.05, 0.5, 0.95)*1]

Thanks again.

Regards,
Jinsong


David Winsemius, MD
Alameda, CA, USA

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


Re: [R] for loop optimization help

2012-08-27 Thread Jinsong Zhao

On 2012-08-27 22:25, David Winsemius wrote:


On Aug 27, 2012, at 1:53 AM, Jinsong Zhao wrote:


On 2012-08-27 9:35, David Winsemius wrote:


On Aug 26, 2012, at 5:06 PM, Jinsong Zhao wrote:


Hi there,

In my code, there is a for loop like the following:

 pmatrix <- matrix(NA, nrow = 99, ncol = 1)
 qmatrix <- matrix(NA, nrow = 99, ncol = 3)
 paf <- seq(0.01, 0.99, 0.01)
 for (i in 1:1) {
 p.r.1 <- rnorm(1000, 1, 0.5)
 p.r.2 <- rnorm(1000, 2, 1.5)
 p.r.3 <- rnorm(1000, 3, 1)
 pmatrix[,i] <- quantile(c(p.r.1, p.r.2, p.r.3), paf)
 }
 for (i in 1:99) {
qmatrix[i,] <- quantile(pmatrix[i,], c(0.05, 0.5, 0.95))
 }

Because of the number of loop is very large, e.g., 1 here, the
code is very slow.


I would think that picking the seq(0.01, 0.99, 0.01) items in the first
case and the 500th, 5000th and the 9500th in the second case,  rather
than asking for what `quantile` would calculate,  would surely be more
"statistical", in the sense of choose order statistics anyway. Likely
much faster.



Also possible that drawing the normals as 10,000 by 1,000 matrices (all
at once, outside the loop) would save time (at the cost of added space
requirements.


Thanks for the suggestion.

Because in real situation the parameters of `rnorm' function is 
different in every loop, so it can't using 10,000 by 1,000 matrix 
outside the loop.






Yes, you are right. Following your suggestions, the execution time of
`sort' is much shorter than `quantile' in the following code:

 pmatrix <- matrix(NA, nrow = 99, ncol = 1)
 qmatrix <- matrix(NA, nrow = 99, ncol = 3)
 paf <- seq(0.01, 0.99, 0.01)
 for (i in 1:1) {
 p.r.1 <- rnorm(1000, 1, 0.5)
 p.r.2 <- rnorm(1000, 2, 1.5)
 p.r.3 <- rnorm(1000, 3, 1)
 pmatrix[,i] <- sort(c(p.r.1, p.r.2, p.r.3))[paf*3000]
 }
 qmatrix <- pmatrix[,c(0.05, 0.5, 0.95)*1]


A mistake here, the above line should be changed to:

for (i in 1:99) qmatrix[i,] <- sort(pmatrix[i,])[c(0.05,0.5,0.95)*1]



Thanks again.

Regards,
Jinsong


David Winsemius, MD
Alameda, CA, USA




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


Re: [R] littler and rJava

2012-08-27 Thread John Laing
Hi Dirk,

Thanks for the quick reply. Setting LD_LIBRARY_PATH does the trick, so
I can get by with that in my environment.

I would be happy to look into potential improvements. The ./configure
may work, but I installed littler through the debian package and thus
bypassed manual configuration. I'll look into that too; at the moment
autoconf is complaining.

Thanks again,
John


On Mon, Aug 27, 2012 at 9:50 AM, Dirk Eddelbuettel  wrote:
>
> John,
>
> I see this:
>
> edd@max:~$ r -e'require(rJava)'
> Loading required package: rJava
> Loading required package: methods
> Error : .onLoad failed in loadNamespace() for 'rJava', details:
>   call: dyn.load(file, DLLpath = DLLpath, ...)
>   error: unable to load shared object 
> '/usr/lib/R/site-library/rJava/libs/rJava.so':
>   libjvm.so: cannot open shared object file: No such file or directory
>
> but also
>
> edd@max:~$ locate libjvm.so
> /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/cacao/libjvm.so
> /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server/libjvm.so
> edd@max:~$ 
> LD_LIBRARY_PATH="/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server" r 
> -e'require(rJava)'
> Loading required package: rJava
> Loading required package: methods
> edd@max:~$
>
> Java is a bit of a moving target. We did add
>
> edd@max:~/svn/littler$ ./configure --help|grep java
>   --with-java-libsLink littler to R's java libraries
> edd@max:~/svn/littler$
>
> a while back but that may not be sufficient.  Would you have time to poke
> around at your end how we could make this better (presuming that you want
> something more solid than the LD_LIBRARY_PATH adjustment you could also do on
> your system-side).
>
> Dirk
>
> --
> 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] total CPU time in Matlab - what is the equivalent in R?

2012-08-27 Thread William Dunlap
I didn't notice a discussion of what system or user time meant in
those two articles.  (The first correctly says that system.time() is
a more convenient way to time things, as it calls proc.time() before
and after your expression and subtracts.  It also does garbage
collection before the timing, so you get more consistent results.)

CPU time is essentially user time plus system time.   User time is time spent
in R itself.  System time is time spent by the kernel of the operating
system doing things that R asked it to do (e.g., finding a file, reading
a file, perhaps getting the time).  What is considered system time
depends on your operating system.  There is some time spent switching
between user and kernel mode and I don't know who gets charged for
that time.  Elapsed time is usually what counts in the end.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Jeff Newmiller
> Sent: Sunday, August 26, 2012 11:13 PM
> To: Gopi Goteti; r-help@r-project.org
> Subject: Re: [R] total CPU time in Matlab - what is the equivalent in R?
> 
> Google is your friend
> 
> http://www.ats.ucla.edu/stat/r/faq/timing_code.htm
> 
> http://r.research.att.com/benchmarks/
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
> 
> Gopi Goteti  wrote:
> 
> >I am trying to compare the speed of my R code with that reported by the
> >authors of a reference paper who used Matlab. The authors of the paper
> >state "Total CPU time for the algorithm was 10 s for the ...on a modest
> >mobile 2.2 GHz Pentium processor running MATLAB v.6".
> >
> >I am using proc.time (similar to the example shown in ?proc.time) to
> >determine the speed of my R code. Could someone please tell me whether
> >"user" time or "system" time is the equivalent of total CPU time
> >reported
> >by Matlab.
> >
> >Thanks
> >GG
> >
> > [[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] Standard deviation from MANOVA??

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 6:37 AM, Jean V Adams wrote:

Not sure why that is.  You can always use manova() to get the  
multivariate
summary statistics and then use individual models to get predictions  
for

each response.  For example,

mydata <- data.frame(y1=rnorm(50), y2=rnorm(50), x1=rnorm(50),
x2=rnorm(50), x3=rnorm(50))

myfit <- manova(cbind(y1, y2) ~ x1 + x2 + x3, data=mydata)
summary(myfit)

myfit1 <- lm(y1 ~ x1 + x2 + x3, data=mydata)
myfit2 <- lm(y1 ~ x1 + x2 + x3, data=mydata)


I'm guessing this was supposed to be:
  myfit2 <- lm(y2 ~ x1 + x2 + x3, data=mydata)


predict(myfit1, se.fit=TRUE)
predict(myfit2, se.fit=TRUE)


I suppose it is possible that what was desired were the standard  
errors around the fitted mean estimates (which I think is what this  
would deliver), but what was asked were for "standard deviations". I  
admit to being puzzled that `manova` was being thought of as a method,  
since the sample or population standard deviations should be derived  
from three applications of `tapply`. I think BruitishFruit should tell  
us what he means by "... don't get the standard deviation values" in  
sufficient detail that we get an unambiguous description what  
"standard deviations" are being requested. (Or he can pose a small  
test case with the "correct answer".)




Jean


BrutishFruit  wrote on 08/25/2012 04:09:04  
PM:


Hi,
I have problem getting the standard deviation from the manova output.

I have used the manova function:  myfit <- manova(cbind(y1,  
y2)

~ x1

+ x2 + x3, data=mydata) .
I tried to get the predicted values and their standard deviation by
using:
predict(myfit, type="response", se.fit=TRUE)

But the problem is that I don't get the standard deviation values, I

only

get the predicted values for y1 and y2.

But if I type: predict*.lm*(myfit, type="response", se.fit=TRUE)
I get the predicted values and standard deviation, but only for y1  
(and

nothing from y2...).

//BF


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


David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] Reference classes and memory consumption

2012-08-27 Thread Jan Mueller
I found something that looks a bit odd to me:

The memory consumption of reference classes seems to increase dramatically 
(roughly 10x in the example below) with serialization/deserialization 
(saveRDS() / readRDS()).
Maybe creating instances of the same class has a synergy effect (references?) 
which is lost after a saveRDS()/readRDS() cycle? Class definitions spring to 
mind but I was unable to track 
down any size difference along these lines.
Any ideas?

Best
Jan

# start new R session, empty workspace
untouched=memory.size();
MySmallClass = setRefClass("MySmallClass",
   fields = list(
 myField = "numeric"
   ),
   methods = list(
initialize = function(f) {
  myField<<- f;
   })
 );

# Generate 10K instances
o = lapply(rnorm(1), MySmallClass$new)
withobj=memory.size()
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", 
round(withobj-untouched, 2))); 
#[1] "Initial Mem: 14.06 With objects: 29.14 Difference: 15.08"

 
# Save and read back those instances
saveRDS(o, "test.rds")
untouched=memory.size();
p=readRDS("test.rds")
withobj=memory.size()
print(paste("Initial Mem:", untouched, "With objects:", withobj, "Difference:", 
round(withobj-untouched, 2))); 
#[1] "Initial Mem: 31.2 With objects: 200.71 Difference: 169.51"


# Platform info:
# R version 2.15.1
# Platform: i386-pc-mingw32/i386 (32-bit)
# Binary package from CRAN
# Windows 7 32 Bit 
#  (I observed a similar behavior on Linux 64 Bit, same R version)

__
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] total CPU time in Matlab - what is the equivalent in R?

2012-08-27 Thread Jan Mueller
Maybe the unix time command comes in handy (provided you work with linux)?
http://en.wikipedia.org/wiki/Time_(Unix)
Best
Jan


-Ursprüngliche Nachricht-
Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im 
Auftrag von William Dunlap
Gesendet: Montag, 27. August 2012 16:38
An: Jeff Newmiller; Gopi Goteti; r-help@r-project.org
Betreff: Re: [R] total CPU time in Matlab - what is the equivalent in R?

I didn't notice a discussion of what system or user time meant in those two 
articles.  (The first correctly says that system.time() is a more convenient 
way to time things, as it calls proc.time() before and after your expression 
and subtracts.  It also does garbage collection before the timing, so you get 
more consistent results.)

CPU time is essentially user time plus system time.   User time is time spent
in R itself.  System time is time spent by the kernel of the operating system 
doing things that R asked it to do (e.g., finding a file, reading a file, 
perhaps getting the time).  What is considered system time depends on your 
operating system.  There is some time spent switching between user and kernel 
mode and I don't know who gets charged for that time.  Elapsed time is usually 
what counts in the end.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org 
> [mailto:r-help-boun...@r-project.org] On Behalf Of Jeff Newmiller
> Sent: Sunday, August 26, 2012 11:13 PM
> To: Gopi Goteti; r-help@r-project.org
> Subject: Re: [R] total CPU time in Matlab - what is the equivalent in R?
> 
> Google is your friend
> 
> http://www.ats.ucla.edu/stat/r/faq/timing_code.htm
> 
> http://r.research.att.com/benchmarks/
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live Go...
>   Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> --
> - Sent from my phone. Please excuse my brevity.
> 
> Gopi Goteti  wrote:
> 
> >I am trying to compare the speed of my R code with that reported by 
> >the authors of a reference paper who used Matlab. The authors of the 
> >paper state "Total CPU time for the algorithm was 10 s for the ...on 
> >a modest mobile 2.2 GHz Pentium processor running MATLAB v.6".
> >
> >I am using proc.time (similar to the example shown in ?proc.time) to 
> >determine the speed of my R code. Could someone please tell me 
> >whether "user" time or "system" time is the equivalent of total CPU 
> >time reported by Matlab.
> >
> >Thanks
> >GG
> >
> > [[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.

__
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] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread PIKAL Petr
Hi

you could save yourself time to read help page for factor

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Fridolin
> Sent: Monday, August 27, 2012 12:09 PM
> To: r-help@r-project.org
> Subject: [R] Changing entries of column of type "factor"/Adding a new
> level to a factor
> 
> What is a smart way to change an entry inside a column of a dataframe
> or matrix which is of type "factor"?

data.frame is not a matrix

each factor have levels attribute
levels(input$M1)
level 0/0 shall be the first so
levels(input$M1)[1] <-"999/999"

Easiest way is probably cycle through columns of your data

for( i in columns) levels(input[,i])[levels(input[,i])=="0/0"]<-"999/999"

Regards
Petr


> 
> Here is my script incl. input data:
> > #set working directory:
> > setwd("K:/R")
> >
> > #read in data:
> > input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)
> >
> > #check data:
> > input
>Ind  M1  M2  M3
> 11   96/98 120/120 0/0
> 22 102/108 120/124 305/305
> 33  96/108 120/120 0/0
> 44 0/0 116/120 300/305
> 55  96/108 120/130 300/305
> 66   98/98 116/120 300/305
> 77  98/108 120/120 305/305
> 88  98/108 120/120 305/305
> 99  98/102 120/124 300/300
> 10  10 108/108 120/120 305/305
> > str(input)
> 'data.frame': 10 obs. of  4 variables:
>  $ Ind: int  1 2 3 4 5 6 7 8 9 10
>  $ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3  $
> M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2  $
> M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4
> >
> > #replace 0/0 by 999/999:
> > for (r in 1:10)
> +   for (c in 2:4)
> + if (input[r,c]=="0/0") input[r,c]<-"999/999"
> Warnmeldungen:
> 1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> 2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> 3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> > input
>Ind  M1  M2  M3
> 11   96/98 120/120
> 22 102/108 120/124 305/305
> 33  96/108 120/120
> 44 116/120 300/305
> 55  96/108 120/130 300/305
> 66   98/98 116/120 300/305
> 77  98/108 120/120 305/305
> 88  98/108 120/120 305/305
> 99  98/102 120/124 300/300
> 10  10 108/108 120/120 305/305
> 
> 
> I want to replace all "0/0" by "999/999". My code should work for
> columns of type "character" and "integer". But to make it work for a
> "factor"-column I would need to add the new level of "999/999" at
> first, I guess. How do I add a new level?
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Changing-
> entries-of-column-of-type-factor-Adding-a-new-level-to-a-factor-
> tp4641402.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] Dozen+ Online R Courses: $110-$150 total cost thru 9/3

2012-08-27 Thread Geoffrey Hubona
VCU faculty and non-profit Georgia R School offer 11-14 substantial (4-10
classes each) online courses using R on: Fundamentals; Graphics; Data
Manipulation; Data Mining; Multivariate Statistics; PLS Path Modeling;
Programming; Simulation; and more. Take until May 15, 2013 to complete as
many courses as needed, Available 24/7. Total cost (for ALL courses, not
EACH course) through Sept 3 Only Students: $110; Everyone Else: $150.



Costs double on Sept 4, and forever.



Informational website is here:

http://www.regonline.com/r-courses



Email questions to ghub...@vcu.edu



Geoff Hubona

[[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] saving and reading csv and rda files

2012-08-27 Thread Alok Bohara, PhD

Hi:

I am trying to understand the link between ".csv" and  ".rda" files.
Is there any easy to follow tutorial on this?


(I could do some of the operations below, but I got lost in the details.)

1.  Reading .rda file ?

data <- load("profit.rda") # supposed to have four variable --y 
x1 x2 state_name


--how do I find out about the variable names,
--take the log of y and x1
--extract y and calculate mean etc..

(I want to use it in lm regression lny = f(lnx1,x2))



2. How could I save this profit.rda file as a csv file  with the 
variable names attached?

I tried doing this:

profit_data <- load("profit.rda")

#Could I do this?
write.csv(profit_data, file="profit.csv", col.names=TRUE)
data2 <- read.csv("profit.csv", head = TRUE)

# saving .rda file without the header?
write.csv(profit_data, file="profit2.csv", col.names=FALSE)

***

3. Creating a .rda file  from a csv file  using the save command

data2 <- read.csv("poverty.csv", head = T, sep = ",")   # poverty.csv 
file has 4 variables -- Q L K  country_name

save(data2, file="poverty.rda")

--How do I  attach names from the csv file to this .rda file?



Best,
Alok Bohara
UNM

__
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] looping through numbered variables

2012-08-27 Thread Daniel Caro
Hello,

This is a beginner question. I am trying to loop through numbered
variables with "apply" to calculate weighted means. My data is "data",
the variables are "var1" to "var100", the weight is "weight". The
command works using

sapply(paste('data$var', 1:100, sep=''), function(x)
weighted.mean(eval(parse(text=x)), data$weight))

but is there a way to avoid eval(parse())?

Thank you,
Daniel

__
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] [R-pkgs] PVAClone: new package for population viability analysis

2012-08-27 Thread Peter Solymos
Dear UseRs!

We are pleased to announce the release of our new package 'PVAClone'.

The 'PVAClone' package implements Population Viability Analysis (PVA)
methodology using data cloning. The data cloning algorithm by Lele et
al. (2007, 2010) is employed to compute maximum likelihood estimates
of the state-space model parameters and the corresponding standard
errors, heavily capitalizing on JAGS, dclone and dcme packages (see
Solymos 2010).

The main components of the package include estimation of univariate
population growth models, model selection and extinction risk
estimation:

* Model Estimation *
- computes maximum likelihood Estimation of the univariate population
growth models, both in the presence or absence of observation error;
- population time series with missing observations are also accommodated;
- population growth models: Gompertz, Ricker, theta-logistic and the
generalized Beverton-Holt;
- models can also be fitted by fixing a subset of model parameters to
a priori values of interest;
- observation error is incorporated via the general state-space
modeling framework.

* Model Selection *
- we implement Ponciano et. al.'s (2009) data cloned likelihood ratio
algorithm (DCLR) to compute likelihood ratios for comparing
hierarchical (random effect) models;
- this feature allows comparison of any two nested or non-nested
state-space models fitted using the Model Estimation procedure above
- for instance one can compare the state-space Generalized
Beverton-Holt model with a logistic model, even when observations are
missing;
- the underlying function is pva.llr can also be called repeatedly to
compute profile likelihood of a parameter of interest.

* Extinction Risk Estimation (under development) *
- data cloning based frequentist prediction of latent variables in a
general hierarchical model (Lele et al. 2010) is used to forecast
future abundance time series;
- a large number of future population trajectories are generated under
the observed data and estimated model parameters;
- these are then used to estimate various extinction metrics (see
Nadeem and Lele 2012).

Feedback, bug reports and feature requests are welcome!

Khurram and Peter

--
Khurram Nadeem
knad...@math.ualberta.ca
University of Alberta

Péter Sólymos
soly...@ualberta.ca
University of Alberta


References

Nadeem, K. & Lele, S.R. 2012. Likelihood based population viability
analysis in the presence of observation error. Oikos, early online

Ponciano, J. M. et al. 2009. Hierarchical models in ecology:
confidence intervals, hypothesis testing, and model selection using
data cloning. Ecology 90: 356--362.

Lele, S. R. et al. 2007. Data cloning: easy maximum likelihood
estimation for complex ecological models using Bayesian Markov chain
Monte Carlo methods. Ecol. Lett. 10: 551--563.

Lele, S. R. et al. 2010. Estimability and Likelihood Inference for
Generalized Linear Mixed Models Using Data Cloning. J. Am. Stat.
Assoc. 105: 1617--1625.

Solymos, P. (2010): dclone: Data Cloning in R. The R Journal, 2(2): 29--37.

___
R-packages mailing list
r-packa...@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-packages
__
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] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread William Dunlap
One way is to read in your text data as character columns with
read.table(stringsAsFactors=FALSE,...), then fiddle with the values,
and finally make factors out of the columns that  you want to be factors,
specifying the levels explicitly.  (Do you really want those number/number
things to be treated as factors?  The orders of their levels will be weird 
unless
you list them all when making the factor.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Fridolin
> Sent: Monday, August 27, 2012 3:09 AM
> To: r-help@r-project.org
> Subject: [R] Changing entries of column of type "factor"/Adding a new level 
> to a factor
> 
> What is a smart way to change an entry inside a column of a dataframe or
> matrix which is of type "factor"?
> 
> Here is my script incl. input data:
> > #set working directory:
> > setwd("K:/R")
> >
> > #read in data:
> > input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)
> >
> > #check data:
> > input
>Ind  M1  M2  M3
> 11   96/98 120/120 0/0
> 22 102/108 120/124 305/305
> 33  96/108 120/120 0/0
> 44 0/0 116/120 300/305
> 55  96/108 120/130 300/305
> 66   98/98 116/120 300/305
> 77  98/108 120/120 305/305
> 88  98/108 120/120 305/305
> 99  98/102 120/124 300/300
> 10  10 108/108 120/120 305/305
> > str(input)
> 'data.frame': 10 obs. of  4 variables:
>  $ Ind: int  1 2 3 4 5 6 7 8 9 10
>  $ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
>  $ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2
>  $ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4
> >
> > #replace 0/0 by 999/999:
> > for (r in 1:10)
> +   for (c in 2:4)
> + if (input[r,c]=="0/0") input[r,c]<-"999/999"
> Warnmeldungen:
> 1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> 2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> 3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>   invalid factor level, NAs generated
> > input
>Ind  M1  M2  M3
> 11   96/98 120/120
> 22 102/108 120/124 305/305
> 33  96/108 120/120
> 44 116/120 300/305
> 55  96/108 120/130 300/305
> 66   98/98 116/120 300/305
> 77  98/108 120/120 305/305
> 88  98/108 120/120 305/305
> 99  98/102 120/124 300/300
> 10  10 108/108 120/120 305/305
> 
> 
> I want to replace all "0/0" by "999/999". My code should work for columns of
> type "character" and "integer". But to make it work for a "factor"-column I
> would need to add the new level of "999/999" at first, I guess. How do I add
> a new level?
> 
> 
> 
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Changing-entries-of-
> column-of-type-factor-Adding-a-new-level-to-a-factor-tp4641402.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] bpplot question

2012-08-27 Thread Andras Farkas
Dear All,
 
wondering if I could get some help on the following using a box-percentile 
plotting in Hmisc:
 
1. how could I put limits on the y axis? the following does NOT seem to work:
 
bpplot(b,c,d,e,f,g,h, ylim=c(0,2500))
 
2. I would like to color in each box plot from b to h the line representing the 
median with the color of red, the interquantile range with the color of yellow, 
and the areas outside of the interquantile ranges with the color of green.
 
I am open to alternative solutions, also
 
All of your help is greatly apreciated, as allways,
 
Sincerely,
 
Andras 
[[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] String Handling() for Split a word by a letter

2012-08-27 Thread Rui Barradas

Hello,

If the op says he has tried strsplit, maybe a simple subsetting 
afterwards would solve it.


a <- "12345_mydata"
strsplit(a, "_")[[1]][1]

Hope this helps,

Rui Barradas

Em 27-08-2012 15:22, jim holtman escreveu:

Is this what you want:



a<- "12345_mydata"
sub("_.*", "", a)

[1] "12345"


On Mon, Aug 27, 2012 at 5:29 AM, Rantony  wrote:


Hi,

here im unable to run a string handle function called unpaste().

for eg:- a<- "12345_mydata"
Actually my requirement what is i need to get , only 12345.  Means that , i
need the all letter as a word which is before of  first  " _ " - symbol of
"a". i tried to do with unpaste, it says function not found.  After that i
tried with "strsplit() ". it ran, but again i need to write another method
to get again after spliting.

Any other good method is there for this requirement ?

- Thanks
Antony.



--
View this message in context:
http://r.789695.n4.nabble.com/String-Handling-for-Split-a-word-by-a-letter-tp4641397.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] interpret the importance output?

2012-08-27 Thread Johnathan Mercer
> importance(rfor.pdp11_t25.comb1,type=1)
  %IncMSE
v1 -0.28956401263
v2  1.92865561147
v3 -0.63443929130
v4  1.58949137047
v5  0.03190940065

I wasn't entirely confident with interpreting these results based on the
documentation.
Could you please interpret?

[[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] randomLCA

2012-08-27 Thread Gabriele Accetta
Can anybody, please, explain me how many parameter are estimated using
randomLCA?

For examples,  model  "dentistry.lca2random"   estimate 1 scale (or
variance, b_j)  parameter  and 2 position parameters (a_cj)? Doesn't
it?
Do I need at least 4 diagnostic tests for such a model?

What happens if I specify options blocksize and byclass? How many
diagnostic tests (or rater) I need?


Extract from see "randomLCA examples", by Ken Beath.

> dentistry.lca2random <- randomLCA(dentistry[,
+ 1:5], freq = dentistry$freq, initmodel = dentistry.lca2,
+ nclass = 2, random = TRUE, quadpoints = 31,
+ probit = TRUE)


Thank you.

__
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 matrix of Beta or Binomial distribution

2012-08-27 Thread JeffND
Hi folks,

I have a question about how to efficiently produce random numbers from Beta
and Binomial distributions. 

For Beta distribution, suppose we have two shape vectors shape1 and shape2.
I hope to generate a 1 x 2 matrix X whose i th rwo is a sample from
reta(2,shape1[i]mshape2[i]). Of course this can be done via loops:

for(i in 1:1)
{
X[i,]=rbeta(2,shape1[i],shape2[i])
}

However, the above code is time consuming. It would be better to directly
generate X without any loops. I tried the following code

X<- rbeta(2,shape1,shape2)

but it looks not right. So I was wondering if there is an R code doing this.

For Binomial distribution, I have a similar question.  I hope to generate a
1 x n matrix Y whose i th row is a sample from rbinom(n,2,p[i]), where p
is a vector of binomial probabilities. We can also do it by loops

for(i in 1:1)
{
Y[i,]=rbinom(n,2,p[i])
}

But it would be nice to do it without using any loops. Is this possible in
R?

Many thanks for great help and suggestions!
Jeff









--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422.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] looping through numbered variables

2012-08-27 Thread Bert Gunter
Have you read "An Introduction to R". If not, please do so before
osting and pay particular attention to the section on indexing. If so,
re-read the sections on indexing.

For a terser exposition, ?"["

-- Bert

On Mon, Aug 27, 2012 at 7:11 AM, Daniel Caro  wrote:
> Hello,
>
> This is a beginner question. I am trying to loop through numbered
> variables with "apply" to calculate weighted means. My data is "data",
> the variables are "var1" to "var100", the weight is "weight". The
> command works using
>
> sapply(paste('data$var', 1:100, sep=''), function(x)
> weighted.mean(eval(parse(text=x)), data$weight))
>
> but is there a way to avoid eval(parse())?
>
> Thank you,
> Daniel
>
> __
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 3:09 AM, Fridolin wrote:

What is a smart way to change an entry inside a column of a  
dataframe or

matrix which is of type "factor"?

Here is my script incl. input data:

#set working directory:
setwd("K:/R")

#read in data:
input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)

#check data:
input

  Ind  M1  M2  M3
11   96/98 120/120 0/0
22 102/108 120/124 305/305
33  96/108 120/120 0/0
44 0/0 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305

str(input)

'data.frame':   10 obs. of  4 variables:
$ Ind: int  1 2 3 4 5 6 7 8 9 10
$ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
$ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2
$ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4


#replace 0/0 by 999/999:
for (r in 1:10)

+   for (c in 2:4)
+ if (input[r,c]=="0/0") input[r,c]<-"999/999"
Warnmeldungen:
1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated
3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated

input

  Ind  M1  M2  M3
11   96/98 120/120
22 102/108 120/124 305/305
33  96/108 120/120
44 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305


I want to replace all "0/0" by "999/999". My code should work for  
columns of
type "character" and "integer". But to make it work for a "factor"- 
column I
would need to add the new level of "999/999" at first, I guess. How  
do I add

a new level?


?levels

levels(input$M1) <- c(levels(input$M1), "999/999")

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

__
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] practical way to change column names?

2012-08-27 Thread Niklas Fischer
Dear R helpers,
I have a social network data including repated measures of ten alters (whom
you contact) and their attributes(gender, age, strength of tie).

I wrote variables related with alters just for wrote alter 1 and alter 2.

I'd like to change the like below. I'd change each name separetely.

Do you know any pratical way to change it?
All the bests,
Niklas

variables for alter 1
g61a (id)
g62a (gender)
g63a (age)
g63aa (tie)
g63aan (tie friequency)

variables for alter 2
g61b
g62b
g63b
g64b
g64bb
g64bbn

new names

alterid_1
alterag_1
alteraa_1
alatert_1
altersf_1

alterid_2
alterag_2
alteraa_2
alatert_2
altersf_2

[[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] looping through numbered variables

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 7:11 AM, Daniel Caro wrote:


Hello,

This is a beginner question.


You should read the Posting Guide. (As well as following Bert's  
suggestion to work through "Intro to R".



I am trying to loop through numbered
variables with "apply" to calculate weighted means. My data is "data",
the variables are "var1" to "var100", the weight is "weight". The
command works using

sapply(paste('data$var', 1:100, sep=''), function(x)
weighted.mean(eval(parse(text=x)), data$weight))

but is there a way to avoid eval(parse())?


I'm guessing you want:

library(  )
lapply(data, function(x) weighted_mean(x, data$weight))

(I'm was guessing that weighted_mean was from the Hmisc package (but  
its similar function has a different name), but at any rate, it is  
better practice to indicate what package holds non-core functions. I  
also think it is better practice to use named arguments in function  
calls and to not use teh name "data" for objects, since it is also a  
function name.)


?data


--
David Winsemius, MD
Alameda, CA, USA

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


Re: [R] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread Bert Gunter
Well ...See below.

-- Cheers, Bert

On Mon, Aug 27, 2012 at 9:19 AM, David Winsemius  wrote:
>
> On Aug 27, 2012, at 3:09 AM, Fridolin wrote:
>
>> What is a smart way to change an entry inside a column of a dataframe or
>> matrix which is of type "factor"?
>>
>> Here is my script incl. input data:
>>>
>>> #set working directory:
>>> setwd("K:/R")
>>>
>>> #read in data:
>>> input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)
>>>
>>> #check data:
>>> input
>>
>>   Ind  M1  M2  M3
>> 11   96/98 120/120 0/0
>> 22 102/108 120/124 305/305
>> 33  96/108 120/120 0/0
>> 44 0/0 116/120 300/305
>> 55  96/108 120/130 300/305
>> 66   98/98 116/120 300/305
>> 77  98/108 120/120 305/305
>> 88  98/108 120/120 305/305
>> 99  98/102 120/124 300/300
>> 10  10 108/108 120/120 305/305
>>>
>>> str(input)
>>
>> 'data.frame':   10 obs. of  4 variables:
>> $ Ind: int  1 2 3 4 5 6 7 8 9 10
>> $ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
>> $ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2
>> $ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4
>>>
>>>
>>> #replace 0/0 by 999/999:
>>> for (r in 1:10)
>>
>> +   for (c in 2:4)
>> + if (input[r,c]=="0/0") input[r,c]<-"999/999"
>> Warnmeldungen:
>> 1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>>  invalid factor level, NAs generated
>> 2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>>  invalid factor level, NAs generated
>> 3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
>>  invalid factor level, NAs generated
>>>
>>> input
>>
>>   Ind  M1  M2  M3
>> 11   96/98 120/120
>> 22 102/108 120/124 305/305
>> 33  96/108 120/120
>> 44 116/120 300/305
>> 55  96/108 120/130 300/305
>> 66   98/98 116/120 300/305
>> 77  98/108 120/120 305/305
>> 88  98/108 120/120 305/305
>> 99  98/102 120/124 300/300
>> 10  10 108/108 120/120 305/305
>>
>>
>> I want to replace all "0/0" by "999/999". My code should work for columns
>> of
>> type "character" and "integer". But to make it work for a "factor"-column
>> I
>> would need to add the new level of "999/999" at first, I guess. How do I
>> add
>> a new level?
>
>
> ?levels
>
> levels(input$M1) <- c(levels(input$M1), "999/999")

This adds an additional level; then you have to replace the "0/0"
level with this one; then you have to call levels again to remove the
"0/0" level.

I think the following slight tweak may be preferred, as illustrated
with a little example (opinions?):

> x <- factor(letters[1:3])
> x
[1] a b c
Levels: a b c

## create a new levels vector
> newlvl <- levels(x)
> newlvl[newlvl == "a"] <- "d"

## Create the new factor and replace the old with it

> x <- factor(newlvl[x])
> x
[1] d b c
Levels: b c d

Note, however, as Bill D. said, in either case your level ordering --
which will be used, e.g. in printing and displaying -- will be weird.



>
> --
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> __
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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 average time series data around regular intervals

2012-08-27 Thread jim holtman
try this:


> x <- read.table(text = "2012-07-22 12:12:00, 21
+ 2012-07-22 12:15:00, 22
+ 2012-07-22 12:18:00, 24
+ 2012-07-22 12:39:00, 21
+ 2012-07-22 12:45:00, 25
+ 2012-07-22 12:49:00, 26
+ 2012-07-22 12:53:00, 20
+ 2012-07-22 13:00:00, 18
+ 2012-07-22 13:06:00, 22", colClasses = c("POSIXct", "integer"), sep = ',')
> # get minimum at an hour granularity
> tMin <- trunc(min(x$V1), units = 'hour')
> # back off 7.5 minute
> tMin <- tMin - (7.5 * 60)
> # create sequence for 'cut'
> cSeq <- seq(tMin, max(x$V1) + (7.5 * 60), by = '15 min')
> # now split and average
> cCut <- cut(x$V1, cSeq)
> # compute means
> tapply(x$V2, cCut, mean)
2012-07-22 11:52:30 2012-07-22 12:07:30 2012-07-22 12:22:30 2012-07-22 12:37:30
 NA22.3  NA24.0
2012-07-22 12:52:30
   20.0
>


On Mon, Aug 27, 2012 at 9:53 AM, Jason Gilmore  wrote:
>
> Hi,
>
> I'm pretty new to R and have run into a task which although I'm certain is
> within R's capabilities, falls outside of mine. :-) Consider the following
> data set:
>
> 2012-07-22 12:12:00, 21
> 2012-07-22 12:15:00, 22
> 2012-07-22 12:18:00, 24
> 2012-07-22 12:39:00, 21
> 2012-07-22 12:45:00, 25
> 2012-07-22 12:49:00, 26
> 2012-07-22 12:53:00, 20
> 2012-07-22 13:00:00, 18
> 2012-07-22 13:06:00, 22
>
> My task involves creating a data set which *averages* these values at a
> resolution of 15 minutes, meaning that I need to average the values falling
> within 7.5 minutes of a 15 minute increment. Therefore given the above data
> set I need to treat it as three groups:
>
> 2012-07-22 12:12:00, 21
> 2012-07-22 12:15:00, 22
> 2012-07-22 12:18:00, 24
>
> 2012-07-22 12:39:00, 21
> 2012-07-22 12:45:00, 25
> 2012-07-22 12:49:00, 26
>
> 2012-07-22 12:53:00, 20
> 2012-07-22 13:00:00, 18
> 2012-07-22 13:06:00, 22
>
> The end result should look like this:
>
> 2012-07-22 12:15:00, 22.33
> 2012-07-22 12:30:00, NA <- Because this 15 minute slot did not previously
> exist
> 2012-07-22 12:45:00, 24
> 2012-07-22 1:00:00, 20
>
> Any help much appreciated. I've been working on this for several hours with
> little success. I'm able to identify the missing (NA) value using zoo/xts
> but can't seem to sort out the averaging matter.
>
> Thanks so much!
> Jason
>
> [[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.




--
Jim Holtman
Data Munger Guru

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

__
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] Drawing a simplex

2012-08-27 Thread Chel Hee Lee
Thank you, David.  I found some good stuffs for which I have been
looking. 

On Sun, 2012-08-26 at 09:01 -0700, David Winsemius wrote:
> On Aug 25, 2012, at 9:00 PM, Chel Hee Lee wrote:
> 
> > I will very appreciate if anyone can provide some materials to draw a
> > simplex plot of a Dirichlet distribution in R as shown in the page at
> > http://en.wikipedia.org/wiki/File:Dirichlet_distributions.png .
> 
> Go to:
> 
> http://www.rseek.org
> 
> search on:   dirichlet surface
> 
> Choose the Support lists panel. There are worked examples there.
>

__
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] dimnames in an array(I'll be grateful if this message will be passed to all list users)

2012-08-27 Thread William Dunlap
   res3 = Profile.LS(fhn, data=data2, times=times, pars=pars,
   coefs=coefs, lambda=lambda, out.meth='nls',
   control.in=control.in, control.out=control.out)

  #Rather than parameter estimates, as with the single replicate 
analysis, this produces the error:

   Error in as.array.default(Y) : attempt to set an attribute on 
NULL

This happens for me when I omit the basisvals argument to Profile.LS().
Profile.LS ought to catch this directly.

Profile.LS also produces a slew of warnings while running the example in its
help file.  E.g.,
  Warning in dim(weights[whichrows, ]) == dim(diffs) :
  longer object length is not a multiple of shorter object length

You may want to correspond with maintainer of the CollocInfer package:
  R> maintainer("CollocInfer")
  [1] "Giles Hooker "

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

From: aleksandr shfets [mailto:a_shf...@mail.ru]
Sent: Wednesday, August 22, 2012 1:52 AM
To: William Dunlap
Cc: R. Michael Weylandt; r-help@r-project.org
Subject: Re[2]: [R] dimnames in an array(I'll be grateful if this message will 
be passed to all list users)

Hi,
Thank you for the suggestions.
I've attached the code in the text file "test"; to run this one needs the 
CollocInfer package(available under R 2.14, now I think if you have 2.14 on 
your system you can download CollocInfer from CRAN).

I've tried the 'error' options you suggest; using the syntax  
"dimnames(data2)[1:2]" does seem to give an improvement of the attributes of 
the array,

But even with the attributes seeming to be in as good order as they might 
be(i.e., each boxcar, [[1]],[[2]],[[3]] contains the correct number of names), 
rerunning the analysis with the revised array returns the same error.


traceback() gives
4: `colnames<-`(`*tmp*`, value = c("V", "R"))
3: objective(.par, ...)
2: nlminb(coefs, SplineCoefsErr, gradient = SplineCoefsDC, hessian = Hessian,
   control = control.in, times = times, data = data, lik = lik,
   proc = proc, pars = pars)
1: inneropt(coefs = DEfd2$coefs, times = times, data = data2, lik = lik,
   proc = proc, pars = spars, in.meth = "nlminb", control.in = control.out)

To my (untrained) eye, this points again to some kind of mismatch between the 
array 'data2' and its dimnames.

Again I'll be grateful if anyone can see what the problem is.

regards,

A
(recover() also is unfruitful)



Fri, 17 Aug 2012 15:28:19 + от William Dunlap 
mailto:wdun...@tibco.com>>:
Have you showed us how to reproduce your original problem?
Have you showed us the output of traceback() after encountering
the error? Have you tried setting options(error=recover) before
encountering the error and then using recover() to look at the dimensions
and dimnames of the array that caused the problem?

This error message
> > Error in `colnames<-`(`*tmp*`, value = c("V", "R")) :
> > length of 'dimnames' [2] not equal to array extent
comes from a nested replacement operation. One such such case is
 mat <- matrix(1:6,nrow=2,ncol=3)
 colnames(mat)[1:2] <- c("V", "R")
where the nested replacement gets expanded into
 tmp <- colnames(mat) # tmp becomes NULL
 tmp[1:2] <- c("V", "R") # tmp becomes c("V","R"), length is 2
 colnames(tmp) <- tmp # error: 2 colnames for 3 columns
(R uses `*tmp*` where I used tmp - the former is for internal use only.)

There are lots of other possibilities, but you need to at least show the
output of traceback() to pin it down.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: 
> r-help-boun...@r-project.org
>  
> [mailto:r-help-boun...@r-project.org]
>  On Behalf
> Of aleksandr shfets
> Sent: Friday, August 17, 2012 6:25 AM
> To: R. Michael Weylandt
> Cc: r-help@r-project.org
> Subject: Re: [R] dimnames in an array(I'll be grateful if this message will 
> be passed to all
> list users)
>
>
> Michael,
> Thank you for suggestions;
> it seems to me that there's a fundamental lacuna with respect to names of a 
> three
> dimensional array:
> that is, rownames fits dimension 1, colnames fits another dimension(that is, 
> 3, if I read it
> correctly),
> but there is no specific name for the third dimension. I've tried "setnames" 
> thinking that
> this would be appropriate
> for naming the sets, but there is no such R function.
>
> On the other hand you're right, the suggestion
>  dimnames(data11a)[[2]]=c("V","R")
>
> doesn't ameliorate the analysis: while now dimnames(data11a)[[2]]
>
> comes out as
> [2]
>
> returning to the original analysis the same error comes up, that is, that the 
> length of
> dimnames[2] doesn't agree with thee.
> 'array extent' -- that is, even though the boxcar contents are two in number, 
> it evaluates
> the analysis on the basis of the
> dimnames(data11a)[2] value.
>
> I'm using colloc infer (available under R 2.14) and trying to do the 
> FHN(FitzHugh Nagumo)
> ana

[R] Reading pdf file into R

2012-08-27 Thread Christofer Bogaso
Dear all, I have got a pdf file with lot of numerical data which I
want to export to R for some analysis. Is there any way to doing that?

Thanks for your time.

Thanks and regards,

__
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] simplest way (set of functions) to parse a file

2012-08-27 Thread Giovanni Azua
Thank you very much!

It worked nicely.

Best!
Giovanni

On Aug 27, 2012, at 4:18 PM, jim holtman wrote:

> try this:
> 
> 
> > input <- readLines(textConnection("Iteration   JminError   Elapsed 
> > Corral  Duality Gap Step1   Step2   Step3   Step4
> + 2   2   0.00e+001.912976e-031   0.00e+00
> 1.779780e-037.214600e-051.243600e-052.246700e-05
> + 
> ../test/genmoons_data/logdet_two_moons_n10.txt,2,2,1.754115e-02,0.00e+00,9.799000e+03,0.00e+00,5.586293e-01,0.00e+00
> + Iteration   JminError   Elapsed Corral  Duality Gap Step1   
> Step2   Step3   Step4
> + 4   9   0.00e+001.280841e-032   -7.105427e-15   
> 9.557570e-042.301610e-041.571100e-052.177300e-05
> + 
> ../test/genmoons_data/logdet_two_moons_n20.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00
> + Iteration   JminError   Elapsed Corral  Duality Gap Step1   
> Step2   Step3   Step4
> + 10  32  3.133476e-036.075853e-038   4.057531e-01
> 1.613035e-033.956920e-033.077200e-054.390900e-05
> + 20  28  5.597685e-044.376530e-0316  4.711146e-03
> 0.00e+004.390998e-032.229600e-052.517100e-05
> + 30  27  1.148159e-044.357923e-0322  8.408166e-06
> 0.00e+004.326610e-032.697700e-053.233200e-05
> + 40  27  4.036778e-054.388260e-0329  2.529294e-07
> 0.00e+004.329726e-032.713000e-053.558400e-05
> + 50  27  1.840383e-054.357373e-0336  1.34e-07
> 0.00e+004.327526e-033.097000e-053.255700e-05
> + 53  27  0.00e+001.322382e-0336  -2.842171e-14   
> 0.00e+001.327239e-031.420400e-052.043500e-05
> + 
> ../test/genmoons_data/logdet_two_moons_n64.txt,53,69,3.330987e-02,0.00e+00,2.229830e+07,0.00e+00,6.694201e+02,0.00e+00
> + Iteration   JminError   Elapsed Corral  Duality Gap Step1   
> Step2   Step3   Step4
> + 10  70  7.739525e-032.389529e-028   1.494829e+00
> 2.975209e-031.873082e-024.713600e-055.837200e-05
> + 20  74  3.379192e-032.084753e-0215  3.372041e-01
> 0.00e+002.084637e-024.302400e-053.907800e-05
> + 30  76  1.322821e-032.093204e-0221  1.018845e-01
> 0.00e+002.083170e-024.704100e-055.707100e-05
> + 40  78  1.176950e-032.095179e-0228  2.447970e-02
> 0.00e+002.088284e-024.890700e-054.955100e-05
> + 50  78  2.233669e-042.050571e-0235  1.573952e-02
> 0.00e+002.045954e-024.046600e-053.899000e-05
> + 60  78  2.167956e-042.095130e-0239  8.362982e-03
> 0.00e+002.082586e-026.699700e-058.506400e-05
> + 70  78  2.085968e-042.085355e-0246  5.135190e-03
> 0.00e+002.083204e-025.432900e-054.078600e-05
> + 80  78  2.570800e-042.044932e-0251  5.470225e-04
> 0.00e+002.033571e-025.334200e-055.318400e-05
> + 81  78  0.00e+002.099610e-0351  1.421085e-14
> 0.00e+002.100072e-039.147000e-062.324800e-05
> + 
> ../test/genmoons_data/logdet_two_moons_n.txt,4,5,6.062756e-03,0.00e+00,1.365970e+05,0.00e+00,2.253051e+01,0.00e+00"))
> > closeAllConnections()
> > 
> > # remove headers
> > input <- input[!grepl("^Iteration", input)]
> > 
> > # extract the file names
> > files <- input[grepl("^\\.\\.", input)]
> > 
> > # add separation based on file name
> > x <- cumsum(grepl("^\\.\\.", input))
> > 
> > # prepend separation onto the input
> > input <- paste(x, input)
> > 
> > # remove the file names
> > input <- input[!grepl("\\.\\.", input)]
> > 
> > # extract 'n' from the file names
> > n <- as.integer(sub(".*_n([0-9]+).*", "\\1", files))
> > 
> > # read the modified data back in 
> > tempFile <- tempfile()
> > writeLines(input, tempFile)
> > newInput <- read.table(tempFile)
> > 
> > # replace first column with 'n'
> > newInput[[1]] <- n[newInput[[1]] + 1L]
> > 
> > names(newInput) <- c('n', 'iter', 1,2,3,4,5,'Step1', 'Step2', 'Step3', 
> > 'Step4')
> > 
> > require(reshape2)
> > x <- melt(newInput, id = c('n', 'iter'), measure = 
> > c('Step1','Step2','Step3', 'Step4'))
> > x
>   n iter variable   value
> 1102Step1 0.001779780
> 2204Step1 0.000955757
> 364   10Step1 0.001613035
> 464   20Step1 0.0
> 564   30Step1 0.0
> 664   40Step1 0.0
> 764   50Step1 0.0
> 864   53Step1 0.0
> 9     10Step1 0.002975209
> 10    20Step1 0.0
> 11    30Step1 0.0
> 12    40Step1 0.0
> 13    50Step1 0.0
> 14    60Step1 0.0
>

Re: [R] Reading pdf file into R

2012-08-27 Thread Berend Hasselman

On 27-08-2012, at 20:21, Christofer Bogaso wrote:

> Dear all, I have got a pdf file with lot of numerical data which I
> want to export to R for some analysis. Is there any way to doing that?
> 
> Thanks for your time.


Possibly the program pdftotext from xpdf tools 
(http://www.foolabs.com/xpdf/download.html) could help you.

Berend

__
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] write.matrix.csr data conversion

2012-08-27 Thread Sam Steingold
> write.matrix.csr(mx, y = y, file = file)
> table(y)
  0   1 
5194394   23487
$ cut -d' ' -f1 f | sort | uniq -c
  23487 2
5194394 1

i.e., 0 is written as 1 and 1 is written as 2.
why?
is there a way to disable this?

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://mideasttruth.com
http://pmw.org.il http://ffii.org http://www.memritv.org http://dhimmi.com
Experience always comes right after it would have been useful.

__
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 pdf file into R

2012-08-27 Thread Christofer Bogaso
Thanks Berend for your reply. However I was expecting something may be
available within R itself (or perhaps some added package.)

Thanks and regards,

On Tue, Aug 28, 2012 at 12:23 AM, Berend Hasselman  wrote:
>
> On 27-08-2012, at 20:21, Christofer Bogaso wrote:
>
>> Dear all, I have got a pdf file with lot of numerical data which I
>> want to export to R for some analysis. Is there any way to doing that?
>>
>> Thanks for your time.
>
>
> Possibly the program pdftotext from xpdf tools 
> (http://www.foolabs.com/xpdf/download.html) could help you.
>
> Berend
>

__
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] write.matrix.csr data conversion

2012-08-27 Thread jim holtman
Most likely when 'y' is converted to a dataframe (not sure what the
function 'write.matrix.csr' does since you did not say where you got
it), '0' and '1' are converted to factors which probably show up as 1
and 2 in the file.  Here is a quick example:

> x <- sample(c(0,1), 20,TRUE)
> y <- table(x)
> y
x
 0  1
12  8
> str(y)
 'table' int [1:2(1d)] 12 8
 - attr(*, "dimnames")=List of 1
  ..$ x: chr [1:2] "0" "1"
> y.df <- as.data.frame(y)
> str(y.df)
'data.frame':   2 obs. of  2 variables:
 $ x   : Factor w/ 2 levels "0","1": 1 2  #<<- norice the 1 & 2 as the
values of the factors '0' & '1'
 $ Freq: int  12 8
>



On Mon, Aug 27, 2012 at 2:47 PM, Sam Steingold  wrote:
>> write.matrix.csr(mx, y = y, file = file)
>> table(y)
>   0   1
> 5194394   23487
> $ cut -d' ' -f1 f | sort | uniq -c
>   23487 2
> 5194394 1
>
> i.e., 0 is written as 1 and 1 is written as 2.
> why?
> is there a way to disable this?
>
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 
> 11.0.11103000
> http://www.childpsy.net/ http://palestinefacts.org http://mideasttruth.com
> http://pmw.org.il http://ffii.org http://www.memritv.org http://dhimmi.com
> Experience always comes right after it would have been useful.
>
> __
> 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
Data Munger Guru

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

__
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] matrix.csr %*% matrix --> matrix

2012-08-27 Thread Sam Steingold
When a sparse matrix is multiplied by a regular one, the result is
usually not sparse. However, when matrix.csr is multiplied by a regular
matrix in R, a matrix.csr is produced.
Is there a way to avoid this?
Thanks!
-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://palestinefacts.org http://truepeace.org
http://americancensorship.org http://honestreporting.com
If you have no enemies, you are probably dead.

__
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 pdf file into R

2012-08-27 Thread Marc Schwartz
I was going to point you to the same utility, which I have used over the years 
on both Linux and OSX. A Google search using "pdf to text" will bring up a 
variety of non-R related possibilities.

It is possible that somebody, somewhere has built an interface in R to 
pdftotext, such as a wrapper function, whereby pdftotext is called via the use 
of system(). 

I don't see anything obvious on CRAN and if such a thing existed, it would make 
most sense to utilize the functionality of pdftotext, which is pretty mature, 
rather than develop something from scratch.

Of course, the use of pdftotext itself is predicated upon the source PDF not 
being a scanned image of a text page, in which case you would need an OCR based 
application.

Regards,

Marc Schwartz

On Aug 27, 2012, at 1:48 PM, Christofer Bogaso  
wrote:

> Thanks Berend for your reply. However I was expecting something may be
> available within R itself (or perhaps some added package.)
> 
> Thanks and regards,
> 
> On Tue, Aug 28, 2012 at 12:23 AM, Berend Hasselman  wrote:
>> 
>> On 27-08-2012, at 20:21, Christofer Bogaso wrote:
>> 
>>> Dear all, I have got a pdf file with lot of numerical data which I
>>> want to export to R for some analysis. Is there any way to doing that?
>>> 
>>> Thanks for your time.
>> 
>> 
>> Possibly the program pdftotext from xpdf tools 
>> (http://www.foolabs.com/xpdf/download.html) could help you.
>> 
>> Berend

__
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] write.matrix.csr data conversion

2012-08-27 Thread Sam Steingold
> * jim holtman  [2012-08-27 14:55:08 -0400]:
>
> Most likely when 'y' is converted to a dataframe (not sure what the
> function 'write.matrix.csr' does since you did not say where you got
> it),

sorry,
library(e1071)

> '0' and '1' are converted to factors which probably show up as 1
> and 2 in the file.

sounds reasonable, thanks.

David, could you please add an option `fac' to `write.matrix.csr',
similar to `read.matrix.csr' which already accepts `fac'?

thanks!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://ffii.org http://honestreporting.com
http://thereligionofpeace.com http://memri.org http://pmw.org.il
I'd give my right arm to be ambidextrous.

__
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] Filtering Beadarray Summary Data

2012-08-27 Thread gundliwe
 Hey all,

I was wondering if someone could tell me a way to remove the lines in my
annoated BeadSummary Data file that have a PROBEQUALITY of "BAD" and
"Match".

They method they used in the Bead summary tutorial gives me the following
error. 
Anyone have any ideas?

Thanks in advance
Will

ids <- as.character(featureNames(BSData.norm)) 
qual <- unlist(mget(ids, illuminaHumanv4PROBEQUALITY, ifnotfound = NA))
table(qual)
  BadGood Good***GoodNo match Perfect
  123761158 137 507 468   27335
 Perfect*** Perfect
   29112394

rem <- qual == "No match" | qual == "Bad" | is.na(qual)
BSData.filt <- BSData.norm[!rem, ]

I get the following error

Error in FUN(X[[2L]], ...) : (subscript) logical subscript too long





--
View this message in context: 
http://r.789695.n4.nabble.com/Filtering-Beadarray-Summary-Data-tp4641442.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] looping through numbered variables

2012-08-27 Thread Daniel Caro
Thank you, character indexing (?"[") is what I was looking for:

sapply(paste('var', 1:100, sep=''), function(x)
weighted.mean(data[[x]], data$weight))

Daniel

On Mon, Aug 27, 2012 at 6:26 PM, Bert Gunter  wrote:
> Have you read "An Introduction to R". If not, please do so before
> osting and pay particular attention to the section on indexing. If so,
> re-read the sections on indexing.
>
> For a terser exposition, ?"["
>
> -- Bert
>
> On Mon, Aug 27, 2012 at 7:11 AM, Daniel Caro  wrote:
>> Hello,
>>
>> This is a beginner question. I am trying to loop through numbered
>> variables with "apply" to calculate weighted means. My data is "data",
>> the variables are "var1" to "var100", the weight is "weight". The
>> command works using
>>
>> sapply(paste('data$var', 1:100, sep=''), function(x)
>> weighted.mean(eval(parse(text=x)), data$weight))
>>
>> but is there a way to avoid eval(parse())?
>>
>> Thank you,
>> Daniel
>>
>> __
>> 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.
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] Standard deviation from MANOVA??

2012-08-27 Thread BrutishFruit
Hi David.
I mean that I want to get the *standard error of the predicted means* (which
is a type standard deviation, if I have understand everything right), which
the se.fit switch mentioned above should require from the "predict()"
function.
But the se.fit switch doesn't seem to work for manova object for some
reason, which is my problem.

Thanks for a suggestion of solution Jean.
But i don't think that the standard error for the individual models will be
the same as for the manova model.



--
View this message in context: 
http://r.789695.n4.nabble.com/Standard-deviation-from-MANOVA-tp4641322p4641441.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] High Density region using hdrcde

2012-08-27 Thread Andras Farkas
Dear All,
 
could you please confirm (or disconfimr, and then please explain:-)) my 
understanding of the results generated by the following code:
 
hdr.den(rnorm(200,55,3))
 
result:
 
    [,1] [,2]
99% 47.07170 63.26864
95% 48.83670 61.55108
50% 53.04884 57.37916
 
the way I am interpreting the results is that in this normally distributed data 
set with the mean of 55 and SD of 3:
 
1. 99% of all randomly generated values lie within 47.07170 and 63.26864
2. 95% of all randomly generated values lie within 48.83670 and 61.55108
3. 50% of all randomly generated values lie within 53.04884 and 57.37916
 
apreciate the help,
 
Sincerely,
 
Andras
[[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] RGL plot : lighting problem when triangle3d and persp3d are used in the same plot

2012-08-27 Thread Stephane Chantepie
Dear all,

I have tried to plot a triangular matrix with the function persp3d(rgl).

for example

z=rbind(c(1,NA,NA,NA),c(5,3,NA,NA),c(4,2,9,NA),c(8,6,5,11))
x=1:4
y=1:4
persp3d(x,y,z,color="gray")

The two extreme points are not plotted (value=1 and value=10). It seems
because the half of the matrix have 'NA'  and perp3d need planar
quadrilateral face. So I decided to use the function triangle3d to
integrate these points into the scene.

So, for the first point I did :

triangles3d(x=c(1,2,2),y=c(1,1,2),z=c(1,5,3)) and it worked

But a problem remains because the light movment on this triangle looks
independant and I did not find how to solve this issue.

How can I prefectly integrate this triangle into the scene?

Thank for help

[[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] ?nchar ?strsplit

2012-08-27 Thread Sapana Lohani
Hi, my data frame is

x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))

I want to split my column ID using "/" as the place to split. How can I do that 
without telling the code how many sub-columns. I could 
use nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row of the 
column, but could not use it to split ID in.

Thanks

[[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] has anybody used gpuCor?

2012-08-27 Thread flora flora
I tried to calculate the pairwise pearson correlation of the column
vector of a matrix of 20,000 columns using the gpuCor fucntion from the
package gputools, and compare the speed with the ordinary cor function.

The gpuCor is supposed to be much faster than cor. But I found that the
ordinay cor is faster than gpuCor.

Anybody has any experience with the gpuCor function? And is it fater han
cor fucntion?

Thanks very much.

[[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 with recursive least squares

2012-08-27 Thread Nikhil Joshi
I need some help with using recursive least squares  lm.fit.recursive
{quantreg}.
I found some references online but cannot find a reproducible example as to
how to use recursive least squares.
I'd really appreciate if anyone can point me to a reproducible example.

[[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 generate a matrix of Beta or Binomial distribution

2012-08-27 Thread R. Michael Weylandt
On Mon, Aug 27, 2012 at 9:49 AM, JeffND  wrote:
> Hi folks,
>
> I have a question about how to efficiently produce random numbers from Beta
> and Binomial distributions.
>
> For Beta distribution, suppose we have two shape vectors shape1 and shape2.
> I hope to generate a 1 x 2 matrix X whose i th rwo is a sample from
> reta(2,shape1[i]mshape2[i]). Of course this can be done via loops:
>
> for(i in 1:1)
> {
> X[i,]=rbeta(2,shape1[i],shape2[i])
> }
>
> However, the above code is time consuming. It would be better to directly
> generate X without any loops. I tried the following code
>
> X<- rbeta(2,shape1,shape2)

No, not quite: this only makes 2 random variates, while before you
made 2*10,000 = 20k. You can make them all in one fell swoop like
this:

X <- rbeta(2, shape1, shape2)

Now you might ask, "shape1" and "shape2" are only 10k long: how does
this work? R will recycle shape1 and shape2 as needed, so you get a
call as if you actually used

rbeta(2, c(shape1, shape1), c(shape2, shape2))

or more generally

rbeta(2, rep(shape1, length.out = 2), rep(shape2, length.out = 2))

If I understand your code correctly, you want each row to use the same
shape1/shape2 parameters, so we will need to reshape the result of
rbeta() into a matrix: it turns out that this is actually pretty easy
to do for your case:

matrix(rbeta(2, shape1, shape2), ncol = 2)

R will get the correct number of rows by default (it does the 2
elements / 2 columns = 1 rows division internally) and because R
uses column-major ordering, you get the parameter arrangement by happy
coincidence. (Basically, R fills up the first column first, then the
second; this aligns nicely with the recycling behavior in this
problem, so we get back to shape1[1] as soon as we start filling the
second column)

This is a little bit of happy hackery, and you might instead prefer
something more explicit like this:

matrix(rbeta(2, rep(shape1, each = 2), rep(shape2, each = 2)),
ncol = 2, byrow = TRUE)

which will repeat shape1 and shape2 elementwise two times (so c(1,2,3)
becomes c(1,1,2,2,3,3)) and then fills rowwise. It's a little clearer,
at the expense of verbosity.

>
> but it looks not right. So I was wondering if there is an R code doing this.
>
> For Binomial distribution, I have a similar question.  I hope to generate a
> 1 x n matrix Y whose i th row is a sample from rbinom(n,2,p[i]), where p
> is a vector of binomial probabilities. We can also do it by loops
>
> for(i in 1:1)
> {
> Y[i,]=rbinom(n,2,p[i])
> }

This should follow pretty easily from the above.

Hope this helps,
Michael

>
> But it would be nice to do it without using any loops. Is this possible in
> R?
>
> Many thanks for great help and suggestions!
> Jeff
>
>
>
>
>
>
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422.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] ?nchar ?strsplit

2012-08-27 Thread Bert Gunter
(Re-)read ?strsplit. You do not have to tell the code how many columns ... etc.
And the split isn't into sub columns.

y <- as.character(x[[1]] ## You need a character vector argument
strsplit(y,"/") ## works

-- Bert

On Mon, Aug 27, 2012 at 11:40 AM, Sapana Lohani  wrote:
> Hi, my data frame is
>
> x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))
>
> I want to split my column ID using "/" as the place to split. How can I do 
> that without telling the code how many sub-columns. I could use 
> nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row of the 
> column, but could not use it to split ID in.
>
> Thanks
>
> [[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.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] ?nchar ?strsplit

2012-08-27 Thread Bert Gunter
Typo:
> y <- as.character(x[[1]]) ## You need a character vector argument
> strsplit(y,"/") ## works

-- Bert

On Mon, Aug 27, 2012 at 12:58 PM, Bert Gunter  wrote:
> (Re-)read ?strsplit. You do not have to tell the code how many columns ... 
> etc.
> And the split isn't into sub columns.
>
> y <- as.character(x[[1]] ## You need a character vector argument
> strsplit(y,"/") ## works
>
> -- Bert
>
> On Mon, Aug 27, 2012 at 11:40 AM, Sapana Lohani  
> wrote:
>> Hi, my data frame is
>>
>> x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))
>>
>> I want to split my column ID using "/" as the place to split. How can I do 
>> that without telling the code how many sub-columns. I could use 
>> nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row of the 
>> column, but could not use it to split ID in.
>>
>> Thanks
>>
>> [[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.
>>
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] saving and reading csv and rda files

2012-08-27 Thread R. Michael Weylandt
On Mon, Aug 27, 2012 at 9:56 AM, Alok Bohara, PhD  wrote:
> Hi:
>
> I am trying to understand the link between ".csv" and  ".rda" files.Is
> there any easy to follow tutorial on this?
>
> (I could do some of the operations below, but I got lost in the details.)
>
> 1.  Reading .rda file ?
>
> data <- load("profit.rda") # supposed to have four variable --y x1
> x2 state_name
>
> --how do I find out about the variable names,
> --take the log of y and x1
> --extract y and calculate mean etc..
>
> (I want to use it in lm regression lny = f(lnx1,x2))

I personally find load() a little dangerous. It reloads objects with
the names they had when you saved them, potentially clobbering other
objects by that name. I'd recommend saveRDS/readRDS personally since
they don't clobber (they instead do what I think you think load()
does). That said,

data <- load(...) actually doesn't put the variables in data; rather
it puts them into the global envir by their original name and puts the
names as strings into data. I'd guess if you type data, you'll find
something like

> data
[1] "y" "x1" "x2", "state_name"

You probably don't want to use "data" as a name in real code, because
it's the name of an important function used to, not surprisingly, load
data.

As far as the model fitting code, that should be covered in most basic
R tutorials, but you're looking for something like

lm( log(y) ~ x1)

Finally, I think you're a little tripped up on the multiple uses of
the term "names". "name" most commonly refers to variable name of an
object, but you seem to be confusing it with column names, which are
often recording what the variables in a data set are. The data set as
a unit has a "name" and then each of the recorded variates corresponds
to a column name. when you read/write csv files with header = TRUE,
the text keeps the column names, not the R object names.


>
> 
>
> 2. How could I save this profit.rda file as a csv file  with the variable
> names attached?
> I tried doing this:
>
> profit_data <- load("profit.rda")

Again, this doesn't work because load() doesn't return the loaded
objects, only the name.

>
> #Could I do this?
> write.csv(profit_data, file="profit.csv", col.names=TRUE)
> data2 <- read.csv("profit.csv", head = TRUE)

This will work in that profit_data == data2, but profit_data isn't the
desired result of load("profit.rda").

>
> # saving .rda file without the header?
> write.csv(profit_data, file="profit2.csv", col.names=FALSE)
>



> ***
>
> 3. Creating a .rda file  from a csv file  using the save command
>
> data2 <- read.csv("poverty.csv", head = T, sep = ",")   # poverty.csv file
> has 4 variables -- Q L K  country_name
> save(data2, file="poverty.rda")
>
> --How do I  attach names from the csv file to this .rda file?

Take a look at data2: it probably has "Q","L","K","country_name" as
_column_ names (not the same as object names). If it does, the
save/load cycle will preserve them.

Cheers,
Michael

>
>
>
> Best,
> Alok Bohara
> UNM
>
> __
> 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] Assigning colors on low p-values in table

2012-08-27 Thread Jean V Adams
Emil,

It's helpful to provide code that generates your (example) data.
You can use the function dput() to do that.

# I set up your matrix a bit differently than what you presented.
output <- structure(c(-2.3109, -0.2934, -0.5539, -2.1352, -2.6968, 
-2.0429, 
-0.3973, -0.6468, 0.4181, -0.6243, 2.9316, 0.1988, 0.9172, 0.8503, 
0.264, 0.0807, 0.2982, 0.9031, 0.8159, 0.9816, 0.8242, 0.99, 
-3.162, -6.1667, -5.5787, -2.705, -1.3854, -2.2508, -5.0478, 
-4.8601, -3.7542, -4.1885, -1.6759, 0.025, 0.01, 0.01, 0.0794, 
0.542, 0.2211, 0.01, 0.01, 0.01, 0.01, 0.4343, -6.0281, -7.897, 
-8.9303, -6.0953, -5.846, -4.761, -6.7784, -8.6037, -5.9572, 
-6.4269, -5.1065, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01), .Dim = c(11L, 6L), .Dimnames = list(c("Liabilities", 
"Long.Bond", "Stocks", "CPI", "Nom.10Y", "T.Bills", "Commodities", 
"Real.Estate", "Credit.Spread", "Term.Spread", "Dividend.Yield"
), c("ADF-Level", "P-Value", "ADF-First D", "P-Value", "ADF-Second D", 
"P-Value")))

# here's one way to color code information in a table in a graphics device
library(gplots)
textplot(format(output), col.data=(output[, c(2, 2, 4, 4, 6, 6)] > 
0.05)+1, halign="right")

Jean


Rmillan  wrote on 08/27/2012 04:05:38 AM:
> 
> Hi all R-users,
> 
> I?m trying to assign colors on those p-value in my table output that 
fall
> above a certain critical value, let?s say a p-value >0.05.
> My table looks like this:
> 
>   AssetsADF-Level  P-Value 
> ADF-First D  P-Value  ADF-Second DP-Value
> [1,]   Liabilities  -2.3109   0.1988 
> -3.162 
> 0.025   -6.0281   0.01
> [2,]   Long.Bond   -0.2934   0.9172 -6.1667 
> 0.01   -7.897 
> 0.01
> [3,]   Stocks  -0.5539 0.8503-5.
> 5787 
> 0.01   -8.9303   0.01
> [4,]   CPI  -2.1352   0.264 -2.705  
> 0.0794   -6.0953   0.01
> [5,]   Nom.10Y  -2.6968   0.0807 -1.
> 3854 
> 0.542   -5.846   0.01
> [6,]   T.Bills  -2.0429   0.2982 -2.
> 2508 
> 0.2211   -4.761   0.01
> [7,]   Commodities   -0.3973   0.9031 -5.0478 
> 0.01   -6.7784 
> 0.01
> [8,]   Real.Estate   -0.6468   0.8159 -4.8601 
> 0.01   -8.6037 
> 0.01
> [9,]   Credit.Spread0.4181   0.9816 -3.7542 
> 0.01   -5.9572 
> 0.01
> [10,]   Term.Spread   -0.6243   0.8242 -4.1885 
> 0.01   -6.4269 
> 0.01
> [11,]   Dividend.Yield2.9316   0.99 -1.6759 0.4343
> -5.1065   0.01
> 
> What could be nice was, if it is possible to highlight, for instance, 
all
> the p-values in this table that are larger than 0.05 with the color red.
> Any ideas on how this can be done?
> 
> All the values in this table are stored in vectors that I have combined 
as a
> matrix and thereafter plotted as a matrix. Therefore this problem 
> could also be solved if the values that are larger than 0.05 in the 
vector
> ?adf.pvalue? are saved as colored values.
> After the values are stored in the vectors, adf.tvalue, adf.pvalue,
> adf.tvalue1, adf.pvalue1, adf.tvalue2 and adf.pvalue2, the code looks 
like
> this: 
> 
> >output<-matrix(c
> 
(col.names,adf.tvalue,adf.pvalue,adf.tvalue1,adf.pvalue1,adf.tvalue2,adf.pvalue2),ncol=7)
> >colnames(output)<-c("Assets","ADF-Level","P-Value","ADF-First
> D","P-Value","ADF-Second D","P-Value")
> >output
> 
> Thanks in advance.
> 
> Sincerely,
> 
> Emil

[[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] Turning of[f] source echo

2012-08-27 Thread David Winsemius
I wasn't an Rstudio user at that point, but now that I am a new user,  
I am wondering "how" you were interacting with the Rstudio windows. I  
have an editing window open and is has both a "Run" button and a  
"Source" button. I get the behavior you describe when I hit the Source  
button and the behavior you desire when I hit the Run button.


And if you were doing it with the menu or keyboard shortcuts you would  
also choose "run" versus "source" (at least on a Mac the menus are  
where you typically learn the default keyboard shortcuts and you can  
generally edit them with the MacOS tools.


--
David
On Aug 25, 2012, at 12:54 PM, darnold wrote:


Berend,

Yes, I did ask this question on RStudio help page. Josh suggested  
that I try
to find some package that pops  up a window with the results I want.  
Anyone
know how to do that? Is there a package or a tutorial that will  
instruct me

as to how to do that?

Here is my code:

BAGA <- c(10,20,20,30,30,30,30,40,
 40,40,40,50,50,50,50,50,
 60,60,60,60,60,70,70,70,
 70,80,80,80,90,90,100)

BAGB <- c(10,10,10,10,10,20,20,20,
  20,30,30,30,40,40,50,60,
  70,70,80,80,80,90,90,90,
  90,100,100,100,100,100)

A <- sample(c(1,2),1)

if (A==1) {
 B <- BAGA[sample(length(BAGA),1)]
} else {
 B <- BAGB[sample(length(BAGB),1)]
}

cat("X = ", B, ".\n",sep="")
if (A==1) cat("The voucher is from Bag A.\n")
if (A==2) cat("The voucher is from Bag B.\n")

What I'd like to do is find a way to repeat this code N number of  
times,
with the student hitting the enter key or the space bar to get the  
next
repetition. Each time, I want a result similar to the following to  
come up,

but I don't want that source message to come up.


source('~/.active-rstudio-document')

X = 30.
The voucher is from Bag A.

Each time
Anyone have any suggestions?

Thanks.

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] RGL plot : lighting problem when triangle3d and persp3d are used in the same plot

2012-08-27 Thread Duncan Murdoch

On 27/08/2012 1:47 PM, Stephane Chantepie wrote:

Dear all,

I have tried to plot a triangular matrix with the function persp3d(rgl).

for example

z=rbind(c(1,NA,NA,NA),c(5,3,NA,NA),c(4,2,9,NA),c(8,6,5,11))
x=1:4
y=1:4
persp3d(x,y,z,color="gray")

The two extreme points are not plotted (value=1 and value=10). It seems
because the half of the matrix have 'NA'  and perp3d need planar
quadrilateral face. So I decided to use the function triangle3d to
integrate these points into the scene.

So, for the first point I did :

triangles3d(x=c(1,2,2),y=c(1,1,2),z=c(1,5,3)) and it worked

But a problem remains because the light movment on this triangle looks
independant and I did not find how to solve this issue.

How can I prefectly integrate this triangle into the scene?


You need to set the normals at the vertices that are shared with the 
persp3d vertices.  rgl internally averages the normals to the pieces 
making up each facet; you don't have enough access to the internals to 
do that.  (You'd want to change the normal on the surface, as well, 
because your triangles should participate in the averaging.)


But you could set the normals to the new triangle to match the normal to 
the one it joins to, and get something that looks sort of okay.  It's 
not going to be a simple calculation:  you need to figure out the 
coordinates of the edges joining at those vertices, take a cross 
product, and use that.  See ?rgl.primitive for how to send the normals 
to points3d; I think the normals need to be length 1, but that may be 
forced internally.


If you were really ambitious, you could take a look at the C++ code that 
draws the surface, and contribute code to smooth the edge properly; I 
think it would be an improvement over the current behaviour, but it is 
tedious to write.


Duncan Murdoch

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


Re: [R] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 12:18 PM, Bert Gunter wrote:


Well ...See below.

-- Cheers, Bert

On Mon, Aug 27, 2012 at 9:19 AM, David Winsemius > wrote:


On Aug 27, 2012, at 3:09 AM, Fridolin wrote:

What is a smart way to change an entry inside a column of a  
dataframe or

matrix which is of type "factor"?

Here is my script incl. input data:


#set working directory:
setwd("K:/R")

#read in data:
input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)

#check data:
input


 Ind  M1  M2  M3
11   96/98 120/120 0/0
22 102/108 120/124 305/305
33  96/108 120/120 0/0
44 0/0 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305


str(input)


'data.frame':   10 obs. of  4 variables:
$ Ind: int  1 2 3 4 5 6 7 8 9 10
$ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
$ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2  
3 2

$ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4



#replace 0/0 by 999/999:
for (r in 1:10)


+   for (c in 2:4)
+ if (input[r,c]=="0/0") input[r,c]<-"999/999"
Warnmeldungen:
1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
invalid factor level, NAs generated
3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
invalid factor level, NAs generated


input


 Ind  M1  M2  M3
11   96/98 120/120
22 102/108 120/124 305/305
33  96/108 120/120
44 116/120 300/305
55  96/108 120/130 300/305
66   98/98 116/120 300/305
77  98/108 120/120 305/305
88  98/108 120/120 305/305
99  98/102 120/124 300/300
10  10 108/108 120/120 305/305


I want to replace all "0/0" by "999/999". My code should work for  
columns

of
type "character" and "integer". But to make it work for a "factor"- 
column

I
would need to add the new level of "999/999" at first, I guess.  
How do I

add
a new level?



?levels

levels(input$M1) <- c(levels(input$M1), "999/999")


This adds an additional level; then you have to replace the "0/0"
level with this one; then you have to call levels again to remove the
"0/0" level.


Then do it this way (different from what I thought was originally  
desired):


> x <- factor(letters[1:3])
> levels(x) <- c("d", levels(x)[2:3])
> x
[1] d b c
Levels: d b c



I think the following slight tweak may be preferred, as illustrated
with a little example (opinions?):


x <- factor(letters[1:3])
x

[1] a b c
Levels: a b c

## create a new levels vector

newlvl <- levels(x)
newlvl[newlvl == "a"] <- "d"


## Create the new factor and replace the old with it


x <- factor(newlvl[x])
x

[1] d b c
Levels: b c d

Note, however, as Bill D. said, in either case your level ordering --
which will be used, e.g. in printing and displaying -- will be weird.


So the above method might be what you expect. Several options are now  
available to the questioner.


--
David.






--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

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




--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm


David Winsemius, MD
Alameda, CA, USA

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


Re: [R] Changing entries of column of type "factor"/Adding a new level to a factor

2012-08-27 Thread Bert Gunter
Perfectly sensible, and indeed what I originally wrote. But it only
works for my trivial example, not the general situation where it might
not be the first level that needs changing.

-- Bert

On Mon, Aug 27, 2012 at 1:52 PM, David Winsemius  wrote:
>
> On Aug 27, 2012, at 12:18 PM, Bert Gunter wrote:
>
>> Well ...See below.
>>
>> -- Cheers, Bert
>>
>> On Mon, Aug 27, 2012 at 9:19 AM, David Winsemius 
>> wrote:
>>>
>>>
>>> On Aug 27, 2012, at 3:09 AM, Fridolin wrote:
>>>
 What is a smart way to change an entry inside a column of a dataframe or
 matrix which is of type "factor"?

 Here is my script incl. input data:
>
>
> #set working directory:
> setwd("K:/R")
>
> #read in data:
> input<-read.table("Exampleinput.txt", sep="\t", header=TRUE)
>
> #check data:
> input


  Ind  M1  M2  M3
 11   96/98 120/120 0/0
 22 102/108 120/124 305/305
 33  96/108 120/120 0/0
 44 0/0 116/120 300/305
 55  96/108 120/130 300/305
 66   98/98 116/120 300/305
 77  98/108 120/120 305/305
 88  98/108 120/120 305/305
 99  98/102 120/124 300/300
 10  10 108/108 120/120 305/305
>
>
> str(input)


 'data.frame':   10 obs. of  4 variables:
 $ Ind: int  1 2 3 4 5 6 7 8 9 10
 $ M1 : Factor w/ 8 levels "0/0","102/108",..: 5 2 4 1 4 8 7 7 6 3
 $ M2 : Factor w/ 4 levels "116/120","120/120",..: 2 3 2 1 4 1 2 2 3 2
 $ M3 : Factor w/ 4 levels "0/0","300/300",..: 1 4 1 3 3 3 4 4 2 4
>
>
>
> #replace 0/0 by 999/999:
> for (r in 1:10)


 +   for (c in 2:4)
 + if (input[r,c]=="0/0") input[r,c]<-"999/999"
 Warnmeldungen:
 1: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated
 2: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated
 3: In `[<-.factor`(`*tmp*`, iseq, value = "999/999") :
 invalid factor level, NAs generated
>
>
> input


  Ind  M1  M2  M3
 11   96/98 120/120
 22 102/108 120/124 305/305
 33  96/108 120/120
 44 116/120 300/305
 55  96/108 120/130 300/305
 66   98/98 116/120 300/305
 77  98/108 120/120 305/305
 88  98/108 120/120 305/305
 99  98/102 120/124 300/300
 10  10 108/108 120/120 305/305


 I want to replace all "0/0" by "999/999". My code should work for
 columns
 of
 type "character" and "integer". But to make it work for a
 "factor"-column
 I
 would need to add the new level of "999/999" at first, I guess. How do I
 add
 a new level?
>>>
>>>
>>>
>>> ?levels
>>>
>>> levels(input$M1) <- c(levels(input$M1), "999/999")
>>
>>
>> This adds an additional level; then you have to replace the "0/0"
>> level with this one; then you have to call levels again to remove the
>> "0/0" level.
>
>
> Then do it this way (different from what I thought was originally desired):
>
>> x <- factor(letters[1:3])
>> levels(x) <- c("d", levels(x)[2:3])
>> x
> [1] d b c
> Levels: d b c
>
>>
>> I think the following slight tweak may be preferred, as illustrated
>> with a little example (opinions?):
>>
>>> x <- factor(letters[1:3])
>>> x
>>
>> [1] a b c
>> Levels: a b c
>>
>> ## create a new levels vector
>>>
>>> newlvl <- levels(x)
>>> newlvl[newlvl == "a"] <- "d"
>>
>>
>> ## Create the new factor and replace the old with it
>>
>>> x <- factor(newlvl[x])
>>> x
>>
>> [1] d b c
>> Levels: b c d
>>
>> Note, however, as Bill D. said, in either case your level ordering --
>> which will be used, e.g. in printing and displaying -- will be weird.
>
>
> So the above method might be what you expect. Several options are now
> available to the questioner.
>
> --
> David.
>>
>>
>>
>>
>>>
>>> --
>>>
>>> David Winsemius, MD
>>> Heritage Laboratories
>>> West Hartford, CT
>>>
>>> __
>>> 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.
>>
>>
>>
>>
>> --
>>
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>>
>> Internal Contact Info:
>> Phone: 467-7374
>> Website:
>>
>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>
> David Winsemius, MD
> Alameda, CA, USA
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

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

[R] To predict Y based on only one sample of X and Y

2012-08-27 Thread Elaine Kuo
Hello



I want to predict wing length using regression commands. (lm and predict)

The data details are as followed



Data:

Bird physiological data

1. body mass

2. body length

3. wing length



Data type:

Order A: consisting of 20 species,

body mass and length of all 20 species are measured

Order B: consisting of 2 species, body mass and length of only 1 species is
measured

Order C: consisting of 5 species, body mass and length of only 1 species is
measured



Method:

1.  for order A, using predict and lm

datam.lm <-lm(Wing.Length~Body.mass+Body.Length)



However, for order B and C, please kindly advise any method to get wing
length based on body mass and length.

(phylogenic relationship is tentatively neglected)



Thank you


Elaine

[[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 generate a matrix of Beta or Binomial distribution

2012-08-27 Thread Rui Barradas

Hello,

I'm glad it helped.
Inline.
Em 27-08-2012 19:00, Zuofeng Shang escreveu:

Hi Rui,

Many thanks for your excellent code! That works very well. But I still 
have a kind of naive question about the set.seed(1)


Consider shape1=c(1,3) and shape2=c(2,4)

Using R I got that

> set.seed(1)
> rbeta(2,c(1,3),c(2,4))
[1] 0.5803922 0.4679171

> set.seed(1)
> c(rbeta(1,1,2),rbeta(1,3,4))
[1] 0.5803922 0.4679171

They have the same outputs. So rbeta(2,shape1,shape2) is the correct 
solution which is equivalent to operating rbeta() componentwise to 
shape1 and shape2. This is exactly the solution I wanted. Thanks a lot!


But I also found the following:

set.seed(1)
> rbeta(1,1,2)
[1] 0.5803922

> set.seed(1)
> rbeta(1,3,4)
[1] 0.3016368

This is pretty strange. Why is the second one 0.3016368 instead of 
being 0.4679171? Probably I was wrong somewhere here.


No, you are not wrong, and no, it is not strange. You are restarting the 
random number generator so your _first_ random beta number cannot be 
eqaul to the _second_ one. Just look:


set.seed(1)
rbeta(1,3,4) # 0.3016368

set.seed(1)
rbeta(2,3,4) # 0.3016368 0.4679171

The second is, as expected, what you had. When yo change the beta 
parameters, the numbers must also change (obvious!)


Rui Barradas



The binomial code works very well. Thanks for this very much!

Have a nice day.
Jeff

于 8/27/2012 1:25 PM, Rui Barradas 写道:

Hello,

Try the following.

set.seed(1)
X <- rbeta(2*length(shape1), rep(shape1, each = 2), rep(shape2, each 
= 2))

X <- matrix(X, ncol = 2, byrow = TRUE)

#-- binomial

n <- 10
p <- runif(1)

Y1 <- matrix(nrow = 1, ncol = 10)

set.seed(6292)
for(i in 1:1){
  Y1[i, ] <- rbinom(n, 2, p[i])
}

set.seed(6292)
Y2 <- rbinom(n*length(p), 2, rep(p, each = 10))
Y2 <- matrix(Y2, ncol = n, byrow = TRUE)

identical(Y1, Y2) #TRUE

The trick is not to use argument recycling.

Hope this helps,

Rui Barradas

Em 27-08-2012 15:49, JeffND escreveu:

Hi folks,

I have a question about how to efficiently produce random numbers 
from Beta

and Binomial distributions.

For Beta distribution, suppose we have two shape vectors shape1 and 
shape2.

I hope to generate a 1 x 2 matrix X whose i th rwo is a sample from
reta(2,shape1[i]mshape2[i]). Of course this can be done via loops:

for(i in 1:1)
{
X[i,]=rbeta(2,shape1[i],shape2[i])
}

However, the above code is time consuming. It would be better to 
directly

generate X without any loops. I tried the following code

X<- rbeta(2,shape1,shape2)

but it looks not right. So I was wondering if there is an R code 
doing this.


For Binomial distribution, I have a similar question.  I hope to 
generate a
1 x n matrix Y whose i th row is a sample from rbinom(n,2,p[i]), 
where p

is a vector of binomial probabilities. We can also do it by loops

for(i in 1:1)
{
Y[i,]=rbinom(n,2,p[i])
}

But it would be nice to do it without using any loops. Is this 
possible in

R?

Many thanks for great help and suggestions!
Jeff









--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422.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] Font size in geom_dl (using ggplot2)

2012-08-27 Thread Matthias Habjan
Hey everyone,

I am an R-newby... so sorry for bothering you with simple-to-solve
questions;) I have the following issue: trying to add labels to my
scatterplots (with geom_dl in ggplot2). Everything works fine, but after
checking every resource I do not find a way to change the font size of my
labels. I tried size, cex, fontsize at every position... but it always stays
the same.

ggplot()+
opts(legend.position="none")+
xlab("x")+ ylab("y")+
xlim(-15,15) + ylim(0,6)+
theme_complete_bw()+
scale_colour_manual(values=cols)+
geom_point(data=m, aes(x=x, y=y, colour=s), shape=19, cex=6,
alpha=0.3)+
geom_dl(method="top.bumptwice", data=m_sig, aes(x=x, y=y,
label=gene.name, colour=s, size=10))+
geom_line(data=m_s0, aes(x= x, y=y), linetype=5, colour="grey55",
size=0.5)

Your help is very much appreciated! Thx.

__
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] Error in Installing RODBC in Linux R

2012-08-27 Thread Madana_Babu
Hi All,

I am trying to install RODBC Package in Linux version of R. PFB the error
message. Request all, if you have any solution how to overcome this error.

configure: error: "ODBC headers sql.h and sqlext.h not found"

Regards,
Madana



--
View this message in context: 
http://r.789695.n4.nabble.com/Error-in-Installing-RODBC-in-Linux-R-tp4641488.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] ?nchar ?strsplit

2012-08-27 Thread David L Carlson
Splitting is easy:

strsplit(as.character(x$ID), "/")

That produces a list with four elements, each of which is a character
vector. R does not have a representation for "sub-columns" so you will have
to be clearer about how you want to represent the data and what you are
planning to do with it.

--
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Sapana Lohani
> Sent: Monday, August 27, 2012 1:41 PM
> To: R help
> Subject: [R] ?nchar ?strsplit
> 
> Hi, my data frame is
> 
> x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))
> 
> I want to split my column ID using "/" as the place to split. How can I
> do that without telling the code how many sub-columns. I could use
> nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row of the
> column, but could not use it to split ID in.
> 
> Thanks
> 
>   [[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 generate a matrix of Beta or Binomial distribution

2012-08-27 Thread JeffND
Dear Michael,

Thanks very much for your explicit explanation! That makes much sense.  

Best wishes,
Jeff



--
View this message in context: 
http://r.789695.n4.nabble.com/How-to-generate-a-matrix-of-Beta-or-Binomial-distribution-tp4641422p4641472.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] find and replace

2012-08-27 Thread Sapana Lohani
I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with 
"y" and all "a" with "b" within these 5 columns. Can I do it in one step?

Thanks
[[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] write.matrix.csr data conversion

2012-08-27 Thread David Meyer

done, thanks for the suggestion.

David

On 2012-08-27 21:15, Sam Steingold wrote:

* jim holtman  [2012-08-27 14:55:08 -0400]:

Most likely when 'y' is converted to a dataframe (not sure what the
function 'write.matrix.csr' does since you did not say where you got
it),


sorry,
library(e1071)


'0' and '1' are converted to factors which probably show up as 1
and 2 in the file.


sounds reasonable, thanks.

David, could you please add an option `fac' to `write.matrix.csr',
similar to `read.matrix.csr' which already accepts `fac'?

thanks!



--
Priv.-Doz. Dr. David Meyer
Department of Information Systems and Operations

WU
Wirtschaftsuniversität Wien
Vienna University of Economics and Business
Augasse 2-6, 1090 Vienna, Austria
Tel: +43-1-313-36-4393
Fax: +43-1-313-36-90-4393
HP:  http://ec.wu.ac.at/~meyer

__
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] ?nchar ?strsplit

2012-08-27 Thread arun


HI,

#In addition to,
nchar1<-nchar(gsub("[^/]","",x$ID)) 
 max(nchar1) 
#[1] 3 
library(stringr) 
 data.frame(str_split_fixed(x$ID,"/",max(nchar1)+1))
#or strsplit(as.character(x$ID),"/")
 
#You can also use:
strsplit(gsub("(.*)/(.*)","\\1 \\2",gsub("(.*)/(.*)/(.*)","\\1 \\2 
\\3",gsub("(.*)/(.*)/(.*)/(.*)","\\1 \\2 \\3 \\4",x$ID)))," ")
#[[1]]
#[1] "abc" "def"

#[[2]]
#[1] "abc" "def" "ghi"

#[[3]]
#[1] "abc"

#[[4]]
#[1] "mno" "pqr" "st"  "ab" 

A.K.


- Original Message -
From: Sapana Lohani 
To: R help 
Cc: 
Sent: Monday, August 27, 2012 2:40 PM
Subject: [R] ?nchar ?strsplit

Hi, my data frame is

x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))

I want to split my column ID using "/" as the place to split. How can I do that 
without telling the code how many sub-columns. I could 
use nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row of the 
column, but could not use it to split ID in.

Thanks

    [[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] Inexplicably different results using subset vs bracket notation on logical variable

2012-08-27 Thread Mauricio Cornejo
Hi,

Would anyone have any idea as to why I would obtain completely different 
results when subsetting using the subset function vs bracket notation?

I have a data frame with 65 variables and 4382 rows. When I use execute the 
following subset command I get the correct results (125 rows)
> subset(df, Renewal==TRUE, 1:2)
  

However, I tried to obtain the same results with bracket notation as follows.  
The output gave me all the rows in the data frame and not just the subset of 
125 I was looking for.
> df[df$Renewal==TRUE, 1:2]

The 'Renewal' variable is of logical type and is the last (65th) variable in 
the data frame.  However, values are either TRUE or NA (there are no 'FALSE' 
values).

My attempts at replicating this with a small dummy data set, for including 
here, have not worked (i.e. I don't get an error when I use synthetic data).  
Any ideas on what could be going on?

Many thanks for any insights anyone may have,
Mauricio

[[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] find and replace

2012-08-27 Thread jim holtman
I am making the assumption that all the columns are character and not factors:

for (i in c("A", "B", "C", "D", "E")){
yourdf[[i]] <- ifelse(yourdf[[i]] == 'x'
, 'y'
, ifelse(yourdf[[i]] == 'a'
, 'b'
, yourdf[[i]]
)
)
}

On Mon, Aug 27, 2012 at 4:19 PM, Sapana Lohani  wrote:
> I have 5 (A,B,C,D,E) columns in my dataframe. I want to replace all "x" with 
> "y" and all "a" with "b" within these 5 columns. Can I do it in one step?
>
> Thanks
> [[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.



-- 
Jim Holtman
Data Munger Guru

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

__
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] Lattice graphics adding abline line (1:1 line) ???

2012-08-27 Thread iwaite
I have read multiple books and looked at many posts online and can't seem to
figure out how to add a 1:1 line in lattice xyplot. I've tried multiple
versions of getting this to work, including trying "panel.abline(h=0,v=1),
panel=function and others. 

Second question, how do I get the legend created via "auto.key" to use the
"pch=c(15,16,17,18) and associated colors, these are circle, square,triangle
and diamond, but the auto.key just plots points with different default
colors, not the grey scales and symbols that the actual plots uses, how do I
get these to match. Is auto.key not the way to go, again I read on many
different methods, but no luck yet.

Thanks,
Ian

xyplot(Inv591$RichTOL~gbm_tol$fitted, data=Inv591,group=EcoRegion, main=
"Observed vs Predicted for RichTOL (BRT development model)",xlab="Observed
RichTOL", ylab="Predicted RichTOL", xlim= c(2,8), ylim= c(2,8),
pch=c(15,16,17,18), cex=1.1,
col=c('grey10','grey30','grey60','grey80'),cex.lab=1.8, cex.axis=1.8,font=2,
auto.key=list(corner=c(1,0)),
panel=function(x,y){
panel.xyplot(x,y)
panel.abline(lm(Inv591$RichTOL~gbm_tol$fitted)) 



--
View this message in context: 
http://r.789695.n4.nabble.com/Lattice-graphics-with-combined-plot-types-tp790070p4641486.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] find and replace

2012-08-27 Thread Sapana Lohani
Hi, 

My data frame (Students) is

ID Name Fav_Place
101 Andrew  Phoenix AZ
201 John San Francisco
303 JulieCalifornia / New York
304 Monica  New York

How can I replace Phoenix with Tucson & New York with New York City in the df?

Thanks
[[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] write.table and read.table commands

2012-08-27 Thread Cheryl Johnson
Greetings,

When I try to use the write.table command to save a matrix as a file and
then open the file with read.table, if I try to take the mean of the entire
matrix, instead each column of the matrix has its mean calculated. I have
copied and pasted an example of my code below. When I try to make the
header false with the read.table command, I am given an error message. I
would appreciate any guidance for how to average the entire matrix instead
of just the columns.

Thanks

z=matrix(1:20,4,5)
write.table(z, file="exercise.csv",sep=",",col.names=NA,qmethod="double")
j=read.table("exercise.csv",header=T,sep=",",row.names=1)
mean(j)

  V1   V2   V3   V4   V5
 2.5  6.5 10.5 14.5 18.5
Warning message:
mean() is deprecated.
 Use colMeans() or sapply(*, mean) instead.

[[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] New to R

2012-08-27 Thread moulirc
Hi,

I am new to R and using Rcmdr and like to automate and get into scripting...

I am using for evaluating two variables 

.Table <- xtabs(~CurSWI+BckMo, data=swanalysis_run1)
.Table
fisher.test(.Table)
remove(.Table)

the output for this will be 
> .Table
  BckMo
CurSWI N/A No Yes
   N/A   1  0   0
   No3 62  38
   Yes   2 23  24

> fisher.test(.Table)

Fisher's Exact Test for Count Data

data:  .Table 
p-value = 0.02689
alternative hypothesis: two.sided
***
What i would like to do is get an output in a excel or csv format with 

p.value and BckMo


(BckMo, 0.02689)

pls help me...thanks in advance..
cheers

Mouli






--
View this message in context: 
http://r.789695.n4.nabble.com/New-to-R-tp4641489.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] Optim Problem

2012-08-27 Thread Christopher Kelvin
Hello,
I want to estimate the exponential parameter by using optim with the following 
input, where t contains 40% of the data and q contains 60% of the data within 
an interval. In implementing the code command for optim i want it to contain 
both the t and q data so i can obtain the correct estimate. Is there any 
suggestion as to how this can be done. I have tried h<-c(t,q) but it is not 
working because q lies within an interval.

rate<-15;n<-100;a<-40;b<-60;rr<-1000
ms11=ms22=0
bia11=bia22=0
t<-rexp(a,rate)
for(i in 1:rr){
C1<-runif(b,0,rate)
C2<-rexp(b,rate)
f2 <- function(C1, C2) {
  r <- pmax(C1 , C2 + C1)
  cbind(C1, r)
} 
m<-f2(C1,C2)
x[1:b]<-(m[,1])
u<-x[1:b]
x[1:b]<-(m[,2])
v<-x[1:b]
q<-cbind(u,v)

h<-c(t,q)

z<-function(data ){ 
rate<-p[2]
log1<--(n/log(p[2]))-sum(t/(p[2]))+sum(log(exp(-(u/(p[2])))-exp(-(v/(p[2])
return(-log1)
}
}
start <- c(1,1)
zz<-optim(start,fn=z,data=h,hessian=T)
m1<-zz$par[2]

thank you

chris b guure
researcher
institute for mathematical research 
upm  


__
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] Inexplicably different results using subset vs bracket notation on logical variable

2012-08-27 Thread William Dunlap
subset(dataFrame, subset) does the equivalent of dataFrame[!is.na(subset) & 
subset,].
I.e., it treats the NA's in the subset argument the same as FALSEs.  Doesn't 
help(subset)
mention this?

By the way, if Renewal is a logical vector, it will be identical to 
Renewal==TRUE so
you may as well leave off the "==TRUE".

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Mauricio Cornejo
> Sent: Monday, August 27, 2012 3:09 PM
> To: r-help@r-project.org
> Subject: [R] Inexplicably different results using subset vs bracket notation 
> on logical
> variable
> 
> Hi,
> 
> Would anyone have any idea as to why I would obtain completely different 
> results when
> subsetting using the subset function vs bracket notation?
> 
> I have a data frame with 65 variables and 4382 rows. When I use execute the 
> following
> subset command I get the correct results (125 rows)
> > subset(df, Renewal==TRUE, 1:2)
> 
> 
> However, I tried to obtain the same results with bracket notation as 
> follows.  The output
> gave me all the rows in the data frame and not just the subset of 125 I was 
> looking for.
> > df[df$Renewal==TRUE, 1:2]
> 
> The 'Renewal' variable is of logical type and is the last (65th) variable in 
> the data
> frame.  However, values are either TRUE or NA (there are no 'FALSE' values).
> 
> My attempts at replicating this with a small dummy data set, for including 
> here, have not
> worked (i.e. I don't get an error when I use synthetic data).  Any ideas on 
> what could be
> going on?
> 
> Many thanks for any insights anyone may have,
> Mauricio
> 
>   [[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] write.table and read.table commands

2012-08-27 Thread R. Michael Weylandt
It's because read.table returns a data frame, not a matrix. You can coerce to a 
matrix with as.matrix() but you might loose information if your variables are 
of different classes. 

Michael

On Aug 27, 2012, at 7:07 PM, Cheryl Johnson  wrote:

> Greetings,
> 
> When I try to use the write.table command to save a matrix as a file and
> then open the file with read.table, if I try to take the mean of the entire
> matrix, instead each column of the matrix has its mean calculated. I have
> copied and pasted an example of my code below. When I try to make the
> header false with the read.table command, I am given an error message. I
> would appreciate any guidance for how to average the entire matrix instead
> of just the columns.
> 
> Thanks
> 
> z=matrix(1:20,4,5)
> write.table(z, file="exercise.csv",sep=",",col.names=NA,qmethod="double")
> j=read.table("exercise.csv",header=T,sep=",",row.names=1)
> mean(j)
> 
>  V1   V2   V3   V4   V5
> 2.5  6.5 10.5 14.5 18.5
> Warning message:
> mean() is deprecated.
> Use colMeans() or sapply(*, mean) instead.
> 
>[[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] ?nchar ?strsplit

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 1:40 PM, Sapana Lohani wrote:


Hi, my data frame is

x<-data.frame(ID=c("abc/def","abc/def/ghi","abc","mno/pqr/st/ab"))

I want to split my column ID using "/" as the place to split. How  
can I do that without telling the code how many sub-columns. I could  
use nchar(gsub("[^/]","",x$ID)) to get how many "/" are in each row  
of the column, but could not use it to split ID in.


> read.table(text=as.character(x$ID), sep="/", fill=TRUE, as.is=TRUE)
   V1  V2  V3 V4
1 abc def
2 abc def ghi
3 abc
4 mno pqr  st ab

--

David Winsemius, MD
Alameda, CA, USA

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


Re: [R] find and replace

2012-08-27 Thread R. Michael Weylandt
Take a look at gsub()

Michael

On Aug 27, 2012, at 6:47 PM, Sapana Lohani  wrote:

> Hi, 
> 
> My data frame (Students) is
> 
> ID Name Fav_Place
> 101 Andrew� Phoenix AZ
> 201 John San Francisco
> 303 JulieCalifornia / New York
> 304 Monica� New York
> 
> How can I replace Phoenix with Tucson & New York with New York City in the df?
> 
> Thanks
>[[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] Inexplicably different results using subset vs bracket notation on logical variable

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 5:08 PM, Mauricio Cornejo wrote:


Hi,

Would anyone have any idea as to why I would obtain completely  
different results when subsetting using the subset function vs  
bracket notation?


I have a data frame with 65 variables and 4382 rows. When I use  
execute the following subset command I get the correct results (125  
rows)

subset(df, Renewal==TRUE, 1:2)



However, I tried to obtain the same results with bracket notation as  
follows.  The output gave me all the rows in the data frame and not  
just the subset of 125 I was looking for.

df[df$Renewal==TRUE, 1:2]


The 'Renewal' variable is of logical type and is the last (65th)  
variable in the data frame.  However, values are either TRUE or NA  
(there are no 'FALSE' values).


That's exactly it. If a logical index returns NA, its row is included  
in the output of "[" extraction. You can correct what I consider a  
failing and others consider a feature with:


df[df$Renewal==TRUE & !is.na(df$Renewal), 1:2]



My attempts at replicating this with a small dummy data set, for  
including here, have not worked (i.e. I don't get an error when I  
use synthetic data).  Any ideas on what could be going on?


You _should_ get the predicted behavior. Perhaps your test case was  
flawed?


> dat <- data.frame(test1=1, Renewal=as.logical( sample(c(0,1,NA),  
20, repl=TRUE)))

> dat[dat$Renewal==TRUE, ]
 test1 Renewal
NA  NA  NA
NA.1NA  NA
31TRUE
NA.2NA  NA
NA.3NA  NA
61TRUE
71TRUE
81TRUE
NA.4NA  NA
12   1TRUE
NA.5NA  NA
NA.6NA  NA
16   1TRUE
17   1TRUE
NA.7NA  NA
NA.8NA  NA

This is all described in ?"["

--

David Winsemius, MD
Alameda, CA, USA

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


Re: [R] [netcdfgroup] [ncdf4] error converting GEIA data to netCDF

2012-08-27 Thread Roy Mendelssohn
If I had to guess, you did not call nc_close or nc_sync.  In netcdf, the data 
is actually really written until that is called.

-Roy

On Aug 27, 2012, at 8:31 PM, Tom Roche wrote:

> 
> summary: I can successfully ncvar_put(...) data to a file, but when I
> try to ncvar_get(...) the same data, I get
> 
>> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
>> else addOffset = 0 : 
>>  argument is of length zero
> 
> How to fix or debug?
> 
> details:
> 
> R code @
> 
> https://github.com/TomRoche/GEIA_to_NetCDF
> 
> successfully (if crudely) uses R packages={ncdf4, maps, fields} to
> 
> + extract data from a GEIA-distributed datafile
> 
> https://github.com/downloads/TomRoche/GEIA_to_netCDF/N2OOC90Y.1A
> 
> + display the data (mostly successfully--the map's legend has problems
>  which I'll attack later)
> 
> https://github.com/downloads/TomRoche/GEIA_to_netCDF/output.1.png
> 
> + create a netCDF file using the data read from the GEIA file. (At
>  least, after nc_sync(netcdf.file), the file `ncdump -h`s properly.)
> 
> However, I can only *put* the data to the netCDF file:
> 
>> ncvar_put(
>> + nc=netcdf.file,
>> + varid=emis.var,
>> + vals=t(global.emis.mx),
>> + start=c(1, 1, 1),
>> + count=c(-1,-1, 1)) # -1 -> all data
> 
> When I try to *pull* the data *from* the netCDF I created,
> 
>>> target.data <- ncvar_get(
>> +   nc=netcdf.file,
>> +   varid=emis.var,
>> +   # read all the data
>> +   start=rep(1, emis.var$ndims),
>> + #  count=rep(-1, emis.var$ndims))
> 
> I get
> 
>> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
>> else addOffset = 0 : 
>>  argument is of length zero
> 
> And I get the same error if I try the minor variation(s)
> 
>> target.data <- ncvar_get(
> +   nc=netcdf.file,
> + #  varid=emis.var,
> +   varid=emis.var.name,
> +   # read all the data
> +   start=rep(1, emis.var$ndims),
> +   count=c(-1, -1, 1))
> 
> But the data itself appears to be OK--at least, it virtualizes
> properly (above). So I'm thinking I must just be missing something
> simple, and hoping Someone Out There with fresh eyeballs can point to
> my error(s).
> 
> (And, in case you're wondering:
> 
> - I'm not just ncvar_put'ing the data for the exercise: I want the
>  GEIA data in netCDF format for subsequent use.
> 
> - I tried to find the GEIA data distributed in netCDF format, and
>  asked around, but have gotten no responses.
> 
> ) TIA, Tom Roche 
> 
> ___
> netcdfgroup mailing list
> netcdfgr...@unidata.ucar.edu
> For list information or to unsubscribe,  visit: 
> http://www.unidata.ucar.edu/mailing_lists/ 

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
1352 Lighthouse Avenue
Pacific Grove, CA 93950-2097

e-mail: roy.mendelss...@noaa.gov (Note new e-mail address)
voice: (831)-648-9029
fax: (831)-648-8440
www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

__
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] [netcdfgroup] [ncdf4] error converting GEIA data to netCDF

2012-08-27 Thread Roy Mendelssohn
Just looked at your code, and you do call nc_sync.  I am wondering if it works 
if you call nc_close, reopen and then write. Perhaps the nc_sync is sufficent 
when you have been in define mode in the nc_create call.

-Roy

On Aug 27, 2012, at 8:31 PM, Tom Roche wrote:

> 
> summary: I can successfully ncvar_put(...) data to a file, but when I
> try to ncvar_get(...) the same data, I get
> 
>> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
>> else addOffset = 0 : 
>>  argument is of length zero
> 
> How to fix or debug?
> 
> details:
> 
> R code @
> 
> https://github.com/TomRoche/GEIA_to_NetCDF
> 
> successfully (if crudely) uses R packages={ncdf4, maps, fields} to
> 
> + extract data from a GEIA-distributed datafile
> 
> https://github.com/downloads/TomRoche/GEIA_to_netCDF/N2OOC90Y.1A
> 
> + display the data (mostly successfully--the map's legend has problems
>  which I'll attack later)
> 
> https://github.com/downloads/TomRoche/GEIA_to_netCDF/output.1.png
> 
> + create a netCDF file using the data read from the GEIA file. (At
>  least, after nc_sync(netcdf.file), the file `ncdump -h`s properly.)
> 
> However, I can only *put* the data to the netCDF file:
> 
>> ncvar_put(
>> + nc=netcdf.file,
>> + varid=emis.var,
>> + vals=t(global.emis.mx),
>> + start=c(1, 1, 1),
>> + count=c(-1,-1, 1)) # -1 -> all data
> 
> When I try to *pull* the data *from* the netCDF I created,
> 
>>> target.data <- ncvar_get(
>> +   nc=netcdf.file,
>> +   varid=emis.var,
>> +   # read all the data
>> +   start=rep(1, emis.var$ndims),
>> + #  count=rep(-1, emis.var$ndims))
> 
> I get
> 
>> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
>> else addOffset = 0 : 
>>  argument is of length zero
> 
> And I get the same error if I try the minor variation(s)
> 
>> target.data <- ncvar_get(
> +   nc=netcdf.file,
> + #  varid=emis.var,
> +   varid=emis.var.name,
> +   # read all the data
> +   start=rep(1, emis.var$ndims),
> +   count=c(-1, -1, 1))
> 
> But the data itself appears to be OK--at least, it virtualizes
> properly (above). So I'm thinking I must just be missing something
> simple, and hoping Someone Out There with fresh eyeballs can point to
> my error(s).
> 
> (And, in case you're wondering:
> 
> - I'm not just ncvar_put'ing the data for the exercise: I want the
>  GEIA data in netCDF format for subsequent use.
> 
> - I tried to find the GEIA data distributed in netCDF format, and
>  asked around, but have gotten no responses.
> 
> ) TIA, Tom Roche 
> 
> ___
> netcdfgroup mailing list
> netcdfgr...@unidata.ucar.edu
> For list information or to unsubscribe,  visit: 
> http://www.unidata.ucar.edu/mailing_lists/ 

**
"The contents of this message do not reflect any position of the U.S. 
Government or NOAA."
**
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
1352 Lighthouse Avenue
Pacific Grove, CA 93950-2097

e-mail: roy.mendelss...@noaa.gov (Note new e-mail address)
voice: (831)-648-9029
fax: (831)-648-8440
www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.

__
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] [ncdf4] error converting GEIA data to netCDF

2012-08-27 Thread Tom Roche

summary: I can successfully ncvar_put(...) data to a file, but when I
try to ncvar_get(...) the same data, I get

> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
> else addOffset = 0 : 
>   argument is of length zero

How to fix or debug?

details:

R code @

https://github.com/TomRoche/GEIA_to_NetCDF

successfully (if crudely) uses R packages={ncdf4, maps, fields} to

+ extract data from a GEIA-distributed datafile

https://github.com/downloads/TomRoche/GEIA_to_netCDF/N2OOC90Y.1A

+ display the data (mostly successfully--the map's legend has problems
  which I'll attack later)

https://github.com/downloads/TomRoche/GEIA_to_netCDF/output.1.png

+ create a netCDF file using the data read from the GEIA file. (At
  least, after nc_sync(netcdf.file), the file `ncdump -h`s properly.)

However, I can only *put* the data to the netCDF file:

> ncvar_put(
> + nc=netcdf.file,
> + varid=emis.var,
> + vals=t(global.emis.mx),
> + start=c(1, 1, 1),
> + count=c(-1,-1, 1)) # -1 -> all data

When I try to *pull* the data *from* the netCDF I created,

> > target.data <- ncvar_get(
> +   nc=netcdf.file,
> +   varid=emis.var,
> +   # read all the data
> +   start=rep(1, emis.var$ndims),
> + #  count=rep(-1, emis.var$ndims))

I get

> Error in if (nc$var[[li]]$hasAddOffset) addOffset = nc$var[[li]]$addOffset 
> else addOffset = 0 : 
>   argument is of length zero

And I get the same error if I try the minor variation(s)

> target.data <- ncvar_get(
+   nc=netcdf.file,
+ #  varid=emis.var,
+   varid=emis.var.name,
+   # read all the data
+   start=rep(1, emis.var$ndims),
+   count=c(-1, -1, 1))

But the data itself appears to be OK--at least, it virtualizes
properly (above). So I'm thinking I must just be missing something
simple, and hoping Someone Out There with fresh eyeballs can point to
my error(s).

(And, in case you're wondering:

- I'm not just ncvar_put'ing the data for the exercise: I want the
  GEIA data in netCDF format for subsequent use.

- I tried to find the GEIA data distributed in netCDF format, and
  asked around, but have gotten no responses.

) TIA, Tom Roche 

__
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] write.table and read.table commands

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 10:04 PM, R. Michael Weylandt > wrote:


It's because read.table returns a data frame, not a matrix. You can  
coerce to a matrix with as.matrix() but you might loose information  
if your variables are of different classes.


Michael

On Aug 27, 2012, at 7:07 PM, Cheryl Johnson > wrote:



Greetings,

When I try to use the write.table command to save a matrix as a  
file and
then open the file with read.table, if I try to take the mean of  
the entire
matrix, instead each column of the matrix has its mean calculated.  
I have

copied and pasted an example of my code below. When I try to make the
header false with the read.table command, I am given an error  
message. I
would appreciate any guidance for how to average the entire matrix  
instead

of just the columns.

Thanks

z=matrix(1:20,4,5)
write.table(z,  
file="exercise.csv",sep=",",col.names=NA,qmethod="double")

j=read.table("exercise.csv",header=T,sep=",",row.names=1)
mean(j)

V1   V2   V3   V4   V5
2.5  6.5 10.5 14.5 18.5
Warning message:
mean() is deprecated.
Use colMeans() or sapply(*, mean) instead.


Notice that the error message was telling you that there was a  
`mean.data.frame` function which is slated for removal (perhaps) in  
the next version of R.


You can read about its  behavior:

?mean.data.frame


You could have gotten the desired behavior with:

mean(mean(j))

Or by saving the z object and load it as an Rdata file.

Or with

> z=matrix(1:20,4,5)
> write.table(z, file="exercise.csv",sep=",",row.names=FALSE,  
col.names=FALSE)

> j=scan("exercise.csv",sep=",", what=double())
Read 20 items
> mean(j)
[1] 10.5

(Although the numbers are now in a vector.)



  [[alternative HTML version deleted]]



David Winsemius, MD
Alameda, CA, USA

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


Re: [R] Optim Problem

2012-08-27 Thread Berend Hasselman

On 28-08-2012, at 03:12, Christopher Kelvin wrote:

> Hello,
> I want to estimate the exponential parameter by using optim with the 
> following input, where t contains 40% of the data and q contains 60% of the 
> data within an interval. In implementing the code command for optim i want it 
> to contain both the t and q data so i can obtain the correct estimate. Is 
> there any suggestion as to how this can be done. I have tried h<-c(t,q) but 
> it is not working because q lies within an interval.
> 
> rate<-15;n<-100;a<-40;b<-60;rr<-1000
> ms11=ms22=0
> bia11=bia22=0
> t<-rexp(a,rate)
> for(i in 1:rr){
> C1<-runif(b,0,rate)
> C2<-rexp(b,rate)
> f2 <- function(C1, C2) {
>   r <- pmax(C1 , C2 + C1)
>   cbind(C1, r)
> } 
> m<-f2(C1,C2)
> x[1:b]<-(m[,1])
> u<-x[1:b]
> x[1:b]<-(m[,2])
> v<-x[1:b]
> q<-cbind(u,v)
> 
> h<-c(t,q)
> 
> z<-function(data ){ 
> rate<-p[2]
> log1<--(n/log(p[2]))-sum(t/(p[2]))+sum(log(exp(-(u/(p[2])))-exp(-(v/(p[2])
> return(-log1)
> }
> }
> start <- c(1,1)
> zz<-optim(start,fn=z,data=h,hessian=T)
> m1<-zz$par[2]

Running the code as given gives an error message 

Error in x[1:b] <- (m[, 1]) : object 'x' not found

So insert

x <- numeric(b) 

just before the for(i in 1:rr)

gives an error message

Error in fn(par, ...) : unused argument(s) (par)
Calls: optim ->  -> fn

Not surprising since the argument list of function z should have the parameter 
vector that optim is supposed to optimize as first argument.
So change the definition to

z<-function(p,data ){ 
rate<-p[2]
log1<--(n/log(p[2]))-sum(t/(p[2]))+sum(log(exp(-(u/(p[2])))-exp(-(v/(p[2])
return(-log1)
}

and this give the error message

Error in optim(start, fn = z, data = h, hessian = T) : 
  function cannot be evaluated at initial parameters

So I give up.
Please fix the example to at least something that is without these errors.

Berend

__
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] To predict Y based on only one sample of X and Y

2012-08-27 Thread David Winsemius


On Aug 27, 2012, at 8:49 PM, Elaine Kuo wrote:

I want to predict wing length using regression commands. (lm and  
predict)


The data details are as followed
Data:

Bird physiological data

1. body mass
2. body length
3. wing length

Data type:

Order A: consisting of 20 species, body mass and length of all 20  
species are measured


Order B: consisting of 2 species, body mass and length of only 1  
species is

measured

Order C: consisting of 5 species, body mass and length of only 1  
species is

measured

Method:

1.  for order A, using predict and lm

datam.lm <-lm(Wing.Length~Body.mass+Body.Length)

However, for order B and C, please kindly advise any method to get  
wing

length based on body mass and length.


Perhaps the Delphi method?



(phylogenic relationship is tentatively neglected)


Elaine

[[alternative HTML version deleted]]

--
David Winsemius, MD
Alameda, CA, USA

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


Re: [R] Inexplicably different results using subset vs bracket notation on logical variable

2012-08-27 Thread peter dalgaard

On Aug 28, 2012, at 05:11 , David Winsemius wrote:

> That's exactly it. If a logical index returns NA, its row is included in the 
> output of "[" extraction. You can correct what I consider a failing and 
> others consider a feature with:
> 
> df[df$Renewal==TRUE & !is.na(df$Renewal), 1:2]
> 

Precisely. To elaborate, some consider it a feature because, if the condition 
is NA, you effectively don't know whether to include or not, so it includes 
with NA content, which is arguably a lesser loss of information. 

Also, it treats numerical and logical NA on the same footing so that x[NA] is 
the same as x[as.numeric(NA)]. It's awkward if x[NA] has length zero but 
x[c(1,NA)] has length 2.

For integer NA, you definitely want not to exclude, consider plot(..., 
col=c("red", "blue", "green")[g])

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

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