[R] Loop avoidance in simulating a vector

2008-10-16 Thread David Afshartous


All, 

I'd like to simulate a vector that is formed from many distinct
distributions and avoid a loop if possible.  E.g, consider:

mu = c(1, 2, 3)
sigma = c(1, 2, 3)
n = c(10, 10, 10)

And we simulate a vector of length 30 that consists of N(mu[i], sigma[i])
distributed data, each of length n[i].   Of course for just three groups we
can simply write it out as:

DV = c(rnorm(n[1], mu[1], sigma[1]), rnorm(n[2], mu[2], sigma[2]),
rnorm(n[3], mu[3], sigma[3]) )

For many groups we can use a loop (assuming equal numbers per group):

n = n[1]
DV = numeric(N*n)
for (i in 1:N) {
DV[(n*i - (n-1)): (n*i)] = rnorm(n, mu[i], sigma[i])
}

Is there any way to do the general cas without using a loop?

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


[R] ldBands (Hmisc)

2008-10-13 Thread David Afshartous

All,

I'm getting the same error message as that discussed in a previous post (Feb
3, 2006).  The reply to that post was to insure that the ld98 program was in
the system path (as also suggested in the help on ldBands).  I have done
this but this does not change the result.  Any advice much appreciated.

David



> sessionInfo()
R version 2.7.2 (2008-08-25)
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] Hmisc_3.4-3

loaded via a namespace (and not attached):
[1] cluster_1.11.11 grid_2.7.2  lattice_0.17-13 tools_2.7.2

## run example from help on ldBands
> b <- ldBands(5, pr=FALSE)
Error in (head + 1):length(w) : NA/NaN argument


## my Path variable is specified as follows on Windows XP, with the ld98
sitting in the WinLD directory:
C:\Program Files\MiKTeX
2.6\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
;C:\Program Files\WinLD

__
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] Binom.test, vector input

2008-10-09 Thread David Afshartous

All,

Is it possible to use binom.test with vector input for only one of the
arguments?  I was thinking that this would possibly work with sapply but
then it seems that the binom.test function would have to be re-written to
supply defaults for all other arguments.

set.seed(101)
sim.x = rbinom(100, 10, .8)  ## generate 100 values distributed B(10, .8)

# test of first sample against H.0: p=.6
binom.test(sim.x[1], n = 10, p = .6, alternative = "greater")

Is it possible to do the same test for all the rest without a loop or
re-writing the binom.test function?

Cheers,
David


> sessionInfo()
R version 2.7.1 (2008-06-23)
i386-apple-darwin8.10.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base
>

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


[R] Using sub to get captions in barplots

2008-09-30 Thread David Afshartous

All,

I've been using "sub" (subtitle) instead of "main" such that captions are
below figures produced by xyplot.  This works fine and captions are on a
single line.  However, when I try this for bar plots with error bars
(altering the error.bars function form Crawley's The R Book, see below), the
captions are split on more than 1 line.  Is there a way to get the caption
on a single line? 

Cheers,
David




y.bar.new = c(30, 15)
se.ybar.new = c(2,3)
error.bars(y.bar.new, se.ybar.new, c("Control (n=18)", "CKD (n=18)"))


error.bars<-function(yv,z,nn){
xv<-barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab="Total five hour
potassium excretion (mmol)", sub= "Figure 1B: Hour
1-5 potassium excretion")
g=(max(xv)-min(xv))/50
for (i in 1:length(xv)) {
lines(c(xv[i],xv[i]),c(yv[i]+z[i],yv[i]-z[i]))
lines(c(xv[i]-g,xv[i]+g),c(yv[i]+z[i], yv[i]+z[i]))
lines(c(xv[i]-g,xv[i]+g),c(yv[i]-z[i], yv[i]-z[i]))
}}

> sessionInfo()
R version 2.7.1 (2008-06-23)
i386-apple-darwin8.10.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid  stats graphics  grDevices utils datasets  methods
base 

other attached packages:
[1] lattice_0.17-8 nlme_3.1-89

loaded via a namespace (and not attached):
[1] Matrix_0.999375-11 lme4_0.999375-24   tools_2.7.1

__
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] plot of all.effects object

2008-09-11 Thread David Afshartous

Dear John,

Thanks and sorry for the typo.

For the example below, how do I get the time.num variable to correspond to
the x-axis?  I tried refitting the model with a different order of supplied
variables but this didn't do it.

Cheers,
David




On 9/11/08 4:28 PM, "John Fox" <[EMAIL PROTECTED]> wrote:

> Dear David,
> 
> You have to spell the name of term correctly:
> 
>  plot(fm.effects, "time.num:drug:X")
> 
> (Admittedly, the error message is cryptic: I'll look into that.)
> 
> A couple of other comments: (1) There is only one high-order term in
> your model, so it's not necessary to use all.effects(); (2) if you
> plot(fm.effects) (i.e., without specifying the term to plot) you'll be
> presented with a menu, in this instance with only one choice.
> 
> I hope this helps,
>  John
> 
> On Thu, 11 Sep 2008 15:54:25 -0400
>  David Afshartous <[EMAIL PROTECTED]> wrote:
>> 
>> 
>> All, 
>> 
>> I'm trying to plot an all.effects() object, as shown in the help for
>> all.effects and also Crawley's R book (p.178, 2007). The data has a
>> repeated
>> measures structure, but I'm using all.effects for the simple lm() fit
>> here.
>> Below is a reproducible example that yields the error message.
>> 
>> 
>> fm.ex = lm(dv ~ time.num*drug*X, data = dat.new)
>> fm.effects = all.effects(fm.ex, xlevels = list(time.num = 1:4))
>> 
>>> plot(fm.effects, "time.num:Drug:X")
>> Error in plot.window(...) : need finite 'xlim' values
>> In addition: Warning messages:
>> 1: In min(x) : no non-missing arguments to min; returning Inf
>> 2: In max(x) : no non-missing arguments to max; returning -Inf
>> 3: In min(x) : no non-missing arguments to min; returning Inf
>> 4: In max(x) : no non-missing arguments to max; returning -Inf
>> 
>> Cheers,
>> David
>> 
>> 
>> 
>> 
>> 
>> 
>>  sessionInfo()
>> R version 2.7.1 (2008-06-23)
>> i386-apple-darwin8.10.1
>> 
>> locale:
>> en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
>> 
>> attached base packages:
>> [1] grid  stats graphics  grDevices utils datasets
>>  methods
>> base 
>> 
>> other attached packages:
>> [1] effects_1.0-12 lattice_0.17-8
>> 
>> loaded via a namespace (and not attached):
>> [1] Matrix_0.999375-11 lme4_0.999375-24   nlme_3.1-89
>>tools_2.7.
>> 
>> set.seed(500)
>> n.timepoints <- 4
>> n.subj.per.tx <- 20
>> sd.d <- 5;
>> sd.p <- 2;
>> sd.res <- 1.3
>> drug <- factor(rep(c("D", "P"), each = n.timepoints, times =
>> n.subj.per.tx))
>> drug.baseline <- rep( c(0,5), each=n.timepoints, times=n.subj.per.tx
>> )
>> Patient <- rep(1:(n.subj.per.tx*2), each = n.timepoints)
>> Patient.baseline <- rep( rnorm( n.subj.per.tx*2, sd=c(sd.d, sd.p) ),
>> each=n.timepoints )
>> time <- factor(paste("Time-", rep(1:n.timepoints, n.subj.per.tx*2),
>> sep=""))
>> time.baseline <-
>> rep(1:n.timepoints,n.subj.per.tx*2)*as.numeric(drug=="D")
>> dv <- rnorm( n.subj.per.tx*n.timepoints*2,
>> mean=time.baseline+Patient.baseline+drug.baseline, sd=sd.res )
>> dat.new <- data.frame(time, drug, dv, Patient)
>> dat.new$time.num = rep(1:n.timepoints, n.subj.per.tx*2)
>> dat.new$X <- rnorm(160) ### to check plot of all.effects info
>> 
>> __
>> 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.
> 
> 
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada
> http://socserv.mcmaster.ca/jfox/

__
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] plot of all.effects object

2008-09-11 Thread David Afshartous


All, 

I'm trying to plot an all.effects() object, as shown in the help for
all.effects and also Crawley's R book (p.178, 2007). The data has a repeated
measures structure, but I'm using all.effects for the simple lm() fit here.
Below is a reproducible example that yields the error message.


