Re: [R] simulate data from multivariate normal with pre-specified correlation matrix

2007-08-14 Thread Thomas Harte
# here's your example correlation matrix:
sigma<- matrix(c(1.00, 0.75, 0,
 0.75, 1.00, 0,
 0.00, 0.00, 0), nr=3, byrow=TRUE)
chol(sigma)
# Error in chol(sigma) : the leading minor of order 3 is not positive definite

# DUH!

# let's chop off that dangling row and column:
sigma<- sigma[1:2, 1:2]
N<- 1000
set.seed(1)
samp<- matrix(rnorm(N*nrow(sigma)), N, nrow(sigma))%*% chol(sigma)
# check if the empirical correlation is close to the theoretical sigma:
cor(samp)
#  [,1]  [,2]
#[1,] 1.000 0.7502607
#[2,] 0.7502607 1.000

# not bad!



>   Message: 50
>   Date: Mon, 13 Aug 2007 17:15:12 -0400
>   From: Jiao Yang <[EMAIL PROTECTED]>
>   Subject: [R] simulate data from multivariate normal with pre-specified
>   correlation matrix
>   To: r-help@stat.math.ethz.ch
>   Message-ID: <[EMAIL PROTECTED]>
>   Content-Type: text/plain; charset=us-ascii

>   For example, the correlation matrix is 3x3 and looks like 

>   1   0.75  0  0  0
>   0.751 0  0  0
>0   0 0   0  0

>   Can I write the code like  this?

>   p<- 3 >  number of variables per observation
>   N<- 10 >  number of samples

>   >  define population correlation matrix sigma
>   sigma<-matrix(0,p,p) > creates a px p matrix of 0
>   rank<-2
>   for (i in 1:rank){
>   for (j in 1:rank){
>   rho<-0.75
>sigma[i,j]<-rho^abs(i-j)
>   sigma[i,i]<-1
>}
>   }

>   >  create a data set of 10 observations from multivariate normalcovariance 
> matrix
sigma  
>   library(MASS)
>   Xprime<-mvrnorm(N,rep(1,p), sigma )

>   Also, if I calculate

>   S<- cov(Xprime) 

>   will S be the sample correlation matrix?

>   Thank you very much for your time!

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


[R] error in using R2WinBUGS on Ubuntu 6.10 Linux

2007-07-30 Thread Thomas Harte
what version of Wine are you running? and is this the patched (1.4.1) version 
of 
WinBUGS that you're trying to run?

cheers,

thomas.

>  Date: Thu, 26 Jul 2007 12:03:38 -0400 (EDT)
>  From: <[EMAIL PROTECTED]>
>  Subject: [R] error in using R2WinBUGS on Ubuntu 6.10 Linux
>  To: r-help@stat.math.ethz.ch
>  Message-ID: <[EMAIL PROTECTED]>
>  Content-Type: text/plain; charset=us-ascii

>  I am trying to run WinBUGS 1.4 from the Ubuntu 6.10 Linux distribution. I am 
> using the
>   R2WinBUGS packages with the  source file listed below. WinBUGS appears to 
> run
properly, but
>   I get the following message after WinBUGS starts in WINE. Does anyone know 
> what may
be
>   causing this error and what the correction may be?

>  Thanks

>  ERROR MESSAGE:

>  fixme:ole:GetHGlobalFromILockBytes cbSize is 13824
>  err:ole:CoGetClassObject class {0003000a---c000-0046} not 
> registered
>  err:ole:CoGetClassObject class {0003000a---c000-0046} not 
> registered
>  err:ole:CoGetClassObject no class object 
> {0003000a---c000-0046} could
be
>   created for context 0x3
>  fixme:keyboard:RegisterHotKey (0x10032,13,0x0002,3): stub
>  fixme:ntdll:RtlNtStatusToDosErrorNoTeb no mapping for 800a
>  err:ole:local_server_thread Failure during ConnectNamedPipe 317



>  R SOURCE FILE:

>  rm(list=ls(all=TRUE))

>  library(R2WinBUGS)

>  inits<-function(){
>   list(alpha0 = 0, alpha1 = 0, alpha2 = 0, alpha12 = 0, sigma = 1)
>  }

>  data<-list(r = c(10, 23, 23, 26, 17, 5, 53, 55, 32, 46, 10,   8, 10,   8, 
> 23, 0,  3,
22,
>   15, 32, 3),
>  n = c(39, 62, 81, 51, 39, 6, 74, 72, 51, 79, 13, 16, 30, 28, 45, 4, 12, 41, 
> 30, 51,
7),
>  x1 = c(0,   0,  0,   0,   0, 0,   0,   0,  0,   0,   0,  1,   1,   1,   1, 
> 1,   1,  1,
 
>   1,   1, 1),
>  x2 = c(0,   0,  0,   0,   0, 1,   1,   1,  1,   1,   1,  0,   0,   0,   0, 
> 0,   1,  1,
 
>   1,   1, 1),
>  N = 21)