fm.ex = lm(dv ~ time.num*drug*X, data = dat.new)
fm.effects = all.effects(fm.ex, xlevels = list(time.num = 1:4))

> plot(fm.effects, "time.num:Drug:X")
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

Cheers,
David






 sessionInfo()
R version 2.7.1 (2008-06-23)
i386-apple-darwin8.10.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid  stats graphics  grDevices utils datasets  methods
base 

other attached packages:
[1] effects_1.0-12 lattice_0.17-8

loaded via a namespace (and not attached):
[1] Matrix_0.999375-11 lme4_0.999375-24   nlme_3.1-89tools_2.7.

set.seed(500)
n.timepoints <- 4  
n.subj.per.tx <- 20
sd.d <- 5;
sd.p <- 2;
sd.res <- 1.3
drug <- factor(rep(c("D", "P"), each = n.timepoints, times = n.subj.per.tx))
drug.baseline <- rep( c(0,5), each=n.timepoints, times=n.subj.per.tx )
Patient <- rep(1:(n.subj.per.tx*2), each = n.timepoints)
Patient.baseline <- rep( rnorm( n.subj.per.tx*2, sd=c(sd.d, sd.p) ),
each=n.timepoints )
time <- factor(paste("Time-", rep(1:n.timepoints, n.subj.per.tx*2), sep=""))
time.baseline <- rep(1:n.timepoints,n.subj.per.tx*2)*as.numeric(drug=="D")
dv <- rnorm( n.subj.per.tx*n.timepoints*2,
mean=time.baseline+Patient.baseline+drug.baseline, sd=sd.res )
dat.new <- data.frame(time, drug, dv, Patient)
dat.new$time.num = rep(1:n.timepoints, n.subj.per.tx*2)
dat.new$X <- rnorm(160) ### to check plot of all.effects info

__
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] Significant digits for checking values of variable?

2008-08-27 Thread David Afshartous

Thanks.  Is there simple way around this for simple checking of single
values?  The all.equal() mentioned in the FAQ doesn't seem appropriate.

 X = c(1.2, 2)
> X.new = X -1
> X == 1.2
[1]  TRUE FALSE
> X.new == .2
[1] FALSE FALSE



On 8/27/08 11:47 AM, "jim holtman" <[EMAIL PROTECTED]> wrote:

> FAQ 7.31
> 
> On Wed, Aug 27, 2008 at 11:51 AM, David Afshartous
> <[EMAIL PROTECTED]> wrote:
>> 
>> All,
>> 
>> I'm witnessing some strange behavior when checking the values of one of my
>> variables.  My guess is that it has something to do with the number of
>> significant digits being represented, but perhaps not as my variable was
>> created from other variables that only had one decimal place.  See below.
>> I'm sure this is something basic, any suggestions much appreciated.
>> 
>> Cheers,
>> David
>> 
>> 
>>> visit.2.SEK.delta.baseline
>>  [1]  0.1 -0.7  0.8 -0.1 -0.5  0.8  0.7  0.2  0.4  0.3  0.6  0.6  0.3  0.8
>> 0.8  0.3  0.4  0.8
>>> visit.2.SEK.delta.baseline > 0
>>  [1]  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
>> TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
>>> visit.2.SEK.delta.baseline ==  .8
>>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>> FALSE FALSE FALSE FALSE FALSE FALSE
>> ### but some of the values are equal to .8
>> 
>> ## here it is okay when I try to reproduce
>>>  junk = c(0.1, -0.7,0.8, -0.1, -0.5,  0.8,  0.7,  0.2,  0.4,  0.3,  0.6,
>>> 0.6,
>> 0.3,  0.8,  0.8,  0.3,  0.4,  0.8 )
>>> junk == .8
>>  [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
>> FALSE  TRUE  TRUE FALSE FALSE  TRUE
>> 
>> __
>> 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] Significant digits for checking values of variable?

2008-08-27 Thread David Afshartous

All,

I'm witnessing some strange behavior when checking the values of one of my
variables.  My guess is that it has something to do with the number of
significant digits being represented, but perhaps not as my variable was
created from other variables that only had one decimal place.  See below.
I'm sure this is something basic, any suggestions much appreciated.

Cheers,
David


> visit.2.SEK.delta.baseline
 [1]  0.1 -0.7  0.8 -0.1 -0.5  0.8  0.7  0.2  0.4  0.3  0.6  0.6  0.3  0.8
0.8  0.3  0.4  0.8
> visit.2.SEK.delta.baseline > 0
 [1]  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
> visit.2.SEK.delta.baseline ==  .8
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE FALSE
### but some of the values are equal to .8

## here it is okay when I try to reproduce
>  junk = c(0.1, -0.7,0.8, -0.1, -0.5,  0.8,  0.7,  0.2,  0.4,  0.3,  0.6,  0.6,
0.3,  0.8,  0.8,  0.3,  0.4,  0.8 )
> junk == .8
 [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE  TRUE  TRUE FALSE FALSE  TRUE

__
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] Axes in perspective plot - persp()

2008-07-30 Thread David Afshartous


All,

I'm making some plots with the persp() function and am having trouble
sorting out the following:

- How does one avoid the axes label overlapping the tick values?

- Is doesn't seem possible to independently control the number of ticks of
the x,y, and z-axes, e.g., I'd like say only 4 ticks on the y axes but 10 on
the other two.

- for my function (although it doens't happen for the reproducible example
below unfortunately) the origin of the x and y tick values seem to overlap,
is there any way to avoid this?

Example below.

theta <- seq(.1, 10, length = 30)
rho <- seq(0, .99, length = 30)
f <- function(theta, rho) { theta + rho^2 }
Z <- outer(theta, rho, f)
persp(theta, rho, Z,  expand = 0.5, col = "lightblue", ticktype =
"detailed",  nticks = 10)

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.


[R] Pairs() function: selective changing of ylim; use of key

2008-07-25 Thread David Afshartous

All,

Two questions RE scatterplot matrices produced via pairs() function:

1) Is it possible to selectively change the ylim of one of the subplots?

2) Is a key allowed? I don't seem to be able to insert a manual key.

Code below:

dat = data.frame(Hour= rep(c(0:3), 4), Y1 = rnorm(16,1),
Y2 = rnorm(16,4),Drug = rep(c("P", "D"), each=4, 2) )

## this works:
pairs(dat, pch = 21, bg = c("red", "green3")[unclass(dat$Drug)])

## this produces several warnings:
pairs(dat, pch = 21, bg = c("red", "green3")[unclass(dat$Drug)],
key = list(space = "top",  text = levels(dat$Drug), lines =
list(col=c("red", "green3")), columns=2)  )

Cheers,
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] Auto.key colors maintained when subsetting

2008-07-09 Thread David Afshartous

I've determined that the problem with getting the subsetting to work with
the error bars was related to not dropping the unused levels of the Grouping
factor.  The simplest solution was to follow a combination of Deepayan's
suggestions 1 and 2 below, i.e., supply one's own colors and subset
beforehand:

## full data w/ error bars, this works:
xyplot(Y ~ Hour,
data = dat, pch = 16,
groups=Group, col=c('red', 'black', 'green', 'blue'),
ly = dat$lower.bound ,
uy = dat$upper.bound ,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = levels(dat$Group),
 points = FALSE, lines = TRUE, columns=4),
par.settings = list(superpose.line = list(lty = c(1,2,3,4),
 col=c('red','black', 'green', 'blue')
) ) )

dev.new()
junk = dat[dat$Group != "A", ]
junk$Group = junk$Group[1:length(junk$Group), drop = T]

## this works below, but one has to be careful w/ the line
## types and colors in par.settings to make sure they match
## previous figure

xyplot(Y ~ Hour,
data = junk, pch = 16,
groups=Group, col=c('black', 'green', 'blue'),
ly = junk$lower.bound ,
uy = junk$upper.bound ,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = levels(junk$Group),
 points = FALSE, lines = TRUE, columns=3),
par.settings = list(superpose.line = list(lty = c(2,3,4),
 col=c('black', 'green', 'blue')
) ) )





On 7/9/08 3:13 PM, "David Afshartous" <[EMAIL PROTECTED]> wrote:

> 
> 
> 
> On 7/9/08 1:07 PM, "Deepayan Sarkar" <[EMAIL PROTECTED]> wrote:
> 
>> On 7/9/08, David Afshartous <[EMAIL PROTECTED]> wrote:
>>> 
>>> 
>>>  All,
>>> 
>>>  I'm plotting points and lines for various groups.
>>>  I'd like subsequent plots done on subsets to maintain the color assignments
>>>  from the original plot.  This works fine, but the key for the subset
>>> doesn't
>>>  maintain the correspondence.  One solution is to reprint the entire key,
>>> but
>>>  this is undesirable since then the key has more elements than the groups in
>>>  the plot.  Reproducible example below.
>> 
>> Well, the ideal solution would have been for auto.key to magically
>> omit the levels of 'groups' that are omitted by the application of
>> 'subset', but there is not enough information available to it for this
>> to happen. One option is to
>> 
>> 1. subset the data beforehand, and drop unsed levels with Group[drop=TRUE]
>> 
> 
> This will work, but unfortunately the color correspondence across plots will
> be lost.  Leading to a preference for your second suggestion:
> 
> 
>> 2. supply colors, etc. explicitly through 'par.settings'.
>> 
> 
> The code below works, but when extended to include error bars (panel.ci, and
> prepanel.ci), it works for the full data but not the subset.
> 
> ## full data w/ colors supplied, this works:
> xyplot(Y ~ Hour,
> 
data = dat, pch = 16,
> 
groups=Group, col=c('red', 'black', 'green', 'blue'),
> 
type="b",
> 
auto.key = list(space = "top",  text = levels(Group),
>  points = FALSE, lines = TRUE, columns=4),
> 
par.settings = list(superpose.line = list(lty = c(1,2,3,4),
>  col=c('red', 'black', 'green', 'blue')
) ) )
> 
> 
> ## subset of data; this works:
> dev.new() xyplot(Y ~ Hour,
> 
data = dat, pch = 16,
> 
subset = (Group != "A"),
> 
groups=Group, col=c('red', 'black', 'green', 'blue'),
>  lty = c(1,2,3,4),
> 
type="b",
> 
auto.key = list(space = "top",  text = levels(Group)[2:4],
>  points = FALSE, lines = TRUE, columns=3),
> 
par.settings = list(superpose.line = list(lty = c(2,3,4),
>  col=c('black', 'green', 'blue')
) ) )
> 
> 
> ## full data w/ error bars, this works:
> xyplot(Y ~ Hour,
> 
data = dat, pch = 16,
> 
groups=Group, col=c('red', 'black', 'green', 'blue'),
>  lty = c(1,2,3,4),
> 
ly = dat$lower.bound ,
>

Re: [R] Auto.key colors maintained when subsetting

2008-07-09 Thread David Afshartous



On 7/9/08 1:07 PM, "Deepayan Sarkar" <[EMAIL PROTECTED]> wrote:

> On 7/9/08, David Afshartous <[EMAIL PROTECTED]> wrote:
>> 
>> 
>>  All,
>> 
>>  I'm plotting points and lines for various groups.
>>  I'd like subsequent plots done on subsets to maintain the color assignments
>>  from the original plot.  This works fine, but the key for the subset doesn't
>>  maintain the correspondence.  One solution is to reprint the entire key, but
>>  this is undesirable since then the key has more elements than the groups in
>>  the plot.  Reproducible example below.
> 
> Well, the ideal solution would have been for auto.key to magically
> omit the levels of 'groups' that are omitted by the application of
> 'subset', but there is not enough information available to it for this
> to happen. One option is to
> 
> 1. subset the data beforehand, and drop unsed levels with Group[drop=TRUE]
> 

This will work, but unfortunately the color correspondence across plots will
be lost.  Leading to a preference for your second suggestion:


> 2. supply colors, etc. explicitly through 'par.settings'.
> 

The code below works, but when extended to include error bars (panel.ci, and
prepanel.ci), it works for the full data but not the subset.

## full data w/ colors supplied, this works:
xyplot(Y ~ Hour,
data = dat, pch = 16,
groups=Group, col=c('red', 'black', 'green', 'blue'),
type="b",
auto.key = list(space = "top",  text = levels(Group),
 points = FALSE, lines = TRUE, columns=4),
par.settings = list(superpose.line = list(lty = c(1,2,3,4),
 col=c('red', 'black', 'green', 'blue')
) ) )


## subset of data; this works:
dev.new() xyplot(Y ~ Hour,
data = dat, pch = 16,
subset = (Group != "A"),
groups=Group, col=c('red', 'black', 'green', 'blue'),
 lty = c(1,2,3,4),
type="b",
auto.key = list(space = "top",  text = levels(Group)[2:4],
 points = FALSE, lines = TRUE, columns=3),
par.settings = list(superpose.line = list(lty = c(2,3,4),
 col=c('black', 'green', 'blue')
) ) )


## full data w/ error bars, this works:
xyplot(Y ~ Hour,
data = dat, pch = 16,
groups=Group, col=c('red', 'black', 'green', 'blue'),
 lty = c(1,2,3,4),
ly = dat$lower.bound ,
uy = dat$upper.bound ,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = levels(Group),
 points = FALSE, lines = TRUE, columns=4),
par.settings = list(superpose.line = list(lty = c(1,2,3,4),
 col=c('red','black', 'green', 'blue')
) ) )


## subset of data w/ error bars produces error message: Error using packet
## 1, Invalid Line Type:

xyplot(Y ~ Hour,
data = dat, pch = 16,
subset = (Group != "A"),
groups=Group, col=c('red', 'black', 'green', 'blue'),
 lty = c(1,2,3,4),
ly = dat$lower.bound ,
uy = dat$upper.bound ,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = levels(Group)[2:4],
 points = FALSE, lines = TRUE, columns=3),
par.settings = list(superpose.line = list(lty = c(2,3,4),
 col=c('black', 'green', 'blue')
) ) )

> The other option is to construct your own key (i.e., use the 'key'
> argument, not 'auto.key', to specify the legend).

Below is an attempt to use key; although the plot is correct, in the key the
color is applied to the text instead of the line and the line type isn't
drawn:

xyplot(Y ~ Hour,
data = dat, pch = 16,
subset = (Group != "A"),
groups=Group, col=c('red', 'black', 'green', 'blue'),
type="b",
key = simpleKey(text = levels(Group)[2:4], lty = c(2,3,4),
 space = "top", columns = 3, points = FALSE, lines = TRUE,
 col = c('black', 'green', 'blue'))
par.settings = list(superpose.line = list(lty = c(1,2,3,4),
 col=c('red', 'black', 'green', 'blue')
) ) )


#
Data and necessary functions:
set.seed(101)
Y = c(rnorm(4,0), rnorm(4,2), rnorm(4,6), rnorm(4,8

[R] Auto.key colors maintained when subsetting

2008-07-09 Thread David Afshartous


All,

I'm plotting points and lines for various groups.
I'd like subsequent plots done on subsets to maintain the color assignments
from the original plot.  This works fine, but the key for the subset doesn't
maintain the correspondence.  One solution is to reprint the entire key, but
this is undesirable since then the key has more elements than the groups in
the plot.  Reproducible example below.

Cheers,
David




dat = data.frame(
Y = c(rnorm(4,0), rnorm(4,2), rnorm(4,6), rnorm(4,8)),
Group = factor(c(rep("A", 4), rep("B", 4), rep("C", 4), rep("D", 4))),
Hour = rep(c(1:4), 4)
)


## plots colors and key correctly:
xyplot(Y ~ Hour, 
data = dat, pch = 16,
groups=Group,
type="b",
auto.key = list(space = "top",  text = levels(Group), points =
FALSE, lines = TRUE, columns=4),
par.settings = list(superpose.line = list(lty = c(1,2,3,4)
) ) )  



dev.new()

## does not plot colors and key correctly:
xyplot(Y ~ Hour,
data = dat, pch = 16,
subset = (Group != "A"),
groups=Group,
type="b",
auto.key = list(space = "top",  text = levels(Group)[2:4], points =
FALSE, lines = TRUE, columns=3),
par.settings = list(superpose.line = list(lty = c(1,2,3,4)
) ) )

__
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] aggregate() function and na.rm = TRUE

2008-07-08 Thread David Afshartous


All,

I've been using aggregate() to compute means and standard deviations at
time/treatment combinations for a longitudinal dataset, using na.rm = TRUE
for missing data. 

This was working fine before, but now when I re-run some old code it isn't.
I've backtracked my steps and can't seem to find out why it was working
before but not now.  In any event, below is a reproducible example of the
current problem, viz., calculating the standard deviation via aggregate and
employing na.rm = TRUE is not working.

Thanks,
David






dat = data.frame( Hour = c(0, 0, 0, 0, 1, 1,1, 1), Drug = factor(c("P", "D",
"P", "D", "P", "D", "P", "D")), Y1 = rnorm(8, 0),
Y2 = c(NA, NA, NA, NA, 1, 2, 3, 4) )

> aggregate(dat[c(3,4)], dat[c(1,2)], mean)
  Hour Drug  Y1 Y2
10D -0.75534554 NA
21D  0.27529835  3
30P -0.03949923 NA
41P  0.02627489  2
> aggregate(dat[c(3,4)], dat[c(1,2)], sd)
Error in var(x, na.rm = na.rm) : missing observations in cov/cor
> aggregate(dat[c(3,4)], dat[c(1,2)], sd, na.rm = TRUE)
Error in var(x, na.rm = na.rm) : no complete element pairs


> sessionInfo()
R version 2.7.1 (2008-06-23)
i386-apple-darwin8.10.1

locale:
en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

loaded via a namespace (and not attached):
[1] grid_2.7.1 lattice_0.17-8 nlme_3.1-89
>

__
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] Installation of packages via GUI, Mac OS X