>  test<-bugs(data,inits,

>  model.file="/home/meyerjp/rasch/test.bug",

>  parameters=c("alpha0","alpha1","alpha12","alpha2","sigma"),

>  n.chains=2,n.iter=1,n.burnin=1000,

>  bugs.directory="/home/meyerjp/.wine/drive_c/Program Files/WinBUGS14/",
>  working.directory="/home/meyerjp/rasch/working",

>  debug=FALSE,
>  WINEPATH="/usr/bin/winepath",
>  newWINE=TRUE)

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


Re: [R] How to install R 2.5 with Synaptic in Ubuntu?

2007-07-04 Thread Thomas Harte
mike,

try installing directly using apt-get instead of Synaptic. 

in my /etc/apt/sources.list i added the line:

deb http://cran.R-project.org/bin/linux/ubuntu/ dapper/ 

and then i did:

bash$ sudo apt-get install r-base r-doc-info r-doc-pdf r-doc-html 
r-mathlib  r-base-html
r-base-latex r-base-dev r-gnome

recently to update to R 2.5.1 on my version of Ubuntu (6.06).

cheers,

thomas.


> Message: 98
> Date: Wed, 4 Jul 2007 02:34:37 -0700 (PDT)
> From: msmith <[EMAIL PROTECTED]>
> Subject: Re: [R] How to install R 2.5 with Synaptic in Ubuntu?
> To: r-help@stat.math.ethz.ch
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> Hi,
> 
> Thanks for the suggestion and I wish the solution was that obvious, but I
> have changed it to really point at my favourite mirror.
> 
> Using your example Synaptic reports the following error when I try to update
> the repositories:
> 
>
http://www.stats.bris.ac.uk/R/bin/linux/ubuntu/dists/feisty/main/binary-i386/Packages.gz:
> 404 Not Found
> 
> This is understandable since that location doesn't exist, but it makes me
> think that the directory structure of the R mirrors is not compatible with
> Ubuntu and Synaptic, since it automatically seeks /dists/feisty/ rather than
> just /feisty/ as it is on the CRAN mirrors.
> 
> Thanks again
> Mike Smith
> 
> 
> Stefan Grosse-2 wrote:
> > 
> > 
> >>  to end of the entry making it:
> >>
> >>  deb http://my.favorite.cran.mirror/bin/linux/ubuntu feisty main
> >>
> >> However after this it still complains that it can't find "packages.gz"
> >>
> >>   
> > 
> > Just a guess: have you replaced the my.favorite.cran.mirror by a mirror
> > which is close to you? If you're in UK it would be for example
> > 
> > deb http://www.stats.bris.ac.uk/R/bin/linux/ubuntu feisty main
> > 
> > ;o)
> > Stefan
> > 
> >> It appears to be looking in 
> >> http://my.favorite.cran.mirror/bin/linux/ubuntu/distsfeisty
> >> which isn't the directory structure of the cran repository, but 
> >> I can see anyway to modify this behaviour.  Every other Ubuntu repositoy
> >> I have looked at contains the dists directory.
> >>
> >> Any suggestions for modifying this behaviour are gratefully recieved.
> >> Many thanks
> >>
> >> Mike Smith
> >>
> >>
> >>   
> > 
> > 
> > 
> > 
> > -=-=-
> > ... The simple truth is that interstellar distances will not fit into
> > the human imagination - (The Hitchhiker's Guide to the Galaxy)
> > 
> > __
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> > 
> > 
> 
> -- 
> View this message in context:
> 
http://www.nabble.com/How-to-install-R-2.5-with-Synaptic-in-Ubuntu--tf3998481.html#a11427837
> Sent from the R help mailing list archive at Nabble.com.
>

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


Re: [R] upside down image/data

2006-12-13 Thread Thomas Harte
the transform that i provided orientates the data matrix so that when plotted 
with image
or levelplot the result is isomorphic to what you see when you print the matrix 
at the r
prompt.

i don't know what your data look like---"commented, minimal, self-contained, 
reproducible
code" would help---but you should be able to work out exactly what way you want 
your data
to appear by playing with the example code. i would advise you to produce a 
data matrix
the way you want to see it on the screen, just like the matrix m in the example 
code, and
then view the output with levelplot(inverse(m)), in which case, the answer to 
your
question is you only need to transform the data with inverse() once you get 
your data
matrix to look the way you want at the r prompt. 


--- Jenny Barnes <[EMAIL PROTECTED]> wrote:

> Thomas,
> 
> Thank you for this example, makes it easier to see what levelplot does - does 
> this mean that EVERY time I want to plot with levelplot() I have to not only 
> reverse the columns [,ncol(output.temp):1] but also have to transform the 
> matrix 
> as below? I am only suprised as I don't remember having read about this in 
> the 
> R-info in ?levelplot or R-help website and it seems like a fundamental thing 
> to 
> know if using levelplot! 
> 
> Thanks,
> 
> Jenny
> 
> >
> > rm(list=ls(all=TRUE))
> > graphics.off()
> > # make a test matrix:
> > nr<- 3
> > nc<- 4
> > # the data:
> > ( m<- matrix((1:(nr*nc)), nr, nc) )
> >  [,1] [,2] [,3] [,4]
> > [1,]147   10
> > [2,]258   11
> > [3,]369   12
> >
> > # the way that levelplot (and image) displays the data:
> > t(m)[dim(t(m))[1]:1, ]
> >  [,1] [,2] [,3]
> > [1,]   10   11   12
> > [2,]789
> > [3,]456
> > [4,]123
> >
> > # undo what levelplot does by performing the inverse transformation
> > inverse<- function(x) t(x[dim(x)[1]:1, ]) 
> >
> > windows(); levelplot(m, main="levelplot(m)")
> > windows(); levelplot(inverse(m), main="levelplot(inverse(m))")
> >
> > > Message: 7
> > > Date: Mon, 11 Dec 2006 12:28:17 + (GMT)
> > > From: Jenny Barnes <[EMAIL PROTECTED]>
> > > Subject: [R] upside down image/data
> > > To: r-help@stat.math.ethz.ch
> > > Message-ID: <[EMAIL PROTECTED]>
> > > Content-Type: TEXT/plain; charset=us-ascii
> > > 
> > > Dear R-community,
> > > 
> > > I am looking for some simple advice - I have a matrix (therefore 2 
> dimensional) 
> > > of global temperature. 
> > > 
> > > Having read R-help I think that when I ask R to image() or 
> > levelplot() 
> my matrix 
> > > will it actually appear upside down - I think I therefore need to use 
> the line:
> > > > levelplot(temperature.matrix[,ncol(output.temp):1], )
> > > to get it looking like it was on the globe due to the matrix rows 
> increasing in 
> > > number down the matrix in its dimensions on longitude and latitude 
> > but 
> the 
> > > y-axis coordinates increase up the axis.
> > > 
> > > Can anyone simply tell me whether this is correct as I find it very 
> hard to know 
> > > which way up my data should be and I cannot tell which is correct 
> simply by 
> > > looking at it!
> > > 
> > > Many thanks for your time in reading this problem,
> > > 
> > > Jenny Barnes
> >
> >
> 
> 
> Jennifer Barnes
> PhD student - long range drought prediction
> Climate Extremes
> Department of Space and Climate Physics
> University College London
> Holmbury St Mary, Dorking
> Surrey
> RH5 6NT
> 01483 204149
> 07916 139187
> Web: http://climate.mssl.ucl.ac.uk
> 
> 
>

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


Re: [R] upside down image/data

2006-12-13 Thread Thomas Harte
rm(list=ls(all=TRUE))
graphics.off()
# make a test matrix:
nr<- 3
nc<- 4
# the data:
( m<- matrix((1:(nr*nc)), nr, nc) )
 [,1] [,2] [,3] [,4]
[1,]147   10
[2,]258   11
[3,]369   12

# the way that levelplot (and image) displays the data:
t(m)[dim(t(m))[1]:1, ]
 [,1] [,2] [,3]
[1,]   10   11   12
[2,]789
[3,]456
[4,]123

# undo what levelplot does by performing the inverse transformation
inverse<- function(x) t(x[dim(x)[1]:1, ]) 

windows(); levelplot(m, main="levelplot(m)")
windows(); levelplot(inverse(m), main="levelplot(inverse(m))")

> Message: 7
> Date: Mon, 11 Dec 2006 12:28:17 + (GMT)
> From: Jenny Barnes <[EMAIL PROTECTED]>
> Subject: [R] upside down image/data
> To: r-help@stat.math.ethz.ch
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: TEXT/plain; charset=us-ascii
> 
> Dear R-community,
> 
> I am looking for some simple advice - I have a matrix (therefore 2 
dimensional) 
> of global temperature. 
> 
> Having read R-help I think that when I ask R to image() or 
levelplot() my matrix 
> will it actually appear upside down - I think I therefore need to use 
the line:
> > levelplot(temperature.matrix[,ncol(output.temp):1], )
> to get it looking like it was on the globe due to the matrix rows 
increasing in 
> number down the matrix in its dimensions on longitude and latitude 
but the 
> y-axis coordinates increase up the axis.
> 
> Can anyone simply tell me whether this is correct as I find it very 
hard to know 
> which way up my data should be and I cannot tell which is correct 
simply by 
> looking at it!
> 
> Many thanks for your time in reading this problem,
> 
> Jenny Barnes

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


[R] cygwin script for Sweave

2006-10-13 Thread Thomas Harte
below is a very simple bash script to run Sweave from a cygwin terminal, run 
pdflatex on 
the generated .tex file, and then view the resulting .pdf output.

i usually use cygwin when i am (forced to be on) Windoze, but i found a few 
issues 
with paths that this script works around.

pdfview, used in the script, is simply:

>
$ cat /usr/local/bin/pdfview
#!/bin/bash.exe

if [ $# -eq 1 ]
then
/c/Program\ Files/Adobe/Acrobat\ 6.0/Reader/AcroRd32.exe `cygpath -w -a 
-s "$1"`
else
/c/Program\ Files/Adobe/Acrobat\ 6.0/Reader/AcroRd32.exe
fi


mutatis mutandis for your own Adobe Reader. 


here is the script:
>
#!/bin/bash.exe
# rnw.sh [.Rnw file]
#
# $1 must be a .Rnw file
#
RNWFILE=$1
PWD=`pwd`
FILEBASE=`basename $1 .Rnw`
TEXFILE=$FILEBASE.tex
PDFFILE=$FILEBASE.pdf

echo " \
library(\"utils\"); \
setwd(\"`cygpath -m $PWD`\");   \
Sweave(\"$RNWFILE\")\
  " \
  | /c/R/R-2.3.1/bin/Rterm.exe --no-save --no-restore
 
# the resulting .tex file contains an annoying "c:" ... 
# replace it with the pdflatex-friendly "/c" :
sed -e 's/c:/\/c/g' --in-place $TEXFILE

# now run text processing
pdflatex $TEXFILE
pdfview $PDFFILE &


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


Re: [R] issues with Sweave and inclusion of graphics in a document

2006-08-26 Thread Thomas Harte


--- Friedrich Leisch <[EMAIL PROTECTED]> wrote:

> >>>>> On Sat, 26 Aug 2006 05:38:58 -0700 (PDT),
> >>>>> Thomas Harte (TH) wrote:
> 
> 
>   > hallo, friedrich, and thanks for your reply.
> 
>   > if i Stangle your code i get:
> 
>   > sp<- make.ar.1(alpha=.5, n=800)
>   > plot(sp, type="l", col="blue")
> 
>   > whereas if i Stangle my code, i get:
> 
>   > width<- 20; height<- 5
>   > x11(width=width, height=height)
>   > sp<- make.ar.1(alpha=.5, n=800)
>   > plot(sp, type="l", col="blue")
>   > savePlotAsPdf("ar.pdf")
> 
>   > this, i think, is the problem in a nutshell.
> 
>   > i want my R code to look like this because i (often) want to open
>   > a separate device (for example, to have specific dimensions as
>   > above) and i may wish to save its contents. on the other hand, i
>   > may not wish to save a device's contents: i may wish to open
>   > several devices for comparison purposes and i may wish to save
>   > only certain devices, in keeping with the Sweave options
>   > echo=false and results=hide.
> 
>   > if i use your, admittedly uncomplicated, code chunk, then i am
>   > limited to seeing the output in the final document. i can't cut
>   > and paste the code chunk into an R session and see the results as
>   > i wish to see them before saving.
> 
>   > currently for each project i work on, i usually end up with a very
>   > lengthy .R file with all the code (and embedded mathematical
>   > annotation in LaTeX), and then i have to write a set of notes or a
>   > report in LaTeX duplicating or expanding on the mathematics. i'm
>   > hoping to supplant this, rather inefficient, method with a single
>   > .Rnw file and the use of Sweave. this, i believe, is what Sweave
>   > was designed to do. but i do wish to write my R code (warts and
>   > all) the way that i want to in the .Rnw file and only include the
>   > output (graphics or text) of certain parts of that code in the
>   > document. i hope that seems reasonable.
> 
>   > perhaps i was not clear, or succinct enough, in my original post
>   > (i tried to get the point across by providing example .Rnw files,
>   > but i'm aware that it's asking a lot to wade through a lengthy
>   > example to retrieve a point).
> 
> OK, no I think I understand what you want: running the *tangled* code
> should open windows with the same height/width ratio as in the Latex
> document, right? 

yes, that is correct. the goal is to develop R code in exactly the same
way as i always do (a monolithic script with lots of inroads into a 
problem, solutions i tried and rejected, helpful pointers to the 
real solution, and so on), but to have a document (.pdf, .ps, whatever)
that goes along with the monolithic script noting all the key points. 
hence, six months after writing the code i want to be able to go back, 
refresh my memory with the document and then delve into the R code
if i have to. 

maintaining both the R code and the LaTeX documentation
in one .Rnw file is the genius of your Sweave package and this is 
what i want to adopt as my standard development methodology.

> Your first posting suggested to me more the other way round ...

mea maxima culpa. 
 
> No, what you want is currently not directly possible with Sweave, you
> will have to use workarounds like the one you posted. You can even
> easily write your own tangle function doing it automatically (Rtangle
> is really trivial can be easily adjusted to ones personal needs).

thank you for the clarification. i feel a lot less awkward in using
that complicated workaround code ;)

cheers / gruss,

thomas.

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


Re: [R] issues with Sweave and inclusion of graphics in a document

2006-08-26 Thread Thomas Harte
--- Friedrich Leisch <[EMAIL PROTECTED]> wrote:

> >>>>> On Fri, 25 Aug 2006 11:05:48 -0700 (PDT),
> >>>>> Thomas Harte (TH) wrote:
>   > i can get pretty close to this in linux by writing a function to save the
>   > plot to a pdf device:
>   > <>=
>   >   # no savePlot in Linux ... so write my own function
>   >   savePlotAsPdf<- function(pdfname, from=dev.cur()) {
>   >   from<- from
>   >   pdf(pdfname, width=width, height=height)
>   >   to<- dev.cur()
>   >   dev.set(from)
>   >   dev.copy(which=to)
>   >   dev.off()
>   >   }
>   >   # a long AR process is best viewed in a wide window ... 
>   >   # width & height are now variables
>   >   width<- 20; height<- 5
>   >   x11(width=width, height=height)
>   >   sp<- make.ar.1(alpha=.5, n=800)
>   >   plot(sp, type="l", col="blue")
>   >   # width & height via dynamic scoping in savePlotAsPdf
>   >   savePlotAsPdf("ar.pdf")
>   > @
> 
> Umm, maybe I don't get your point, but in what way does the complicated
> code above do anythingdifferent from
> 
> **
> 
> <>=
> sp<- make.ar.1(alpha=.5, n=800)
> @
> 
> <>=
> plot(sp, type="l", col="blue")
> @
> 
> \begin{figure}
>   \includegraphics[width=14.5cm]{myprefix-ar}
> \end{figure}
> 
> **

hallo, friedrich, and thanks for your reply.

if i Stangle your code i get:

sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")

whereas if i Stangle my code, i get:

width<- 20; height<- 5
x11(width=width, height=height)
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
savePlotAsPdf("ar.pdf")

this, i think, is the problem in a nutshell.

i want my R code to look like this because i (often) want to open a separate
device (for example, to have specific dimensions as above) and i may wish to 
save its contents. on the other hand, i may not wish to save a device's 
contents: 
i may wish to open several devices for comparison purposes and i may wish to 
save only
certain devices, in keeping with the Sweave options echo=false and results=hide.

if i use your, admittedly uncomplicated, code chunk, then i am limited to 
seeing the
output in the final document. i can't cut and paste the code chunk into an R 
session and
see the results as i wish to see them before saving. 

currently for each project i work on, i usually end up with a very lengthy .R 
file with
all the code (and embedded mathematical annotation in LaTeX), and then i have 
to write a
set of notes or a report in LaTeX duplicating or expanding on the mathematics. 
i'm hoping
to supplant this, rather inefficient, method with a single .Rnw file and the 
use of
Sweave. this, i believe, is what Sweave was designed to do. but i do wish to 
write my R
code (warts and all) the way that i want to in the .Rnw file and only include 
the output
(graphics or text) of certain parts of that code in the document. i hope that 
seems
reasonable.

perhaps i was not clear, or succinct enough, in my original post
(i tried to get the point across by providing example .Rnw files,
but i'm aware that it's asking a lot to wade through a lengthy
example to retrieve a point).

thanks again for your reply.

cheers,

thomas.

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


Re: [R] issues with Sweave and inclusion of graphics in a document

2006-08-25 Thread Thomas Harte
--- Prof Brian Ripley <[EMAIL PROTECTED]> wrote:

> savePlot is just an internal version of dev.copy, part of the support for 
> the menus on the windows() graphics device.
> 
> It is described in `An Introduction to R' (the most basic R manual).

"the most basic R manual" doesn't quite answer my question. 

by itself, dev.copy doesn't copy the width and height of the device whereas 
savePlot
copies whatever is displayed on the screen giving me 
what-you-see-is-what-you-save
capabilities (but only under the Windows OS). 

i can get pretty close to this in linux by writing a function to save the
plot to a pdf device:
<>=
# no savePlot in Linux ... so write my own function
savePlotAsPdf<- function(pdfname, from=dev.cur()) {
from<- from
pdf(pdfname, width=width, height=height)
to<- dev.cur()
dev.set(from)
dev.copy(which=to)
dev.off()
}
# a long AR process is best viewed in a wide window ... 
# width & height are now variables
width<- 20; height<- 5
x11(width=width, height=height)
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
# width & height via dynamic scoping in savePlotAsPdf
savePlotAsPdf("ar.pdf")
@

the only (minor) inconvenience is that i have to specify width and height as 
variables to
take advantage of dynamic scoping in order to minimize mess.

while this is a workaround, via dev.copy, as you pointed out, it still doesn't 
answer why
Sweave doesn't like x11() devices (or windows() devices under the Windows OS 
for that
matter) within figure environments. 

perhaps this is a question for the package maintainer? but i was hoping that 
all the avid
Sweave users would pitch in with how they work with graphics in practice.

here is a revised .Rnw example illustrating the above:

% >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example-linux2.Rnw
\documentclass[a4paper]{article}

\begin{document}

\noindent This is an example of how I can use \texttt{Sweave} under Linux. 

<>=
# create a simple AR process:
make.ar.1<- function(alpha=1,n=300) {
Z<- rnorm(n); 
Y<- numeric(n); 
Y[1]<- Z[1]; 
for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
return(Y)
}
@

\texttt{Sweave} doesn't like the [EMAIL PROTECTED](width=width, height=height)@ 
command 
in the following code chunk if it is placed within a \texttt{figure} 
environment.
Instead, I have to save the plot to a file and then I use [EMAIL PROTECTED]@ in 
the \texttt{figure} environment. This isn't a bad thing, as it allows me to 
fine-tune
the \LaTeX\ graphics placement.
<>=
# no savePlot in Linux ... so write my own function
savePlotAsPdf<- function(pdfname, from=dev.cur()) {
from<- from
pdf(pdfname, width=width, height=height)
to<- dev.cur()
dev.set(from)
dev.copy(which=to)
dev.off()
}
# a long AR process is best viewed in a wide window ... 
# width & height are now variables
width<- 20; height<- 5
x11(width=width, height=height)
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
# width & height via dynamic scoping in savePlotAsPdf
savePlotAsPdf("ar.pdf")
@
\begin{figure}
\begin{center}
%   i retain direct control over graphics in LaTeX; i can fine-tune the 
%   the graphics placement as much as i want:
\includegraphics[width=14.5cm]{./ar.pdf}
\caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
is best viewed in a wide window.}
\end{center}
\end{figure}

Under X, then,
\begin{itemize}
\item Why doesn't \texttt{Sweave} like [EMAIL PROTECTED](width=width, 
height=height)@?
\end{itemize}
There are, however, advantages to doing things this way:
\begin{itemize}
\item I can save the plot to a file without writing any other code;
\item I can include the saved plot in my \LaTeX\ figure, allowing me to 
fine-tune with the [EMAIL PROTECTED]@ command.
\end{itemize}

\end{document}
% <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< example-linux2.Rnw




> 
> On Sat, 19 Aug 2006, Thomas Harte wrote:
> 
> > the problem is a little hard to explain; the .Rnw files (below)
> > probably do a better job, but here goes ...
> > 
> > Sweave doesn't like it when i size a graphical device in a code
> > chunk using either, e.g.:
> > 
> > windo

[R] issues with Sweave and inclusion of graphics in a document

2006-08-19 Thread Thomas Harte
the problem is a little hard to explain; the .Rnw files (below)
probably do a better job, but here goes ...

Sweave doesn't like it when i size a graphical device in a code
chunk using either, e.g.:

windows(width=20, height=5)

in Windows, or, e.g.

x11(width=20, height=5)

under X, when i then plot something in said device and try to 
include this graphical output in the resulting document.

Sweave does not object to my writing code chunks in the above
manner, so long as i do not wish to include the code in a LaTeX 
figure environment.

oftentimes i want to do precisely what Sweave doesn't appear
to allow. for example, with time-series data, i want to see a 
wide window on the screen as i code, and then i want to include 
the graphical output in my document the way that i fine tuned 
it on the screen. i don't want to write two pieces of code:
the first, to view output on the sceen; the second, to save
the output to a .pdf file for inclusion in the document.

some example .Rnw files should illustrate my plight.
suggestions on a workaround (i.e. how to do what i describe in 
linux/X) welcome.


% >> example-windows.Rnw
\documentclass[a4paper]{article}

\begin{document}

\noindent This is an example of what I can do on Windows. Unhappily, I seem to 
be
able to squeeze marginally more out of \texttt{Sweave} \emph{chez\/} Bill Gates
than I can under Linux. Ho, hum.

<>=
# create a simple AR process:
make.ar.1<- function(alpha=1,n=300) {
Z<- rnorm(n); 
Y<- numeric(n); 
Y[1]<- Z[1]; 
for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
return(Y)
}
@

<>=
# a long AR process is best viewed in a wide window:
windows(width=20, height=5)
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
# WISIWIS: What I See Is What I Save ;)
savePlot("ar",type="pdf")
@
\begin{figure}
\begin{center}
%   imporantly, by saving the plot i have direct control over graphics in 
LaTeX, 
%   and i can fine-tune the the graphics placement as much as i want:
\includegraphics[width=14.5cm]{./ar.pdf}
\caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
is best viewed in a wide window.}
\end{center}
\end{figure}


\noindent Had I tried to do the following, \texttt{Sweave} would have blown up!
\begin{verbatim}
<>=
windows(width=20, height=5) # <- this is the offending 
command:
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
@
\begin{figure}
\begin{center}
<>=
<>
@
\caption{An AR(1) process of length~\protect\Sexpr{length(sp)} 
is best viewed in a wide window.}
\end{center}
\end{figure}
\end{verbatim}


\noindent The take-home message is that \texttt{savePlot} saves the day under 
Windows.
As far as I know, there is no equivalent under Linux, or rather, under X.

In Windows, then,
\begin{itemize}
\item I can plot the way I want on the screen;
\item I can save that plot to a file without writing any other code;
\item I can include the saved plot in my \LaTeX\ figure, allowing me to 
fine-tune with the [EMAIL PROTECTED]@ command.
\end{itemize}
Strike one for the Evil Empire.

\end{document}
% << example-windows.Rnw



% >> example-linux.Rnw
\documentclass[a4paper]{article}

\begin{document}

\noindent This is an example of the hapless state of my \texttt{Sweave}ing 
under Linux. 

<>=
# create a simple AR process:
make.ar.1<- function(alpha=1,n=300) {
Z<- rnorm(n); 
Y<- numeric(n); 
Y[1]<- Z[1]; 
for (i in 2:n) Y[i]<- alpha*Y[i-1]+Z[i]; 
return(Y)
}
@

\noindent Because of the [EMAIL PROTECTED](width=20, height=5)@ command, 
I can't embed the graphical output that the following piece of code 
produces in my document, although I can view the results on screen:
<>=
# a long AR process is best viewed in a wide window:
x11(width=20, height=5)
sp<- make.ar.1(alpha=.5, n=800)
plot(sp, type="l", col="blue")
# no savePlot ... can't seem to do anything with this plot
# if i try to include this code in a figure environment then
# Sweave blows up
# so i have to stop here :(
@

\noindent Instead, I have to do something like the following, which has the 
unfortunate 
side effects of disallowing me from seeing the graphical output on the screen, 
and,
probably
more importantly, of duplicating the above code:
<>=
sp<- make.ar.1(alpha=.5, n=800)
pdf("ar.pdf", width=20, height=5)
plot(sp, type="l", col="blue")
dev.off()
@
\begin{figure}
\begin{center}
%   at least i still retain direct control over graphics in LaTeX; i can 
fin

Re: [R] copying previously installed libraries to R 2.3.0

2006-04-26 Thread Thomas Harte
the windoze faq that you refer to doesn't quite address the question that i 
asked, but thanks all the same.

2.8 What's the best way to upgrade?  That's a matter of taste.  For most people 
the best thing to do is to uninstall R (see the previous Q), install the new 
version, copy any installed packages to the library folder in the new 
installation, run update.packages() in the new R (`Update packages...' from the 
Packages menu, if you prefer) and then delete anything left of the old 
installation.  Different versions of R are quite deliberately installed in 
parallel folders so you can keep old versions around if you wish.  
Upgrading from R 1.x.y to R 2.x.y is special as all the packages need to be 
reinstalled.  Rather than copy them across, make a note of their names and 
re-install them from CRAN.  


Christos Hatzis <[EMAIL PROTECTED]> wrote: See Windows FAQ 2.8 - works well.

-Christos
 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Thomas Harte
Sent: Wednesday, April 26, 2006 2:54 PM
To: r-help@stat.math.ethz.ch
Subject: [R] copying previously installed libraries to R 2.3.0

hi all,

is there a new mechanism in R 2.3.0 for copying libraries from, say, R 2.2.1
to R 2.3.0? i ask because gabor grothendieck comments in his copydir.bat
(from gabor's batchfiles at:
http://cran.r-project.org/contrib/extra/batchfiles/batchfiles_0.2-5.zip ):

``::   I personally upgraded my 2.1.0 to 2.2.0 this way so it seems ok until
  ::   R replaces this with something better which is expected for 2.3.0.
'''

see also the posting below.

cheers,

thomas.
 

[R] copy contributed packages from R 2.2.0 to 2.2.1
This message: [ Message body ]  [ More options ] 
Related messages:  [ Next message ] [ Previous message ] [ In reply to ]
[ Next in thread ]  
  
 From: Ronnie Babigumira 
Date: Fri, 23 Dec 2005 15:58:36 +0100
Hi Helli, this came up last week, Here are some of the replys posted 
 
1. 
 In http://cran.r-project.org/contrib/extra/batchfiles/batchfiles_0.2-5.zip 
 
are two Windows XP batch files: 
 
movedir.bat
 copydir.bat 
 
which will move the packages (which is much faster and suitable if you don't
need the old version of R any more) or copy  the packages (which takes
longer but preserves the old version). 
 
2. 
 x <- installed.packages()[,1]
 install.packages(x) 
 
3. 
 This is one reason we normally recommend that you install into a separate
library.  Then update.packages(checkBuilt =
 TRUE) is all that is needed. However, 
 
foo <- installed.packages()
 as.vector(foo[is.na(foo[, "Priority"]), 1]) 
 
will give you a character vector which you can feed to install.packages(),
so it's not complex to do manually. 
 
4. 
 If the previous installation is still alive, fire it up and 
 
pS <- packageStatus()
 pkgs <- pS$inst$Package[!pS$inst$Priority %in% c("base", "recommended")]
save(pkgs, file = "foo") 
 
In the new installation, 
 
load("foo")
 install.packages(pkgs) 
 
Helmut Kudrnovsky wrote: 
> hi R-users,
>  
> a few days ago R 2.2.1 came out. on my win xp i'installed R 2.2.0. along
the time i've installed a lot of contributed packages. my
internet-connection is not very fast. 
>  
> so my question: is it possible after installing R 2.2.1 to do copy/paste
the contributed packages from the C:\Programme\R221 to the
C:\Programme\R2.2.1- location in the files system? 
>  
> or have i to download and install the packages new? 
>  
>  
> greetings from the snowy austria
> merry christmas
> helli
>  
> system
> R.2.2.0
> win xp
>  
> __
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
Received on Fri Dec 23 2005 - 15:58:36 EST


   This message: [ Message body ]
   Next message: Matthias Kohl: "[R] convolution of the double exponential
distribution"
   Previous message: Helmut Kudrnovsky: "[R] copy contributed packages from
R 2.2.0 to 2.2.1"
   In reply to: Helmut Kudrnovsky: "[R] copy contributed packages from R
2.2.0 to 2.2.1"
   Next in thread: Uwe Ligges: "[R] copy contributed packages from R 2.2.0
to 2.2.1"
 
   Contemporary messages sorted: [ by date ] [ by thread ] [ by subject ] [
by author ] [ by messages with attachments ]
  
   This archive was generated by hypermail 2.2.0  : Sat Dec 31 2005 -
19:09:32 EST 

 [[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] copying previously installed libraries to R 2.3.0

2006-04-26 Thread Thomas Harte
hi all,

is there a new mechanism in R 2.3.0 for copying libraries from, say, R 2.2.1 to 
R 2.3.0? i ask because gabor grothendieck comments in his copydir.bat (from 
gabor's batchfiles at: 
http://cran.r-project.org/contrib/extra/batchfiles/batchfiles_0.2-5.zip ):

``::   I personally upgraded my 2.1.0 to 2.2.0 this way so it seems ok until
  ::   R replaces this with something better which is expected for 2.3.0. '''

see also the posting below.

cheers,

thomas.
 

[R] copy contributed packages from R 2.2.0 to 2.2.1
This message: [ Message body ]  [ More options ] 
Related messages:  [ Next message ] [ Previous message ] [ In reply to ]  [ 
Next in thread ]  
  
 From: Ronnie Babigumira  
Date: Fri, 23 Dec 2005 15:58:36 +0100
Hi Helli, this came up last week, Here are some of the replys posted 
 
1. 
 In http://cran.r-project.org/contrib/extra/batchfiles/batchfiles_0.2-5.zip 
 
are two Windows XP batch files: 
 
movedir.bat 
 copydir.bat 
 
which will move the packages (which is much faster and suitable if you don't 
need the old version of R any more) or copy  
 the packages (which takes longer but preserves the old version). 
 
2. 
 x <- installed.packages()[,1] 
 install.packages(x) 
 
3. 
 This is one reason we normally recommend that you install into a separate 
library.  Then update.packages(checkBuilt =  
 TRUE) is all that is needed. However, 
 
foo <- installed.packages() 
 as.vector(foo[is.na(foo[, "Priority"]), 1]) 
 
will give you a character vector which you can feed to install.packages(), so 
it's not complex to do manually. 
 
4. 
 If the previous installation is still alive, fire it up and 
 
pS <- packageStatus() 
 pkgs <- pS$inst$Package[!pS$inst$Priority %in% c("base", "recommended")] 
 save(pkgs, file = "foo") 
 
In the new installation, 
 
load("foo") 
 install.packages(pkgs) 
 
Helmut Kudrnovsky wrote: 
> hi R-users, 
>  
> a few days ago R 2.2.1 came out. on my win xp i'installed R 2.2.0. along the 
> time i've installed a lot of contributed packages. my internet-connection is 
> not very fast. 
>  
> so my question: is it possible after installing R 2.2.1 to do copy/paste the 
> contributed packages from the C:\Programme\R221 to the C:\Programme\R2.2.1- 
> location in the files system? 
>  
> or have i to download and install the packages new? 
>  
>  
> greetings from the snowy austria 
> merry christmas 
> helli 
>  
> system 
> R.2.2.0 
> win xp 
>  
> __ 
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help 
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html 
> 
Received on Fri Dec 23 2005 - 15:58:36 EST


   This message: [ Message body ]
   Next message: Matthias Kohl: "[R] convolution of the double exponential 
distribution"
   Previous message: Helmut Kudrnovsky: "[R] copy contributed packages from R 
2.2.0 to 2.2.1"
   In reply to: Helmut Kudrnovsky: "[R] copy contributed packages from R 2.2.0 
to 2.2.1"
   Next in thread: Uwe Ligges: "[R] copy contributed packages from R 2.2.0 to 
2.2.1"
 
   Contemporary messages sorted: [ by date ] [ by thread ] [ by subject ] [ by 
author ] [ by messages with attachments ]
  
   This archive was generated by hypermail 2.2.0  : Sat Dec 31 2005 - 19:09:32 
EST 

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] gray level values

2006-03-23 Thread Thomas Harte
# clear everything:
rm(list=ls(all=TRUE))
detach()
graphics.off()

# make a test matrix:
( m<- matrix((1:12)/12, 3, 4) )
library(gplots)
# display the test matrix:
image(m, col=colorpanel(12, "darkblue", "yellow", "white"), axes=FALSE)

# here's the action part (no index checking, lame, illustrative code only, 
with advance apologies, &c.):
image.ij<- function(i,j) return( t(m)[dim(t(m))[1]:1,][i,j] )

# we're seeing a "1" in the top right-hand corner of the image at 
coordinates:
i<- 1
j<- 3
image.ij(i,j)
# cf. what's in the matrix at those coordinates:
m[i,j]

> Hello.
> 
> 
> I have a matrix whit n1 rows and n2 columns called example.
> If I do image(example), R shows me an image with 480x480 pixels.
> 
> How can I obtain the gray level of the pixel of row i and column j?
> 
> Thanks,
> 
> Arnau.




[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html