2008-07-03 Thread David Afshartous

All,

I'm running R v2.7.1 on Mac OS X.  When I go to the R package installer GUI,
I am not prompted to select a repository.   The usual screen is there but
the columns for package, installed version, and repository version are all
populated via blank entries.

If I select "Get List" the error below occurs.  The same thing occurs if I
go form the command line via install.packages.  Any suggestions much
appreciated.

Cheers,
David


Error: subscript out of bounds
In addition: There were 50 or more warnings (use warnings() to see the first
50)
> warnings()
Warning messages:
1: In read.dcf(file = tmpf) ... : Line starting ' ...' is malformed!
2: In read.dcf(file = tmpf) ... : Line starting ' ...' is malformed!
3: In read.dcf(file = tmpf) ... :
  Line starting 'HostingZero.c ...' is malformed!

__
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] auto.key in xyplot in conjunction with panel.text

2008-07-03 Thread David Afshartous

Thanks!  That works perfect.


On 7/2/08 6:45 PM, "Deepayan Sarkar" <[EMAIL PROTECTED]> wrote:

> On 7/2/08, David Afshartous <[EMAIL PROTECTED]> wrote:
>> 
>> 
>>  All,
>> 
>>  I can't seem to get auto.key to work properly in an xyplot that is employing
>>  panel.text.  Specifically, I often change the default grouping colors then
>>  use auto.key accordingly, but for some reason the same functionality isn't
>>  working for this different type of plot.  Any help much appreciated.
> 
> You can't really expect it to work unless you go through
> panel.superpose. Try this:
> 
> xyplot(Y ~ X, data = dat, lab = dat$ID,
>groups = Drug,
>auto.key = list(space = "top", text = c("Placebo", "Drug"),
>points = FALSE, lines = TRUE),
>par.settings = list(superpose.line = list(col = c("red","black"))),
>panel = panel.superpose,
>panel.groups = function(x, y, lab, subscripts, col.line, ...){
>panel.text(x, y, labels = lab[subscripts], col = col.line)
>})
> 
> -Deepayan
> 
>>  Cheers,
>>  David
>> 
>> 
>> 
>> 
>>  library("lattice")
>>  dat = data.frame( Y = c(rnorm(18,1), rnorm(18,3)), X = rep(c(1:18), 2),
>>  ID = rep(c(1:18), 2), Drug = factor(rep(c("P", "D"), each = 18)) )
>>  ## this plot correctly provides the key for the grouping color
>>  xyplot(Y ~ X, data=dat, type="p",
>>panel=panel.superpose, groups=Drug,
>>col = rep(c("red", "black"), 18),
>>auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
>>points = FALSE, lines = TRUE),
>>  par.settings = list(superpose.line = list(col = c("red","black") ) ) )
>> 
>> 
>> 
>>  ## this plot correctly uses ID's and colors instead of plotting symbols
>>  xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
>>groups=Drug, col = rep(c("red", "black"), 18),
>>panel= function(x,y, lab, type, auto.key, ...){
>> panel.xyplot(x,y, type = type, ...)
>> panel.text(x,y, lab=lab,  ...)
>>}
>>  )
>> 
>>  ## when trying to get the correct key as in the first plot
>>  ## for the second plot things don't work.
>>  ## I've tried several alterations to the syntax but no luck so far
>>  xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
>>groups=Drug, col = rep(c("red", "black"), 18),
>>auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
>>points = FALSE, lines = TRUE), par.settings = list(superpose.line =
>>list(col = c("red","black") ) )
>>panel= function(x,y, lab, type, ...){
>> panel.xyplot(x,y, type = type, ...)
>> panel.text(x,y, lab=lab,  ...)
>>}
>>  )
>> 
>>  ## another unsuccessful attempt:
>>  xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
>>groups=Drug, col = rep(c("red", "black"), 18),
>>auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
>>points = FALSE, lines = TRUE), par.settings = list(superpose.line =
>>list(col = c("red","black") ) )
>>panel= function(x,y, lab, type, auto.key ...){
>> panel.xyplot(x,y, type = type, auto.key = auto.key ...)
>> panel.text(x,y, lab=lab,  ...)
>>}
>>  )
>> 
>>  __
>>  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] auto.key in xyplot in conjunction with panel.text

2008-07-02 Thread David Afshartous


All,

I can't seem to get auto.key to work properly in an xyplot that is employing
panel.text.  Specifically, I often change the default grouping colors then
use auto.key accordingly, but for some reason the same functionality isn't
working for this different type of plot.  Any help much appreciated.

Cheers, 
David




library("lattice")
dat = data.frame( Y = c(rnorm(18,1), rnorm(18,3)), X = rep(c(1:18), 2),
ID = rep(c(1:18), 2), Drug = factor(rep(c("P", "D"), each = 18)) )
## this plot correctly provides the key for the grouping color
xyplot(Y ~ X, data=dat, type="p",
   panel=panel.superpose, groups=Drug,
   col = rep(c("red", "black"), 18),
   auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
   points = FALSE, lines = TRUE),
par.settings = list(superpose.line = list(col = c("red","black") ) ) )



## this plot correctly uses ID's and colors instead of plotting symbols
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
   groups=Drug, col = rep(c("red", "black"), 18),
   panel= function(x,y, lab, type, auto.key, ...){
panel.xyplot(x,y, type = type, ...)
panel.text(x,y, lab=lab,  ...)
   }
)

## when trying to get the correct key as in the first plot
## for the second plot things don't work.
## I've tried several alterations to the syntax but no luck so far
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
   groups=Drug, col = rep(c("red", "black"), 18),
   auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
   points = FALSE, lines = TRUE), par.settings = list(superpose.line =
   list(col = c("red","black") ) )
   panel= function(x,y, lab, type, ...){
panel.xyplot(x,y, type = type, ...)
panel.text(x,y, lab=lab,  ...)
   }
)

## another unsuccessful attempt:
xyplot(Y ~ X, data=dat, type="n", lab = dat$ID,
   groups=Drug, col = rep(c("red", "black"), 18),
   auto.key = list(space = "top",  text = c( "Placebo", "Drug"),
   points = FALSE, lines = TRUE), par.settings = list(superpose.line =
   list(col = c("red","black") ) )
   panel= function(x,y, lab, type, auto.key ...){
panel.xyplot(x,y, type = type, auto.key = auto.key ...)
panel.text(x,y, lab=lab,  ...)
   }
)

__
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] Switching entries in vector in by groups of two

2008-06-27 Thread David Afshartous

Thanks Henrique, Marc, and Gabor!


On 6/27/08 10:17 AM, "Henrique Dallazuanna" <[EMAIL PROTECTED]> wrote:

> Try this:
> 
> ave(X, rep(1:(length(X)/2), each = 2), FUN=rev)
> 
> On Fri, Jun 27, 2008 at 11:11 AM, David Afshartous <[EMAIL PROTECTED]>
> wrote:
>> 
>> All,
>> 
>> I have a long vector that contains an even number of entries. I'd like to
>> switch the 1st and 2nd entry, the 3rd and 4th, and so on, without writing a
>> loop.
>> 
>> This code works:
>> 
>> X = c(8, 10, 6, 3, 20, 1)
>> index = c(2,1,4,3,6,5)
>> X[index]
>> 
>> But for a long list is there a way to generate the index?  I can get the
>> parts to the index as:
>> 
>> index.odd = seq(1,length(X), by  = 2)
>> index.even = index.odd + 1
>> 
>> Is there a simple way to interweave them to produce the desired index?  Or
>> is there a better way?
>> 
>> Cheers,
>> 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.
> 
> 



[[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] Switching entries in vector in by groups of two

2008-06-27 Thread David Afshartous

All,

I have a long vector that contains an even number of entries. I'd like to
switch the 1st and 2nd entry, the 3rd and 4th, and so on, without writing a
loop.

This code works:

X = c(8, 10, 6, 3, 20, 1)
index = c(2,1,4,3,6,5)
X[index]

But for a long list is there a way to generate the index?  I can get the
parts to the index as:

index.odd = seq(1,length(X), by  = 2)
index.even = index.odd + 1

Is there a simple way to interweave them to produce the desired index?  Or
is there a better way?

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


[R] Connecting lines across missing data points, xyplot

2008-06-26 Thread David Afshartous


All,

I have data across 5 time points that I am graphing via xyplot, along with
error bars.  For one of the variables I have missing data for two of the
time points.  The code below is okay but I can't seem to get the lines to
connect across the missing time points.  Does anyone now how to rectify
this?

Cheers,
David Afshartous



library(lattice)
## the data 
junk = data.frame(
Visit = as.factor(rep(seq(1,5), 2)),
Drug = rep(c("D", "P"), each = 5),
Aldo = c(13, NA, NA, 15, 14, 12, NA, NA, 14, 13),
SE.Aldo = c(3,  NA, NA, 3, 3, 2, NA, NA, 2, 2),
lower.ci.Aldo = c(10, NA, NA, 12, 11,  10,NA,  NA, 12, 11),
upper.ci.Aldo = c(16, NA, NA, 18, 17, 14, NA, NA, 16, 15)
)

## functions for the error bars
prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
x <- as.numeric(x)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
list(ylim = range(y, uy, ly, finite = TRUE)) }
panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) {
x <- as.numeric(x)
y <- as.numeric(y)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
panel.arrows(x, ly, x, uy, col = "black",
 length = 0.25, unit = "native",
 angle = 90, code = 3)
panel.xyplot(x, y, pch = 16, ...)}


## the plot, but visit 1 not connected to visit 4
xyplot(Aldo ~ as.numeric(Visit), xlab="Visit", ylab="Aldo",
groups=Drug,
data=junk,
ly = junk$lower.ci.Aldo,
uy = junk$upper.ci.Aldo,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = c( "D","P"), points = FALSE,
lines = TRUE, columns=2),
par.settings = list(superpose.line = list(lty = c(1,5), col=c('black',
'black') ) ))

__
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 bars within xyplot, panel = function(x,y, ....)

2008-06-16 Thread David Afshartous



All,


I'm trying to adapt some code provided by Deepayan Sarkar from a previous
thread (https://stat.ethz.ch/pipermail/r-help/2005-October/081571.html) on
this topic.

## This code produces a graph w/o error bars:

xyplot(Y ~ Hr, data, groups=DRUG,
panel=function(x,y,...){
  panel.xyplot(x,y,...,  type=c("g", "l")  )
  panel.points(x,y,..., pch=16, type='p', col='black', cex=1)
   },
auto.key = list(space = "top",  text = c( "D","P"), points = FALSE, lines =
TRUE, columns=2), par.settings = list(superpose.line = list(lty = c(1,5),
col=c('black', 'black') ) ) )


## this code uses the functions provided by Deepayan Sarkar to include the
## error bars for the same data:

xyplot(Y ~ Hr,
groups=DRUG,
data=data,
ly = data$lower,
uy = data$upper,
prepanel = prepanel.ci,
panel = panel.superpose,
panel.groups = panel.ci,
type="b",
auto.key = list(space = "top",  text = c( "D","P"), points = FALSE,
lines = TRUE, columns=2),
par.settings = list(superpose.line = list(lty = c(1,5), col=c('black',
'black') ) )
)

Is it possible to write the second version in the format of the first, i.e.,
using panel = function(x,y, ...){} ?

Thanks,
David


### load the following:

Hr = c(0,1,2,3,4,5,0,1,2,3,4,5)
DRUG = rep(c("D", "P"), each=6)
Y = c(1,2,2,2,2,1,3, 4, 4,4, 4, 3)
data = data.frame(Hr, DRUG, Y)
data$lower = data$Y - .5
data$upper = data$Y + .5


prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
x <- as.numeric(x)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
list(ylim = range(y, uy, ly, finite = TRUE)) }

panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) {
x <- as.numeric(x)
y <- as.numeric(y)
ly <- as.numeric(ly[subscripts])
uy <- as.numeric(uy[subscripts])
panel.arrows(x, ly, x, uy, col = "black",
 length = 0.25, unit = "native",
 angle = 90, code = 3)
panel.xyplot(x, y, pch = 16, ...)}

__
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] aggregate() function, strange behavior for augmented data

2008-06-16 Thread David Afshartous

Everything was read in the same way, and str(junk1) confirms that they are
the same structure.  This is very strange.

## original data:
> str(junk1)
'data.frame':   96 obs. of  3 variables:
 $ Hour: int  0 3 5 0 3 5 0 3 5 0 ...
 $ Drug: Factor w/ 2 levels "D","P": 2 2 2 1 1 1 2 2 2 1 ...
 $ Aldo: int  9 15 4 8 13 3 5 11 5 7 ...

## augmented data:
> str(junk1)
'data.frame':108 obs. of  3 variables:
 $ Hour: int  0 3 5 0 3 5 0 3 5 0 ...
 $ Drug: Factor w/ 2 levels "D","P": 2 2 2 1 1 1 2 2 2 1 ...
 $ Aldo: int  9 15 4 8 13 3 5 11 5 7 ...






On 6/16/08 11:37 AM, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:

> 
> hi: do str(junk1) and it will tell you what  the components of junk1
> are.
> 
> the only thing i can think of is that you used stringsAsFactors=FALSE
> when you ( probably ) used read.table to read in junk but you didn't use
> that
> options when you used read.table  to read in junk1 ?
> 
> 
> On Mon, Jun 16, 2008 at 11:30 AM, David Afshartous wrote:
> 
>> All,
>> 
>> I'm re-running some analysis that has been augmented with additional
>> data.
>> When I use the exact same code for the augmented data, the behavior of
>> the
>> aggregate function is very strange, viz., one of the resulting
>> variables is
>> now coded as a factor while it was coded as numeric for the original
>> data.
>> Unfortunately, I cannot provide a reproducible code example since it
>> only
>> seems to occur with this data.  I've checked and re-checked the of
>> both the
>> original and augmented data but nothing appears inconsistent.  Any
>> suggestions much appreciated.  See below for specifics.
>> 
>> Cheers,
>> David
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> # original data
>>> dim(junk1)
>> [1] 96  3
>>> junk1[1,]
>>   Hour Drug Aldo
>> 10P9
>>> junk1$Hour
>>  [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5
>> 0 3
>> 5 0 3
>> [39] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
>> 5 0
>> 3 5 0
>> [77] 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5   ### Not coded as a
>> factor
>>> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
>>> junk1.mean.time.drug$Hour
>> [1] 0 3 5 0 3 5  ### not coded as a factor
>> 
>> # augmented data
>>  dim(junk1)
>> [1] 108   3
>>> junk1[1,]
>>   Hour Drug Aldo
>> 10P9
>>> junk1$Hour
>>   [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
>> 5 0 3
>> 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
>>  [51] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
>> 3 5 0
>> 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
>> [101] 3 5 0 3 5 0 3 5### not coded as a factor
>>> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
>>> junk1.mean.time.drug$Hour
>> [1] 0 3 5 0 3 5
>> Levels: 0 3 5## coded as a factor now!
>> 
>> ## of course, I get recode it again but I'm curious as to why this is
>> ## changing here
>> 
>> __
>> 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] aggregate() function, strange behavior for augmented data

2008-06-16 Thread David Afshartous


All,

I'm re-running some analysis that has been augmented with additional data.
When I use the exact same code for the augmented data, the behavior of the
aggregate function is very strange, viz., one of the resulting variables is
now coded as a factor while it was coded as numeric for the original data.
Unfortunately, I cannot provide a reproducible code example since it only
seems to occur with this data.  I've checked and re-checked the of both the
original and augmented data but nothing appears inconsistent.  Any
suggestions much appreciated.  See below for specifics.

Cheers,
David









# original data
> dim(junk1)
[1] 96  3
> junk1[1,]
  Hour Drug Aldo
10P9
> junk1$Hour
 [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
5 0 3
[39] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
3 5 0
[77] 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5   ### Not coded as a factor
> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
> junk1.mean.time.drug$Hour
[1] 0 3 5 0 3 5  ### not coded as a factor

# augmented data
 dim(junk1)
[1] 108   3
> junk1[1,]
  Hour Drug Aldo
10P9
> junk1$Hour
  [1] 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
5 0 3 5 0 3 5 0 3 5 0 3 5 0 3
 [51] 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
3 5 0 3 5 0 3 5 0 3 5 0 3 5 0
[101] 3 5 0 3 5 0 3 5### not coded as a factor
> junk1.mean.time.drug = aggregate(junk1[3], junk1[c(1,2)], mean)
> junk1.mean.time.drug$Hour
[1] 0 3 5 0 3 5
Levels: 0 3 5## coded as a factor now!

## of course, I get recode it again but I'm curious as to why this is
## changing here

__
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] Alignment of axes intersection

2008-05-20 Thread David Afshartous

Agreed.  The main reason I wanted the change in alignment was that I had
three curves that were converging to a asymptote, and when I drew the
horizontal asymptote via abline(), it distorted the picture somewhat since
the line from abline() goes all the way to the y-axis.




On 5/20/08 12:21 PM, "Greg Snow" <[EMAIL PROTECTED]> wrote:

> Mathematicians like to have axes cross at 0, the general rule for statistics
> is to have the axes positioned so that they help you understand the data, but
> don't interfere with the actual points (or force too much whitespace by being
> put to far away from the data), so the default positioning follows that idea.
> If you really want the axes to cross at 0 you can do:
> 
>> plot(0:10, 0:10, axes=FALSE)
>> axis(1, pos=0)
>> axis(2, pos=0)
> 
> 
> 
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> [EMAIL PROTECTED]
> (801) 408-8111
> 
> 
> 
>> -----Original Message-
>> From: [EMAIL PROTECTED]
>> [mailto:[EMAIL PROTECTED] On Behalf Of David Afshartous
>> Sent: Tuesday, May 20, 2008 9:50 AM
>> To: r-help@r-project.org
>> Subject: [R] Alignment of axes intersection
>> 
>> 
>> All,
>> 
>> Very basic question I can't seem to find the answer to:
>> 
>> plot(0:10, 0:10)
>> 
>> The axes intersection is not aligned at (0,0) in the lower left.
>> How does one force this?
>> 
>> I searched for graphical parameters under par(graphics) but
>> can't seem to find it.
>> 
>> 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.
>> 
>

__
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] Alignment of axes intersection

2008-05-20 Thread David Afshartous

All,

Very basic question I can't seem to find the answer to:

plot(0:10, 0:10)

The axes intersection is not aligned at (0,0) in the lower left.
How does one force this?

I searched for graphical parameters under par(graphics) but can't seem to
find it.

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] (x,y) coordinates for grid.text()

2008-03-04 Thread David Afshartous

Thanks, yes "native" is the right one.  The code below now works as desired:

trellis.focus("panel", 1, 1)
grid.text("put text here", x = 1, y = 1.5, default.units = "native")


On 3/4/08 1:44 PM, "Erik Iverson" <[EMAIL PROTECTED]> wrote:

> You want "native" coordinates.  But since you are using lattice (built
> 'on top' of grid), you probably need some additional info, like the name
> of the viewport you want to write to I assume(?), but I can't help you
> with that part.
> 
> 
> David Afshartous wrote:
>> Okay, I see that default.units is set to "npc" and hence the behavior I
>> mentioned.  Looking at ?unit, I see the description of various units but it
>> isn't clear which one I need to select to achieve the result I specified
>> earlier.  Maybe I'm missing something very basic, but I assume there must be
>> a simple way to add text to an xyplot() at a specified (x,y) coordinate in
>> the units of the plotted points?
>> 
>> 
>> 
>> 
>> 
>> On 3/4/08 12:18 PM, "hadley wickham" <[EMAIL PROTECTED]> wrote:
>> 
>>>>  When using grid.text it seems my supplied (x,y) coordinates are being
>>>>  plotted only in npc (normalized parent coordinates) where (.5,.5) is the
>>>>  center of the graph.  How do I allow (x,y) to be coordinates corresponding
>>>>  to the (x,y) values in the graph?   The examples in ?grid.text seem to do
>>>>  this.  (I think the problem lies in how x and y are defined with respect
>>>> to
>>>>  unit ).   Any help much appreciated.  Sample code below.
>>> Have you read the documentation for grid.text?  You need to read the
>>> description of the default.units argument and then read ?unit.
>>> 
>>> Hadley
>> 
>> __
>> 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] (x,y) coordinates for grid.text()

2008-03-04 Thread David Afshartous

Okay, I see that default.units is set to "npc" and hence the behavior I
mentioned.  Looking at ?unit, I see the description of various units but it
isn't clear which one I need to select to achieve the result I specified
earlier.  Maybe I'm missing something very basic, but I assume there must be
a simple way to add text to an xyplot() at a specified (x,y) coordinate in
the units of the plotted points?





On 3/4/08 12:18 PM, "hadley wickham" <[EMAIL PROTECTED]> wrote:

>>  When using grid.text it seems my supplied (x,y) coordinates are being
>>  plotted only in npc (normalized parent coordinates) where (.5,.5) is the
>>  center of the graph.  How do I allow (x,y) to be coordinates corresponding
>>  to the (x,y) values in the graph?   The examples in ?grid.text seem to do
>>  this.  (I think the problem lies in how x and y are defined with respect to
>>  unit ).   Any help much appreciated.  Sample code below.
> 
> Have you read the documentation for grid.text?  You need to read the
> description of the default.units argument and then read ?unit.
> 
> Hadley

__
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] (x,y) coordinates for grid.text()

2008-03-04 Thread David Afshartous


All,

When using grid.text it seems my supplied (x,y) coordinates are being
plotted only in npc (normalized parent coordinates) where (.5,.5) is the
center of the graph.  How do I allow (x,y) to be coordinates corresponding
to the (x,y) values in the graph?   The examples in ?grid.text seem to do
this.  (I think the problem lies in how x and y are defined with respect to
unit ).   Any help much appreciated.  Sample code below.

Cheers,
David




y = c( 0.4,  0.6, -0.1,  0.3,  0.3, -0.2,  0.7,  0.7,  0.2,  0.0,  0.9,
-0.1,  0.6, -1.1,  0.8, -1.0,  0.4,  0.1,  0.7, -0.2, -0.1, -0.1,  2.2,
0.7,  1.1,  0.2, -0.2, -0.9,  0.4,  0.1, -0.3, -0.4)
x = c(4.1000,  4.9600,  1.2000,  3.9000,  3.1875,  1.9000,  1.8625,
0.7650,  1.5750,  2.4700,  1.6250,  1.5500,  2.3125,  1.3125,  1.0600,
-0.5500,  1.1000,  0.0200, -0.0375,  3.4600,  2.5250,  2.0950,  0.8000,
1.6050, -0.4150, -0.7300,  1.1550,  1.4850,  2.2000,  2.2500,  0.6000,
2.1000)
junk.frm = data.frame(ID = rep(1:16, each = 2), x, y, z = rep(c("D", "P"),
16))
xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20)

grid.text("put text here", x = 1, y = 1.5)  # doesn't show up
grid.text("put text here", x = .4, y = .7) # shows up in the desired
location but this is for npc

__
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] Replacing plot symbols w/ subject IDs in xyplot()

2008-02-29 Thread David Afshartous


For plot():
plot(x,y, type = "n")
text(x,y, lab= junk.frm$ID)

This accomplishes the goal of using subject IDs as plot symbols, but I can't
seem to get the same result below w/ xyplot().  Is there something different
I have to do for xyplot()?

xyplot(y ~ x, type = "n")
text(x,y , lab=junk.frm$ID)
# doesn't work 



On 2/28/08 5:09 PM, "Bert Gunter" <[EMAIL PROTECTED]> wrote:

> ?text
> 
> e.g 
> 
> plot(x,y,type="n") ##
> text(x,y,lab= whatever)
> 
> This replaces the plot symbols with the ID's. If you just wish to plot them
> next, remove the type="n" specification and just offset the x's in text a
> hair.
> 
> -- Bert Gunter
> Genentech
> 
> 
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
> Behalf Of David Afshartous
> Sent: Thursday, February 28, 2008 1:15 PM
> To: r-help@r-project.org
> Subject: [R] Replacing plot symbols w/ subject IDs in xyplot()
> 
> 
> 
> All, 
> 
> How does one replace plot symbols with say subject IDs when using xyplot? Or
> superimpose them next to plot symbols?  I searched the archives under
> various key words but haven't had much.  Any suggestions or links much
> appreciated.  Sample code below.
> 
> David
> 
> 
> 
> 
> 
> junk.frm = data.frame(ID = rep(1:16, each = 2), x, y, z = rep(c("D", "P"),
> 16))
> y = c( 0.4,  0.6, -0.1,  0.3,  0.3, -0.2,  0.7,  0.7,  0.2,  0.0,  0.9,
> -0.1,  0.6, -1.1,  0.8, -1.0,  0.4,  0.1,  0.7, -0.2, -0.1, -0.1,  2.2,
> 0.7,  1.1,  0.2, -0.2, -0.9,  0.4,  0.1, -0.3, -0.4)
> x = c(4.1000,  4.9600,  1.2000,  3.9000,  3.1875,  1.9000,  1.8625,
> 0.7650,  1.5750,  2.4700,  1.6250,  1.5500,  2.3125,  1.3125,  1.0600,
> -0.5500,  1.1000,  0.0200, -0.0375,  3.4600,  2.5250,  2.0950,  0.8000,
> 1.6050, -0.4150, -0.7300,  1.1550,  1.4850,  2.2000,  2.2500,  0.6000,
> 2.1000)
> xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
> "smooth"), pch = 20)
> 
> __
> 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] Replacing plot symbols w/ subject IDs in xyplot()

2008-02-28 Thread David Afshartous


All, 

How does one replace plot symbols with say subject IDs when using xyplot? Or
superimpose them next to plot symbols?  I searched the archives under
various key words but haven't had much.  Any suggestions or links much
appreciated.  Sample code below.

David





junk.frm = data.frame(ID = rep(1:16, each = 2), x, y, z = rep(c("D", "P"),
16))
y = c( 0.4,  0.6, -0.1,  0.3,  0.3, -0.2,  0.7,  0.7,  0.2,  0.0,  0.9,
-0.1,  0.6, -1.1,  0.8, -1.0,  0.4,  0.1,  0.7, -0.2, -0.1, -0.1,  2.2,
0.7,  1.1,  0.2, -0.2, -0.9,  0.4,  0.1, -0.3, -0.4)
x = c(4.1000,  4.9600,  1.2000,  3.9000,  3.1875,  1.9000,  1.8625,
0.7650,  1.5750,  2.4700,  1.6250,  1.5500,  2.3125,  1.3125,  1.0600,
-0.5500,  1.1000,  0.0200, -0.0375,  3.4600,  2.5250,  2.0950,  0.8000,
1.6050, -0.4150, -0.7300,  1.1550,  1.4850,  2.2000,  2.2500,  0.6000,
2.1000)
xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20)

__
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] Subsetting within xyplot()

2008-02-26 Thread David Afshartous

All,

I'm having problems w/ a simple attempt to subset an xyplot.

The first plot below is a plot of y versus x for certain values of a third
categorical variable z.  Now I'd like to further restrict this to certain
values of variable y.  Neither of the two attempts below work.  Any
suggestions much appreciated.  (note: I don't want to merely use ylim since
I have a loess plot and I want this to be calculated w/ the restricted
values).

Thanks,
David 




y = c( 0.4,  0.6, -0.1,  0.3,  0.3, -0.2,  0.7,  0.7,  0.2,  0.0,  0.9,
-0.1,  0.6, -1.1,  0.8, -1.0,  0.4,  0.1,  0.7, -0.2, -0.1, -0.1,  2.2,
0.7,  1.1,  0.2, -0.2, -0.9,  0.4,  0.1, -0.3, -0.4)
x = c(4.1000,  4.9600,  1.2000,  3.9000,  3.1875,  1.9000,  1.8625,
0.7650,  1.5750,  2.4700,  1.6250,  1.5500,  2.3125,  1.3125,  1.0600,
-0.5500,  1.1000,  0.0200, -0.0375,  3.4600,  2.5250,  2.0950,  0.8000,
1.6050, -0.4150, -0.7300,  1.1550,  1.4850,  2.2000,  2.2500,  0.6000,
2.1000)
junk.frm = data.frame(x, y, z = rep(c("D", "P"), 16))

xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20)

xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20,
, subset = junk.frm[junk.frm$y < 2, ])
Error in tmp[subset] : invalid subscript type 'list'

xyplot(y ~ x , data = junk.frm[junk.frm$z =="D",], type = c("g", "p",
"smooth"), pch = 20,
data = junk.frm[junk.frm$y < 2 & junk.frm$z =="D", ])
Error in xyplot(y ~ x, data = junk.frm[junk.frm$z == "D", ], type = c("g",
: 
  formal argument "data" matched by multiple actual arguments

__
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] Extending curve() function; 2D function plot with arguments

2008-02-07 Thread David Afshartous

All,

I have a simple function below that defines a 2-dimensional curve:

n.j = 4; sigma.y = 1.2; sigma.a = 2.2; y.bar.j = 8.1; mu.a = 4.4

alpha.j.fun <- function(sigma.a) {
alpha.j = ((n.j/sigma.y^2)*y.bar.j + (1/sigma.a^2)*mu.a)/(n.j/sigma.y^2
+ 1/sigma.a^2   )
alpha.j}

The parameters to the function are hard coded.  I plot the function via
curve():

curve(alpha.j.fun, 2, 4, ylim = c(0,10), xlab="sigma.a values",
ylab="alpha.j.hat")


Is there a way to supply the function parameters parameter as arguments,
viz., to graph alpha.j versus sigma.a for a different value of sigma.y?

Below is an attempted solution, but this is very clunky and also doesn't
work since the embedded function isn't using the new value for the parameter
to be changed (sigma.y). (Also, this solution wouldn't be useful for the
goal of adding several curves on one plot via add=TRUE.)

alpha.j.fun.general <- function(sigma.y.value) {
sigma.y = sigma.y.value
curve(alpha.j.fun, 2, 4, ylim = c(0,10), xlab="sigma.a values",
ylab="alpha.j.hat")
}

Any suggestions much appreciated.

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.


[R] Stratifying level-1 variance with lmer()

2008-02-05 Thread David Afshartous


All,

I've fit some models via lme() and now I'm trying to fit similar models with
lmer() for some simulations I'm running.

The model below (fm1) has an intercept variance that depends on treatment
group.  How would one accomplish a similar stratification for the level-1
variance, i.e., the within-group or patient variance?

With lme() I was able to do this via:
update(fm1,
weights = varIdent(form = ~ 1 | treatment.ind) )

However, this does not work for lmer().  Is there any way around this?

Model and simulated data below.

Cheers,
David



 
fm1 = lmer (y ~  treatment.ind + ( 0 + placebo.ind | person1) + (0 +
treatment.ind | person1), data = fake)

###

Strat.mean.var.simple <- function (J, K){
 # function to generate simple longit
 time <- rep(seq(0,1, ,length=K), J) # K measurements
 person <- rep(1:(J/2), each = K)
 treatment <- rep(0:1, each = J/2)
 treatment.ind <- rep(0:1, each = (J/2)*K)
 person1 <- rep(1:J, ,each = K)
 placebo.ind.1 <- treatment.ind < 1
 placebo.ind = ifelse( placebo.ind.1, 1, 0)
 #
 mu.a.true.P = 4.8
 mu.a.true.T = 8.8
 sigma.a.true.P = 2.2
 sigma.a.true.T = 4.2
 sigma.y.true = 1.2
 #
 a.true.P = rnorm (J/2, mu.a.true.P, sigma.a.true.P)
 a.true.T = rnorm (J/2, mu.a.true.T, sigma.a.true.T)
 #
 y.P <- rnorm( (J/2)*K, a.true.P[person], sigma.y.true)
 y.T <- rnorm( (J/2)*K, a.true.T[person], sigma.y.true)
 y <- c(y.P, y.T)
 return ( data.frame (y, time, person1, treatment.ind, placebo.ind))
 }
 
 
 J = 10
 K = 4
 set.seed(500)
 fake = Strat.mean.var.simple (J,K)

__
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] Extracting level-1 variance from lmer()

2008-02-05 Thread David Afshartous

All,

How does one extract the level-1 variance from a model fit via lmer()?

In the code below the level-2 variance component may be obtained via
subscripting, but what about the level-1 variance, viz., the 3.215072 term?
(actually this term  squared)  Didn't see anything in the archives on this.

Cheers,
David



> fm <- lmer( dv ~ time.num*drug + (1 | Patient.new),  data=dat.new )

> VarCorr(fm)
$Patient.new
1 x 1 Matrix of class "dpoMatrix"
(Intercept)
(Intercept)8.519916

attr(,"sc")
   scale 
3.215072 

> VarCorr(fm)[[1]][1]
[1] 8.519916
> VarCorr(fm)[[2]][1]
Error in VarCorr(fm)[[2]] : subscript out of bounds







##
set.seed(500)
n.timepoints <- 4
n.subj.per.tx <- 20
sd.d <- 5;
sd.p <- 2;
sd.res <- 1.3
drug <- factor(rep(c("D", "P"), each = n.timepoints, times =
n.subj.per.tx))
drug.baseline <- rep( c(0,5), each=n.timepoints, times=n.subj.per.tx )
#Patient <- rep(1:(n.subj.per.tx*2), each = n.timepoints)
Patient.baseline <- rep( rnorm( n.subj.per.tx*2, sd=c(sd.d, sd.p) ),
each=n.timepoints )
time.baseline <- rep(1:n.timepoints,n.subj.per.tx*2)*as.numeric(drug=="D")
dv <- rnorm( n.subj.per.tx*n.timepoints*2,
mean=time.baseline+Patient.baseline+drug.baseline, sd=sd.res )

dat.new <- data.frame(drug, dv)
dat.new$Dind <- as.numeric(dat.new$drug == "D")
dat.new$Pind <- as.numeric(dat.new$drug == "P")
dat.new$time.num = rep(1:n.timepoints, n.subj.per.tx*2)
dat.new$Patient.new = rep(1:20, each=8)

__
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] Alternating numbers in rep()

2008-01-24 Thread David Afshartous

All,

I'm trying to obtain a one-liner to generate a certain sequence of
alternatign numbers.

Consider:
> unlist(rep(list(c(1,2), c(3,4)), each = 6))
 [1] 1 2 1 2 1 2 1 2 1 2 1 2 3 4 3 4 3 4 3 4 3 4 3 4

I'd like the result to be as above but continue until 38.  Of course, I
could hardcode this going up to c(37,38), but is there a more elegant way?


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.


[R] Wireframe graph: black and white shading instead of color

2007-11-29 Thread David Afshartous


All,

The code below produces a color 3D graph.  I'd like to make it black and
white shading.  I tried setting col.regions to FALSE but this just made it
completely white.  I want the graph to look exactly as is, except black (or
grey) and white shading.  Is this possible?

Cheers,
David


p.list = c(.01, .1, .25, .5, .75, .9)
cov.list = c(.2, .1, .05)  ### leave out .01



X = rep(p.list, 3) 
Y = rep(cov.list, 1, each=6)
Z = c(2475.00,   225.00,75.00,25.00, 8.33,
2.78,  9900.00, 900.00,   300.00,   100.00,
33.33,11.11, 39600.00,  3600.00,
1200.00,   400.00,   133.33,44.4)


lattice.options(default.theme = "col.whitebg")



wireframe(Z ~ X * Y, scales = list(arrows = FALSE),

   bg = "white", drape = TRUE, shade = FALSE, colorkey = FALSE,
xlab = "p", ylab = "C.O.V.", zlab ="n")

__
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] Adding points on top of lines in xyplot

2007-11-20 Thread David Afshartous

All,

I'm trying to make a basic plot: data points superimposed upon the a line
connecting the points w/ a different color.  Example below doesn't work as
the first xyplot call doesn't remain.  Suggestions?

David


Hour = c(NA,1,2,3,4)
y = c(2,2,3,2,1.5)

xyplot(y ~ Hour, xlab = list("Hour", font=2, cex=2), ylab= list("U_x * V",
font=2, cex=2), type = 'l', scales = list( x= list(tick.number=5, cex=2,
limits=c(0,4.5)), y=list(cex=0)), lines=TRUE, lwd = 2 , add=TRUE)


xyplot(y ~ Hour, xlab= "", ylab="", pch= 16, col=  "red", cex=3 , xlim =
c(0, 4.5), add=TRUE)

__
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] Basic plot question: Figure 1.1 Pinheiro & Bates

2007-10-12 Thread David Afshartous
All,
Sorry for overly simplistic question, but I can't seem to remember how to
create the basic plot shown in Figure 1.1 of Pinheiro & Bates (2004; p.4).
The y-axis delineates a factor (Rail) while the x-axis displays the
distribution of a continuous variable (time) according to each level of the
factor.  Didn't see it in archives but perhaps I'm not searching on correct
key words, any help appreciated,
David


> library("nlme")
> Rail
Grouped Data: travel ~ 1 | Rail
   Rail travel
1 1 55
2 1 53
3 1 54
4 2 26
5 2 37
6 2 32
7 3 78
8 3 91
9 3 85
104 92
114100
124 96
135 49
145 51
155 50
166 80
176 85
186 83

__
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] Rounding of lme coefficients: Mac vs Windows

2007-10-06 Thread David Afshartous
All,

I have an lme model estimated in R 2.5.1 on my Mac; when I estimate the same
model on Windows, the parameter coefficients are rounded to integers.  Below
is a similar example for the Orthodont data.  Is there some option I need to
set in the Windows version to prevent rounding?  Didn't see this in the
archives.

Thanks,
David Afshartous


> sessionInfo()
R version 2.5.1 (2007-06-27)
i386-apple-darwin8.9.1

locale:
en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] "stats" "graphics"  "grDevices" "utils" "datasets"  "methods"
"base" 

other attached packages:
nlme 
"3.1-84" 
> fm1 <- lme(distance ~ age, data = Orthodont) # random is ~ age
> 
> fm1
Linear mixed-effects model fit by REML
  Data: Orthodont 
  Log-restricted-likelihood: -221.3183
  Fixed: distance ~ age
(Intercept) age
 16.761   0.6601852

Random effects:
 Formula: ~age | Subject
 Structure: General positive-definite
StdDevCorr
(Intercept) 2.3270338 (Intr)
age 0.2264276 -0.609
Residual1.3100399

Number of Observations: 108
Number of Groups: 27


> sessionInfo()
R version 2.6.0 (2007-10-03)
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] nlme_3.1-85   foreign_0.8-23arm_1.0-33R2WinBUGS_2.1-6
[5] coda_0.12-1   lme4_0.99875-8Matrix_0.999375-2 lattice_0.16-5
[9] MASS_7.2-36   

loaded via a namespace (and not attached):
[1] grid_2.6.0  tools_2.6.0
> fm1 <- lme(distance ~ age, data = Orthodont) # random is ~ age
> fm1
Linear mixed-effects model fit by REML
  Data: Orthodont 
  Log-restricted-likelihood: -221
  Fixed: distance ~ age
(Intercept) age
  16.760.66

Random effects:
 Formula: ~age | Subject
 Structure: General positive-definite
StdDev Corr
(Intercept) 2.33   (Intr)
age 0.23   -0.61
Residual1.31  

Number of Observations: 108
Number of Groups: 27
>

__
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] Multiple R sessions, Mac version

2007-09-13 Thread David Afshartous
Hello all,

I've just switched to running R 2.5.1 on a Mac 0S X 10.4.1 platform.  I
can't seem to find how to run simultaneous R sessions.  Didn't see anything
in the archives on this or under the R file menu.

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.


[R] Multiple R sessions, Mac version

2007-09-13 Thread David Afshartous
Hello all,

I've just switched to running R 2.5.1 on a Mac 0S X 10.4.1 platform.  I
can't seem to find how to run simultaneous R sessions.  Didn't see anything
in the archives on this or under the R file menu.

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.