Re: [R] Modifying the embed-results

2006-08-26 Thread Atte Tenkanen
Again my example was't very clear: there were not enough same numbers in the 
VECTOR.
What I need is something like this:

VECTOR-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8)
MATRIX-c()
for(i in 1:length(VECTOR)){
v-(unique(VECTOR[i:length(VECTOR)])[1:5])
MATRIX-rbind(MATRIX,v)
}

MATRIX-na.omit(MATRIX)
MATRIX

 data.frame(MATRIX, row.names=NULL)
  X1 X2 X3 X4 X5
1   0  3  6 11  2
2   3  6 11  2  4
3   6  3 11  2  4
4   3 11  2  4  7
5  11  2  4  3  7
6   2 11  4  3  7
7  11  4  3  7  6
8   4  3  7  6  8
9   3  4  7  6  8

So, there are no duplicates in rows. 
VECTOR is always scanned forward as long as the number of  items (here 5) 
becomes full.

Atte

 Try:
 
 embed(VECTOR, 5)[,5:1]
 
 On 8/25/06, Atte Tenkanen [EMAIL PROTECTED] wrote:
  Hi,
 
  Here is a vector and the result from the embed-command:
 
  VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8)
 
   embed(VECTOR, dimension=5)
   [,1] [,2] [,3] [,4] [,5]
   [1,]   113630
   [2,]2   11363
   [3,]42   1136
   [4,]342   113
   [5,]7342   11
   [6,]67342
   [7,]46734
   [8,]54673
   [9,]   105467
  [10,]2   10546
  [11,]32   1054
  [12,]532   105
  [13,]8532   10
 
  Is there a way to little modify the algorithm so that the result 
 looks like this:
 
  [1]  0  3  6 11  2 - beginning from the first number of the VECTOR
  [1]  3  6 11  2  4 - beginning from the second number of the 
 VECTOR etc
  [1]  6  3 11  2  4
  [1]  3 11  2  4  7
  [1] 11  2  4  3  7
  [1] 2 4 3 7 6
  [1] 4 3 7 6 5
  [1] 3 7 6 4 5
  [1]  7  6  4  5 10
  [1]  6  4  5 10  2
  [1]  4  5 10  2  3
  [1]  5 10  2  3  8
  [1] 10  2  3  5  8
 
  Every row consists of next five unique(!) member of the VECTOR. I 
 made this example result with a time consuming algorithm which 
 uses for-loops
  and whiles.
 
  How to do this better??
 
  Thanks in advance!
 
  Atte Tenkanen
  University of Turku
 
  __
  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-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] Problem on Histogtam

2006-08-26 Thread stat stat
Dear all,

May be  question seems trivial for most of the R
users, but really at least for me, this comes out to
be very problematic. 

Suppose I have the following data:

 r
  [1] -0.0008179960 -0.0277968529 -0.0105731583
-0.0254050262  0.0321847131  0.0328170674
  [7]  0.0431894392 -0.0217614918 -0.0218366946 
0.0048939739 -0.0012212499  0.0032533579
 [13] -0.0081533269 -0.0098725606 -0.0099710008
-0.0130281313  0.0121927094  0.0029209284
 [19] -0.0206279678 -0.0210624593 -0.0100415600
-0.0266801063 -0.0241744954  0.0178453603
 [25]  0.0452204703  0.0133478839  0.0274220937 
0.0091135676  0.0380304730 -0.0087719861
 [31] -0.0412893310 -0.0092243841 -0.0016863410
-0.0080491856 -0.0293504020  0.0411898638
 [37] -0.0037902762 -0.0076239251  0.0193690266
-0.0012518257  0.0259647803  0.0241169245
 [43]  0.0446589449  0.0536073638 -0.0028839241 
0.0168251687  0.0095390230 -0.0213227695
 [49] -0.0519741069 -0.0287872498 -0.0035108287 
0.0197336642 -0.0065347148 -0.0011576308
 [55] -0.0085304899  0.0162228355  0.0087736592 
0.0007593015  0.0113208756  0.0229980462
 [61]  0.0370746078  0.0679558204  0.0401218485
-0.005759  0.0188922496 -0.0230330422
 [67] -0.0261938985 -0.0503969590 -0.0069156569
-0.0031277176 -0.0235967983 -0.0100287373
 [73] -0.0014409224 -0.0307491283 -0.0240794177
-0.0091813958  0.0076570053  0.0621042105
 [79]  0.0156475454  0.0353589695  0.0078032627 
0.0070719271  0.0026809668 -0.0111056164
 [85] -0.0088375830  0.0128902735  0.0279180644 
0.0268407382 -0.0038363218 -0.0194055249
 [91] -0.0284881803  0.0016786977 -0.0145297369 
0.0081356381 -0.0187430943  0.0123078477
 [97]  0.0148400700 -0.0070552955  0.0103975446 
0.0109508312  0.0147422817  0.0218728566
[103] -0.0041447533 -0.0096308931  0.0289322358 
0.0216995495  0.0499431039  0.0717506367
[109]  0.0032529169 -0.0040678022 -0.0331522073
-0.0164263110 -0.0322093707  0.0542379373
[115]  0.0259181864  0.0387076215  0.0044415488
-0.0078513881  0.0411792612  0.0062837960
[121] -0.0243481467  0.0328312315 -0.0520127424
-0.0137024542 -0.0367509610 -0.0149753215
[127]  0.0256530410  0.0376798081  0.0106952891 
0.0525638932  0.0281572000  0.0324963843
[133] -0.0170584812 -0.0063837550  0.0294482602
-0.0118152654 -0.0169216729  0.0061436866
[139] -0.0245630568  0.0093716894 -0.0122730454 
0.0234516610 -0.0090283341  0.0059488575
[145] -0.0104937762 -0.0123026519  0.0263488297
-0.0087855249  0.0021441342  0.0204930031
[151] -0.0079588435 -0.0021173987  0.0135674596 
0.0053296384 -0.0004623209  0.0199159610
[157] -0.0034056112 -0.0052445679 -0.0142760886
-0.0158956129 -0.0137605967 -0.0007169316
[163]  0.0236250062  0.0002334540  0.0007000350
-0.0023353584 -0.0210285362 -0.0178275018
[169] -0.0663226429  0.0098217383 -0.0111212836
-0.0597472435  0.0371266051 -0.0247790755
[175]  0.0337833736  0.0265364166 -0.0160458929 
0.0085725944  0.0161645598  0.0700273748
[181] -0.0308458190  0.0362889752 -0.0195430433 
0.0284716741  0.0097766142 -0.0037131628
[187]  0.0275183610  0.0276618128  0.0109410282
-0.0302685208  0.0191074465 -0.0086197904
[193]  0.0013309674  0.0169253408 -0.0010903937 
0.0073913380  0.0330203607  0.0116667990
[199] -0.0099917567  0.0035501760 -0.0231980036 
0.0023441673 -0.0150120769 -0.0165581125
[205] -0.0101568548 -0.0031118051 -0.0051333669 
0.0312800833 -0.0050005540 -0.0109578129
[211] -0.0222825618 -0.0388242167 -0.0037549915
-0.0185102580 -0.0120483385  0.0336106003
[217]  0.0146599484  0.0146757965  0.0117675040 
0.0196041030 -0.0030932415 -0.0024371343
[223] -0.0158751238 -0.0145292000  0.0050182587 
0.0061245513  0.0139234228 -0.0116645558
[229] -0.0475950303 -0.0346688548  0.0041559771
-0.0246987117  0.0195623423  0.0167785091
[235] -0.0133513332 -0.0113051093  0.0215167225
-0.0009680543 -0.0141431277 -0.0150936925
[241] -0.0183676066 -0.0117498159  0.0165629981
-0.0248189789 -0.0133004960 -0.0132137229
[247] -0.0429413799 -0.0055694937  0.0005589715 
0.0168908903  0.0321518982  0.0007973422
[253]  0.0573060001 -0.0078076206 -0.0253502245 
0.0210426035  0.0025361414 -0.0091603694
[259] -0.0064111003 -0.0126862543 -0.0067974118 
0.0023581827 -0.0359703092  0.0185464260
[265] -0.0007992541 -0.0193762790  0.0225694695 
0.0052994295 -0.0039719369  0.0144872809
[271]  0.0002614721 -0.0076105865 -0.0153971717 
0.0056022555 -0.0104292963 -0.0189965007
[277]  0.0079138085  0.0254944708 -0.0165646320
-0.0162958522 -0.0107365106  0.0049696403
[283]  0.0041225838  0.00 -0.0035719230
-0.0200173489 -0.0022490873 -0.0039481157
[289] -0.0182497223  0.0157033919 -0.0154156635
-0.0023041485 -0.0028876717  0.00
[295] -0.0195655241  0.0210104023 -0.0020234145
-0.0026075634  0.0149729259  0.0045623119
[301]  0.0431521368  0.0228966231 -0.0077530130 
0.0285733724 -0.0091707704 -0.0137823544
[307]  0.0092974504 -0.0076974501  0.0106017426 
0.0481245566  0.0164469194  0.0356877294
[313]  0.0002384643  0.0151446340 -0.0184883199 
0.00

Now I want to plot a histogram:

 histo = hist(r,freq=F,breaks=50)
 

Re: [R] generating an expression for a formula automatically

2006-08-26 Thread Prof Brian Ripley
On Fri, 25 Aug 2006, Maria Montez wrote:

 Thank you for your answers yesterday. I now have another question!
 
 Suppose that instead of creating a formula for a regression model I 
 simply wanted to add the variables. I believe I cannot use the 
 as.formula anymore. Again I tried to use expression to no avail. I get 
 an expression but I can't use it.
 
 fit.sum - function(x) {
 fo - expression(paste(x, collapse = +))
eval( fo)
 }
 fit.sum(c(x1,x2))
 
 Basically what I need is to learn how to use variables when what is 
 given to me are their names (character list).

I think we do need to tell you that parse(text=) is how to turn a 
character vector into an expression, although it is rarely a good way (see 
the fortune in the 'fortunes' package about it) and you need to be careful 
where you evaluate:

fit.sum - function(x) eval.parent(parse(text=paste(x, collapse = +)))

As an alternative, the answer to

 Basically what I need is to learn how to use variables when what is
 given to me are their names (character list).

is most often 'get'.  So for two objects

fit.sum2 - function(x) {
   env - parent.frame()
   do.call(+, lapply(x, get, envir=env))
}

but again you need to be careful where you get() from.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] Memory usage decreases drastically after save workspace, quit, restart, load workspace

2006-08-26 Thread Prof Brian Ripley
On Sat, 26 Aug 2006, Klaus Thul wrote:

 Dear all,
 
 I have the following problem:
 
   - I have written a program in R which runs out of physical memory  
 on my MacBook Pro with 1.5 GB RAM

How does R know about physical memory on a virtual-memory OS?  I presume 
the symptom is swapping by your OS, but how do you attribute that to R?

   - The memory usage is much higher then I would expect from the  
 actual data in my global environment
 
   - When I save the workspace, quit R, restart R and load the  
 workspace again, memory usage is much less then before
 (~50 MB instead of ~1.5 GB), although nothing seems to be missing.
 
   - It doesn't seem to be possible to reduce the amount of memory  
 used by calling gc()
 
   - the behavior is independent of if I use R in command line or from  
 GUI, so I can exclude a problem of the Mac GUI
 
 Any suggestions what the problem might be or how I could debug this?

How are you measuring memory usage?

If this is by gc(), note that lazyloading is one-way, and memory increases 
after you use a lazyloaded object in a session.  This can be dramatic 
where datasets are involved.

If you are asking the OS about memory usage, return of memory to the OS is 
a rather haphazard and OS-specific issue.  On some OSes virtual memory is 
not actually reclaimed from applications until it is needed (or ever).

It is certainly possible that something you are using has a memory leak, 
but valgrind has been used to plug those on the most-used parts of R.


I at least need more precise indications of what you observed to be able 
to comment more.

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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 iteratively extract elements out of a list

2006-08-26 Thread Patrick Burns
  sub.m - lapply(m, function(x)x[x2])
  sub.m
[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

  sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))]
[[1]]
[1] 3 4

[[2]]
[1] 4 5

  sub4.m - lapply(m, function(x)x[x4])
  sub4.m[unlist(lapply(sub4.m, function(x) length(x)  0))]
[[1]]
[1] 5


Patrick Burns
[EMAIL PROTECTED]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and A Guide for the Unwilling S User)

xpRt.wannabe wrote:

Yet another question:

Let's say I do the following:

set.seed(123)
tmpf - function(){
x - rpois(rpois(1,4),2)
}
n - 3
m - replicate(n, tmpf())
m
sub.m - lapply(m, function(x)x[x2])

'sub.m' gives me:

[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

The question is:  What do I need to do such that I can extract
componets of length 2 in 'sub.m' into another sublist, which would
look like this:

[[1]]
[1] 3 4

[[2]]
[1] 4 5

I think that's all the questions I can think of -- for now.

Many, many thanks!!!

On 8/25/06, xpRt. wannabe [EMAIL PROTECTED] wrote:
  

Jim and Patrick,

Both of you made the same suggestion, which works great!

A follow-up question: Suppose I change the condition 'x2' in 'lapply'
to 'x4', as follows:

set.seed(123)
tmpf - function() {
x - rpois(rpois(1,4),2)
}
n - 3
m - replicate(n,tmpf())
m
sub.m - lapply(m, function(x)x[x4])  # was x2

As a result, I'd get:



sub.m
  

[[1]]
numeric(0)

[[2]]
[1] 5

[[3]]
numeric(0)

However, what would I need to do such that 'sub.m' contains only the
non-zero length component; namely, the 'sub.m[[2]]'?  In essence, I'd
like to drop all the components of zero length such that 'sub.m'
results in:

[[1]]
[1] 5

My best effort was to use 'lapply' again:

lapply(sub.m, function(x)x[length(x)0])

which still gives me:

[[1]]
numeric(0)

[[2]]
[1] 5

[[3]]
numeric(0)

Again, any help would be greately appreciated.

p.s. Sorry to bug you again.   I should have thought through a little
more prior to composing an example that would represent all possible
scenarios.

On 8/25/06, jim holtman [EMAIL PROTECTED] wrote:


try this:

  

set.seed(123)
tmpf - function() {


+ x - rpois(rpois(1,4),2)
+ }
  

n - 3
m - replicate(n,tmpf())
m


[[1]]
[1] 3 2 4

[[2]]
[1] 0 2 4 2 2 5 2

[[3]]
[1] 2 0 4 1 0

  

lapply(m, function(x)x[x2])


[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

  

On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote:
  

Dear List,

The following code produces a list, which is what I what:

set.seed(123)
tmpf - function() {
x - rpois(rpois(1,4),2)
}
n - 3
m - replicate(n,tmpf())
m

[[1]]
[1] 3 2 4

[[2]]
[1] 0 2 4 2 2 5 2

[[3]]
[1] 2 0 4 1 0


Now I need something that would to extract iteratively (or as many
times as
the size of 'n') the values that are greater than 2 in each component
of
'm' into another list such that the sub-list would be:

[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

Below is what I tried:

for(i in 1:3)
sub.list - lapply(m,subset,m[[i]]2)



sub.list
  

which gives me something different from what I want:

[[1]]
[1] 4

[[2]]
[1] 4

[[3]]
[1] 4

Any help would be appreciated.



version
  

_
platform i386-pc-mingw32
arch i386
os   mingw32
system   i386, mingw32
status
major2
minor2.1
year 2005
month12
day  20
svn rev  36812
language R

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



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

What is the problem you are trying to solve?

  


__
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-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 Friedrich Leisch
 On Fri, 25 Aug 2006 11:05:48 -0700 (PDT),
 Thomas Harte (TH) wrote:

   --- 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:
   label=first.ar.1, results=hide=
   # 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

**

results=hide=
sp- make.ar.1(alpha=.5, n=800)
@

ar,fig=true,width=20,height=5,include=false=
plot(sp, type=l, col=blue)
@

\begin{figure}
  \includegraphics[width=14.5cm]{myprefix-ar}
\end{figure}

**

???

Best,

-- 
---
Prof. Dr. Friedrich Leisch 

Institut für Statistik  Tel: (+49 89) 2180 3165
Ludwig-Maximilians-Universität  Fax: (+49 89) 2180 5308
Ludwigstraße 33
D-80539 München http://www.stat.uni-muenchen.de/~leisch

__
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] Problem on Histogtam

2006-08-26 Thread jim holtman
try thie:

r - rnorm(100,4,1)
hist(r, freq=FALSE)
r.f - function(x)dnorm(x, mean(r), sd(r))
curve(r.f, from=min(r), to=max(r), add=TRUE, col='red')


On 8/26/06, stat stat [EMAIL PROTECTED] wrote:
 Dear all,

 May be  question seems trivial for most of the R
 users, but really at least for me, this comes out to
 be very problematic.

 Suppose I have the following data:

  r
  [1] -0.0008179960 -0.0277968529 -0.0105731583
 -0.0254050262  0.0321847131  0.0328170674
  [7]  0.0431894392 -0.0217614918 -0.0218366946
 0.0048939739 -0.0012212499  0.0032533579
  [13] -0.0081533269 -0.0098725606 -0.0099710008
 -0.0130281313  0.0121927094  0.0029209284
  [19] -0.0206279678 -0.0210624593 -0.0100415600
 -0.0266801063 -0.0241744954  0.0178453603
  [25]  0.0452204703  0.0133478839  0.0274220937
 0.0091135676  0.0380304730 -0.0087719861
  [31] -0.0412893310 -0.0092243841 -0.0016863410
 -0.0080491856 -0.0293504020  0.0411898638
  [37] -0.0037902762 -0.0076239251  0.0193690266
 -0.0012518257  0.0259647803  0.0241169245
  [43]  0.0446589449  0.0536073638 -0.0028839241
 0.0168251687  0.0095390230 -0.0213227695
  [49] -0.0519741069 -0.0287872498 -0.0035108287
 0.0197336642 -0.0065347148 -0.0011576308
  [55] -0.0085304899  0.0162228355  0.0087736592
 0.0007593015  0.0113208756  0.0229980462
  [61]  0.0370746078  0.0679558204  0.0401218485
 -0.005759  0.0188922496 -0.0230330422
  [67] -0.0261938985 -0.0503969590 -0.0069156569
 -0.0031277176 -0.0235967983 -0.0100287373
  [73] -0.0014409224 -0.0307491283 -0.0240794177
 -0.0091813958  0.0076570053  0.0621042105
  [79]  0.0156475454  0.0353589695  0.0078032627
 0.0070719271  0.0026809668 -0.0111056164
  [85] -0.0088375830  0.0128902735  0.0279180644
 0.0268407382 -0.0038363218 -0.0194055249
  [91] -0.0284881803  0.0016786977 -0.0145297369
 0.0081356381 -0.0187430943  0.0123078477
  [97]  0.0148400700 -0.0070552955  0.0103975446
 0.0109508312  0.0147422817  0.0218728566
 [103] -0.0041447533 -0.0096308931  0.0289322358
 0.0216995495  0.0499431039  0.0717506367
 [109]  0.0032529169 -0.0040678022 -0.0331522073
 -0.0164263110 -0.0322093707  0.0542379373
 [115]  0.0259181864  0.0387076215  0.0044415488
 -0.0078513881  0.0411792612  0.0062837960
 [121] -0.0243481467  0.0328312315 -0.0520127424
 -0.0137024542 -0.0367509610 -0.0149753215
 [127]  0.0256530410  0.0376798081  0.0106952891
 0.0525638932  0.0281572000  0.0324963843
 [133] -0.0170584812 -0.0063837550  0.0294482602
 -0.0118152654 -0.0169216729  0.0061436866
 [139] -0.0245630568  0.0093716894 -0.0122730454
 0.0234516610 -0.0090283341  0.0059488575
 [145] -0.0104937762 -0.0123026519  0.0263488297
 -0.0087855249  0.0021441342  0.0204930031
 [151] -0.0079588435 -0.0021173987  0.0135674596
 0.0053296384 -0.0004623209  0.0199159610
 [157] -0.0034056112 -0.0052445679 -0.0142760886
 -0.0158956129 -0.0137605967 -0.0007169316
 [163]  0.0236250062  0.0002334540  0.0007000350
 -0.0023353584 -0.0210285362 -0.0178275018
 [169] -0.0663226429  0.0098217383 -0.0111212836
 -0.0597472435  0.0371266051 -0.0247790755
 [175]  0.0337833736  0.0265364166 -0.0160458929
 0.0085725944  0.0161645598  0.0700273748
 [181] -0.0308458190  0.0362889752 -0.0195430433
 0.0284716741  0.0097766142 -0.0037131628
 [187]  0.0275183610  0.0276618128  0.0109410282
 -0.0302685208  0.0191074465 -0.0086197904
 [193]  0.0013309674  0.0169253408 -0.0010903937
 0.0073913380  0.0330203607  0.0116667990
 [199] -0.0099917567  0.0035501760 -0.0231980036
 0.0023441673 -0.0150120769 -0.0165581125
 [205] -0.0101568548 -0.0031118051 -0.0051333669
 0.0312800833 -0.0050005540 -0.0109578129
 [211] -0.0222825618 -0.0388242167 -0.0037549915
 -0.0185102580 -0.0120483385  0.0336106003
 [217]  0.0146599484  0.0146757965  0.0117675040
 0.0196041030 -0.0030932415 -0.0024371343
 [223] -0.0158751238 -0.0145292000  0.0050182587
 0.0061245513  0.0139234228 -0.0116645558
 [229] -0.0475950303 -0.0346688548  0.0041559771
 -0.0246987117  0.0195623423  0.0167785091
 [235] -0.0133513332 -0.0113051093  0.0215167225
 -0.0009680543 -0.0141431277 -0.0150936925
 [241] -0.0183676066 -0.0117498159  0.0165629981
 -0.0248189789 -0.0133004960 -0.0132137229
 [247] -0.0429413799 -0.0055694937  0.0005589715
 0.0168908903  0.0321518982  0.0007973422
 [253]  0.0573060001 -0.0078076206 -0.0253502245
 0.0210426035  0.0025361414 -0.0091603694
 [259] -0.0064111003 -0.0126862543 -0.0067974118
 0.0023581827 -0.0359703092  0.0185464260
 [265] -0.0007992541 -0.0193762790  0.0225694695
 0.0052994295 -0.0039719369  0.0144872809
 [271]  0.0002614721 -0.0076105865 -0.0153971717
 0.0056022555 -0.0104292963 -0.0189965007
 [277]  0.0079138085  0.0254944708 -0.0165646320
 -0.0162958522 -0.0107365106  0.0049696403
 [283]  0.0041225838  0.00 -0.0035719230
 -0.0200173489 -0.0022490873 -0.0039481157
 [289] -0.0182497223  0.0157033919 -0.0154156635
 -0.0023041485 -0.0028876717  0.00
 [295] -0.0195655241  0.0210104023 -0.0020234145
 -0.0026075634  0.0149729259  0.0045623119
 [301]  0.0431521368  0.0228966231 -0.0077530130

Re: [R] Modifying the embed-results

2006-08-26 Thread Gabor Grothendieck
You can replace the for with lapply like this:

VECTOR - c(0, 3, 6, 3, 11, 2, 11, 4, 3, 4, 7, 7, 6, 4, 8)
f - function(i) unique(tail(VECTOR, length(VECTOR)-i+1))[1:5]
out - do.call(rbind, lapply(seq(along = VECTOR), f))
na.omit(rbind(rep(NA, 5), out))

Note that  a matrix with zero rows is returned in the case
that VECTOR has zero length and in the case that VECTOR
has fewer than 5 unique elements.


On 8/26/06, Atte Tenkanen [EMAIL PROTECTED] wrote:
 Again my example was't very clear: there were not enough same numbers in the 
 VECTOR.
 What I need is something like this:

 VECTOR-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8)
 MATRIX-c()
 for(i in 1:length(VECTOR)){
v-(unique(VECTOR[i:length(VECTOR)])[1:5])
MATRIX-rbind(MATRIX,v)
 }

 MATRIX-na.omit(MATRIX)
 MATRIX

  data.frame(MATRIX, row.names=NULL)
  X1 X2 X3 X4 X5
 1   0  3  6 11  2
 2   3  6 11  2  4
 3   6  3 11  2  4
 4   3 11  2  4  7
 5  11  2  4  3  7
 6   2 11  4  3  7
 7  11  4  3  7  6
 8   4  3  7  6  8
 9   3  4  7  6  8

 So, there are no duplicates in rows.
 VECTOR is always scanned forward as long as the number of  items (here 5) 
 becomes full.

 Atte

  Try:
 
  embed(VECTOR, 5)[,5:1]
 
  On 8/25/06, Atte Tenkanen [EMAIL PROTECTED] wrote:
   Hi,
  
   Here is a vector and the result from the embed-command:
  
   VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8)
  
embed(VECTOR, dimension=5)
[,1] [,2] [,3] [,4] [,5]
[1,]   113630
[2,]2   11363
[3,]42   1136
[4,]342   113
[5,]7342   11
[6,]67342
[7,]46734
[8,]54673
[9,]   105467
   [10,]2   10546
   [11,]32   1054
   [12,]532   105
   [13,]8532   10
  
   Is there a way to little modify the algorithm so that the result
  looks like this:
  
   [1]  0  3  6 11  2 - beginning from the first number of the VECTOR
   [1]  3  6 11  2  4 - beginning from the second number of the
  VECTOR etc
   [1]  6  3 11  2  4
   [1]  3 11  2  4  7
   [1] 11  2  4  3  7
   [1] 2 4 3 7 6
   [1] 4 3 7 6 5
   [1] 3 7 6 4 5
   [1]  7  6  4  5 10
   [1]  6  4  5 10  2
   [1]  4  5 10  2  3
   [1]  5 10  2  3  8
   [1] 10  2  3  5  8
  
   Every row consists of next five unique(!) member of the VECTOR. I
  made this example result with a time consuming algorithm which
  uses for-loops
   and whiles.
  
   How to do this better??
  
   Thanks in advance!
  
   Atte Tenkanen
   University of Turku
  
   __
   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-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:
label=first.ar.1, results=hide=
  # 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
 
 **
 
 results=hide=
 sp- make.ar.1(alpha=.5, n=800)
 @
 
 ar,fig=true,width=20,height=5,include=false=
 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.


[R] problems with loop

2006-08-26 Thread Simon Ruegg
Dear all,

 

I am trying to evaluate the optimisation behaviour of a function. Originally
I have optimised a model with real data and got a set of parameters. Now I
am creating simulated data sets based on these estimates. With these
simulations I am estimating the parameters again to see how variable the
estimation is. To this end I have written a loop which should generate a new
simulated data set each time. However, the optimisation algorithm, which
works fine if only one data set is used, does not recognise the simulated
data in the loop. Can anyone tell me where the error is? The code is below.

 

Thanks for your help

 

Simon

 

 

# The loop in which nothing works anymore. The NLL in the optim function
appears not to recognise the new data set. The functions used by this loop
are given below.

 

sim.estim=function(r)

{

  res=matrix(nrow=r,ncol=5)

  for (s in 1:r)

  {

new=new.set()

 
Min=optim(c(0.5,0.5,0.01,0.1),NLL,method=L-BFGS-B,lower=c(0,0,0,0))

res[s,1]=Min$par[1]

res[s,2]=Min$par[2]

res[s,3]=Min$par[3]

res[s,4]=Min$par[4]

res[s,5]=Min$value[1]

  }

return(res)

}

 

 

 

# function to create a new data set. Works fine on its own

 

new.set=function()

{

  tmp=matrix(nrow=510,ncol=2)

  v=numeric()

  sim=Model_1(1.2761707, 0.1953354, 2.7351930, 0.1032929)

 

  tmp[,1]=sample(sim[,1],510,replace=T)

  v=runif(510,0,1)

 

  for (k in 1 : 510)

  {

for (n in 1 : length(sim[,1]))

{

  if (tmp[k,1]==sim[n,1]  v[k]=sim[n,2]){tmp[k,2]=1}

  if(tmp[k,1]==sim[n,1]  v[k]sim[n,2]) {tmp[k,2]=0}

}

  }

return(tmp)

}

 

 

# The model to be fitted

 

Model_1=function(beta0_mod,mu_mod,k_mod,I_mod)

{

  parms = c(beta0=beta0_mod, mu=mu_mod, k=k_mod)

  my.atol = 1e-6

 
times1=c(0,0.002884615,0.003846154,0.005769231,0.009615385,0.01,0.019230769)


  times2=c(0.028846154,0.038461538,0.057692308,
0.076923077,0.08333,0.096153846)

 
times3=c(0.115384615,0.125,0.134615385,0.16667,0.25,0.41667,0.5) 

  times4=c(0.5833,0.7,0.75,0.8333,0.91667,1)

  times5=c( 1.16667,1.25,1.5,1.6,1.75,2,2.5)

  times6=seq(3,20)

  times = c(times1,times2,times3,times4,times5,times6)

  ODE_sys = function(t, x, w)  #w=c(beta0,mu,k)

 {

  I=x[1]

   

dI=-w[mu]*I+w[beta0]*exp(-w[k]*t)*(1-I)



list(c(dI),c(S=1-I))

 }

 

  output = lsoda(c(I_mod),times,ODE_sys, parms, rtol=1e-6, atol=
my.atol)

  return(output)

}

 

# The negative log likelihood of this model given a data set new. This
works fine if it is used in the optim() routine with only one data set.

 

NLL=function(coef) 

{  

  beta0=coef[1]

  mu=coef[2]

  k=coef[3]

  I=coef[4]



  data_sim=Model_1(beta0,mu,k,I)

 

  LLsum=0  #log likelihood sum

  

  for (j in 1:length(data_sim[,1]))

{

if (data_sim[j,2]0 || data_sim[j,3]0)

{return(1500)}

 

for (i in 1:length(new[,1]))

{

  if (new[i,1]==data_sim[j,1]  is.na(new[i,1])==F 
is.na(data_sim[j,1])==F)

  # if age of real data = age of simulated model and not
equal to NA

  {

if (is.na(new[i,2])==F  is.na(data_sim[j,2])==F 
is.na(data_sim[j,3])==F)

# if state of real data and state of simulated model
are not equal to NA

{

 tmp1=new[i,2]*log(data_sim[j,2])

 tmp2=(1-new[i,2])*log(data_sim[j,3])

 

 if (tmp1==-Inf || tmp1==NaN){tmp1=-700}

 if (tmp2==-Inf || tmp2==NaN){tmp2=-700}

 LLsum=LLsum+tmp1+tmp2

}

  }

  

}

  }

 

return(-LLsum)

}

 

 

 

 

 



Simon Ruegg

Dr.med.vet.,  PhD student

Institute for Parasitology

Winterthurstr. 266a

8057 Zurich

Switzerland

 

phone: +41 44 635 85 93

fax: +41 44 635 89 07

e-mail: [EMAIL PROTECTED]

 


[[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
and provide commented, minimal, self-contained, reproducible code.


[R] Adding a footnote in plot-window in R

2006-08-26 Thread Arun Kumar Saha
Dear all R users,

Suppose,
 x = rnorm(1000)
 y = rt(1000,3)
 plot(range(1:1000),range(x,y),type=n,xlab=NA,ylab=NA)
 lines(x,col=red)
 lines(y,col=blue)
Now I want to put a footnote in the plot window to tell that RED lines
represents the random numbers from normal-dist and blue line represents the
random numbers from t-dist.

Can anyone please tell me how to do that?

Sincerely yours,
Arun

[[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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a footnote in plot-window in R

2006-08-26 Thread Rolf Turner
?legend

__
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 Friedrich Leisch
 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? Your first posting suggested to me more the other way
round ...

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

Best,
Fritz

__
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 iteratively extract elements out of a list

2006-08-26 Thread Mark Lyman
lapply(m,function(x)x[x2])
[[1]]
[1] 3 4

[[2]]
[1] 4 5

[[3]]
[1] 4

__
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] Implementing EM Algorithm in R!

2006-08-26 Thread Pushkar Kumar
Hi All,
I need some help in how one can implement maximumlikelihood estimation for
models with discrete hidden variables in EM in R.

Regards

[[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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to iteratively extract elements out of a list

2006-08-26 Thread xpRt.wannabe
Thank you!

On 8/26/06, Patrick Burns [EMAIL PROTECTED] wrote:
   sub.m - lapply(m, function(x)x[x2])
   sub.m
 [[1]]
 [1] 3 4

 [[2]]
 [1] 4 5

 [[3]]
 [1] 4

   sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))]
 [[1]]
 [1] 3 4

 [[2]]
 [1] 4 5

   sub4.m - lapply(m, function(x)x[x4])
   sub4.m[unlist(lapply(sub4.m, function(x) length(x)  0))]
 [[1]]
 [1] 5


 Patrick Burns
 [EMAIL PROTECTED]
 +44 (0)20 8525 0696
 http://www.burns-stat.com
 (home of S Poetry and A Guide for the Unwilling S User)

 xpRt.wannabe wrote:

 Yet another question:
 
 Let's say I do the following:
 
 set.seed(123)
 tmpf - function(){
 x - rpois(rpois(1,4),2)
 }
 n - 3
 m - replicate(n, tmpf())
 m
 sub.m - lapply(m, function(x)x[x2])
 
 'sub.m' gives me:
 
 [[1]]
 [1] 3 4
 
 [[2]]
 [1] 4 5
 
 [[3]]
 [1] 4
 
 The question is:  What do I need to do such that I can extract
 componets of length 2 in 'sub.m' into another sublist, which would
 look like this:
 
 [[1]]
 [1] 3 4
 
 [[2]]
 [1] 4 5
 
 I think that's all the questions I can think of -- for now.
 
 Many, many thanks!!!
 
 On 8/25/06, xpRt. wannabe [EMAIL PROTECTED] wrote:
 
 
 Jim and Patrick,
 
 Both of you made the same suggestion, which works great!
 
 A follow-up question: Suppose I change the condition 'x2' in 'lapply'
 to 'x4', as follows:
 
 set.seed(123)
 tmpf - function() {
 x - rpois(rpois(1,4),2)
 }
 n - 3
 m - replicate(n,tmpf())
 m
 sub.m - lapply(m, function(x)x[x4])  # was x2
 
 As a result, I'd get:
 
 
 
 sub.m
 
 
 [[1]]
 numeric(0)
 
 [[2]]
 [1] 5
 
 [[3]]
 numeric(0)
 
 However, what would I need to do such that 'sub.m' contains only the
 non-zero length component; namely, the 'sub.m[[2]]'?  In essence, I'd
 like to drop all the components of zero length such that 'sub.m'
 results in:
 
 [[1]]
 [1] 5
 
 My best effort was to use 'lapply' again:
 
 lapply(sub.m, function(x)x[length(x)0])
 
 which still gives me:
 
 [[1]]
 numeric(0)
 
 [[2]]
 [1] 5
 
 [[3]]
 numeric(0)
 
 Again, any help would be greately appreciated.
 
 p.s. Sorry to bug you again.   I should have thought through a little
 more prior to composing an example that would represent all possible
 scenarios.
 
 On 8/25/06, jim holtman [EMAIL PROTECTED] wrote:
 
 
 try this:
 
 
 
 set.seed(123)
 tmpf - function() {
 
 
 + x - rpois(rpois(1,4),2)
 + }
 
 
 n - 3
 m - replicate(n,tmpf())
 m
 
 
 [[1]]
 [1] 3 2 4
 
 [[2]]
 [1] 0 2 4 2 2 5 2
 
 [[3]]
 [1] 2 0 4 1 0
 
 
 
 lapply(m, function(x)x[x2])
 
 
 [[1]]
 [1] 3 4
 
 [[2]]
 [1] 4 5
 
 [[3]]
 [1] 4
 
 
 
 On 8/25/06, xpRt.wannabe [EMAIL PROTECTED] wrote:
 
 
 Dear List,
 
 The following code produces a list, which is what I what:
 
 set.seed(123)
 tmpf - function() {
 x - rpois(rpois(1,4),2)
 }
 n - 3
 m - replicate(n,tmpf())
 m
 
 [[1]]
 [1] 3 2 4
 
 [[2]]
 [1] 0 2 4 2 2 5 2
 
 [[3]]
 [1] 2 0 4 1 0
 
 
 Now I need something that would to extract iteratively (or as many
 times as
 the size of 'n') the values that are greater than 2 in each component
 of
 'm' into another list such that the sub-list would be:
 
 [[1]]
 [1] 3 4
 
 [[2]]
 [1] 4 5
 
 [[3]]
 [1] 4
 
 Below is what I tried:
 
 for(i in 1:3)
 sub.list - lapply(m,subset,m[[i]]2)
 
 
 
 sub.list
 
 
 which gives me something different from what I want:
 
 [[1]]
 [1] 4
 
 [[2]]
 [1] 4
 
 [[3]]
 [1] 4
 
 Any help would be appreciated.
 
 
 
 version
 
 
 _
 platform i386-pc-mingw32
 arch i386
 os   mingw32
 system   i386, mingw32
 status
 major2
 minor2.1
 year 2005
 month12
 day  20
 svn rev  36812
 language R
 
 __
 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.
 
 
 
 --
 Jim Holtman
 Cincinnati, OH
 +1 513 646 9390
 
 What is the problem you are trying to solve?
 
 
 
 
 __
 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-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] problems with loop

2006-08-26 Thread Reid Huntsinger
I think it's a scoping problem. Your function NLL() looks for new in 
the environment in which NLL() was defined, but you generate your 
simulated datasets in a different environment (local to sim.estim()). 
There are a number of ways to deal with this:
- pass the dataset as a parameter to NLL()
- manipulate environments with assign() or -
- put new in its own environment and use assign() and get()
- define NLL() within sim.estim() after generating new, like NLL - 
negative.log.likelihood(new) where negative.log.likelihood() is a 
function like
negative.log.likelihood - function(new) {
NLL - function(coef) {
... (your definition here)...
}
NLL
}
This defines a new NLL() each time, using the dataset new just generated.

Reid Huntsinger

Simon Ruegg wrote:

Dear all,

 

I am trying to evaluate the optimisation behaviour of a function. Originally
I have optimised a model with real data and got a set of parameters. Now I
am creating simulated data sets based on these estimates. With these
simulations I am estimating the parameters again to see how variable the
estimation is. To this end I have written a loop which should generate a new
simulated data set each time. However, the optimisation algorithm, which
works fine if only one data set is used, does not recognise the simulated
data in the loop. Can anyone tell me where the error is? The code is below.

 

Thanks for your help

 

Simon

 

 

# The loop in which nothing works anymore. The NLL in the optim function
appears not to recognise the new data set. The functions used by this loop
are given below.

 

sim.estim=function(r)

{

  res=matrix(nrow=r,ncol=5)

  for (s in 1:r)

  {

new=new.set()

 
Min=optim(c(0.5,0.5,0.01,0.1),NLL,method=L-BFGS-B,lower=c(0,0,0,0))

res[s,1]=Min$par[1]

res[s,2]=Min$par[2]

res[s,3]=Min$par[3]

res[s,4]=Min$par[4]

res[s,5]=Min$value[1]

  }

return(res)

}

 

 

 

# function to create a new data set. Works fine on its own

 

new.set=function()

{

  tmp=matrix(nrow=510,ncol=2)

  v=numeric()

  sim=Model_1(1.2761707, 0.1953354, 2.7351930, 0.1032929)

 

  tmp[,1]=sample(sim[,1],510,replace=T)

  v=runif(510,0,1)

 

  for (k in 1 : 510)

  {

for (n in 1 : length(sim[,1]))

{

  if (tmp[k,1]==sim[n,1]  v[k]=sim[n,2]){tmp[k,2]=1}

  if(tmp[k,1]==sim[n,1]  v[k]sim[n,2]) {tmp[k,2]=0}

}

  }

return(tmp)

}

 

 

# The model to be fitted

 

Model_1=function(beta0_mod,mu_mod,k_mod,I_mod)

{

  parms = c(beta0=beta0_mod, mu=mu_mod, k=k_mod)

  my.atol = 1e-6

 
times1=c(0,0.002884615,0.003846154,0.005769231,0.009615385,0.01,0.019230769)


  times2=c(0.028846154,0.038461538,0.057692308,
0.076923077,0.08333,0.096153846)

 
times3=c(0.115384615,0.125,0.134615385,0.16667,0.25,0.41667,0.5) 

  times4=c(0.5833,0.7,0.75,0.8333,0.91667,1)

  times5=c( 1.16667,1.25,1.5,1.6,1.75,2,2.5)

  times6=seq(3,20)

  times = c(times1,times2,times3,times4,times5,times6)

  ODE_sys = function(t, x, w)  #w=c(beta0,mu,k)

 {

  I=x[1]

   

dI=-w[mu]*I+w[beta0]*exp(-w[k]*t)*(1-I)



list(c(dI),c(S=1-I))

 }

 

  output = lsoda(c(I_mod),times,ODE_sys, parms, rtol=1e-6, atol=
my.atol)

  return(output)

}

 

# The negative log likelihood of this model given a data set new. This
works fine if it is used in the optim() routine with only one data set.

 

NLL=function(coef) 

{  

  beta0=coef[1]

  mu=coef[2]

  k=coef[3]

  I=coef[4]



  data_sim=Model_1(beta0,mu,k,I)

 

  LLsum=0  #log likelihood sum

  

  for (j in 1:length(data_sim[,1]))

{

if (data_sim[j,2]0 || data_sim[j,3]0)

{return(1500)}

 

for (i in 1:length(new[,1]))

{

  if (new[i,1]==data_sim[j,1]  is.na(new[i,1])==F 
is.na(data_sim[j,1])==F)

  # if age of real data = age of simulated model and not
equal to NA

  {

if (is.na(new[i,2])==F  is.na(data_sim[j,2])==F 
is.na(data_sim[j,3])==F)

# if state of real data and state of simulated model
are not equal to NA

{

 tmp1=new[i,2]*log(data_sim[j,2])

 tmp2=(1-new[i,2])*log(data_sim[j,3])

 

 if (tmp1==-Inf || tmp1==NaN){tmp1=-700}

 if (tmp2==-Inf || tmp2==NaN){tmp2=-700}

 LLsum=LLsum+tmp1+tmp2

}

  }

  

}

  }

 

return(-LLsum)

}

 

 

 

 

 




[R] for() loop question

2006-08-26 Thread Wensui Liu
Dear Lister,

If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
to loop through each number in x one by one?

Thank you so much!

wensui

[[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
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] for() loop question

2006-08-26 Thread Marc Schwartz
On Sat, 2006-08-26 at 13:06 -0400, Wensui Liu wrote:
 Dear Lister,
 
 If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
 to loop through each number in x one by one?
 
 Thank you so much!
 
 wensui

Two options:

x - c(0.1, 0.5, 0.6)

 for (i in x) {print (i)}
[1] 0.1
[1] 0.5
[1] 0.6


 for (i in seq(along = x)) {print (x[i])}
[1] 0.1
[1] 0.5
[1] 0.6


Which approach you take tends to depends upon what else you are doing
within the loop.

I would also take a look at ?sapply, depending up what is it you are
doing.

HTH,

Marc Schwartz

__
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] for() loop question

2006-08-26 Thread MARK LEEDS
let us know what you want to do because the beauty of R is that, in many 
cases, you may not have to loop.


- Original Message - 
From: Wensui Liu [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Saturday, August 26, 2006 1:06 PM
Subject: [R] for() loop question


 Dear Lister,

 If I have a list of number, say x-c(0.1, 0.5, 0.6...), how to use a for()
 to loop through each number in x one by one?

 Thank you so much!

 wensui

 [[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
 and provide commented, minimal, self-contained, reproducible code.


__
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] Can R compute the expected value of a random variable?

2006-08-26 Thread Paul Smith
Dear All

Can R compute the expected value of a random variable?

Thanks in advance,

Paul

__
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] Can R compute the expected value of a random variable?

2006-08-26 Thread Mike Nielsen
Yes.

On 8/26/06, Paul Smith [EMAIL PROTECTED] wrote:
 Dear All

 Can R compute the expected value of a random variable?

 Thanks in advance,

 Paul

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



-- 
Regards,

Mike Nielsen

__
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] fitting a gaussian to some x,y data

2006-08-26 Thread Michael Koppelman

Success! The line I needed was:

gcoeffs -nls(y~(a/b)*exp(-(x-c)^2/(2*b^2)),start=list 
(a=0.4,b=2,c=-10), trace=TRUE)


I also needed to provide good guesses for a, b and c. The attached  
PNG should explain what I was going after, which is the line in the  
center of that curve. I am sorry for my confusing and perhaps  
incorrect usage of terminology.


Cheers,
Michael



__
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] Implementing EM Algorithm in R!

2006-08-26 Thread Ritwik Sinha
Implementing the EM algorithm will be easy if you know what the algorithm is
for your particular problem. This will be very specific to your problem. The
trick is to augment your data to get something for which there is an easy ML
estimate. I do not believe there is a unique recipe to perform the EM
algorithm for any problem.

On 8/26/06, Pushkar Kumar [EMAIL PROTECTED] wrote:

 Hi All,
 I need some help in how one can implement maximumlikelihood estimation for
 models with discrete hidden variables in EM in R.

 Regards

 [[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
 and provide commented, minimal, self-contained, reproducible code.




-- 
Ritwik Sinha
Graduate Student
Epidemiology and Biostatistics
Case Western Reserve University

http://darwin.cwru.edu/~rsinha

[[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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] fitting a gaussian to some x,y data

2006-08-26 Thread Michael Koppelman
Whoops, I forgot to add, many thanks to all who replied publicly and  
privately. I am very appreciative of all comments and suggestions.  
Mark Leeds was especially kind in terms of clarifying why my  
description of the situation was so confusing.

Cheers,
Michael

On Aug 26, 2006, at 2:51 PM, Michael Koppelman wrote:

 Success! The line I needed was:

 gcoeffs -nls(y~(a/b)*exp(-(x-c)^2/(2*b^2)),start=list 
 (a=0.4,b=2,c=-10), trace=TRUE)

 I also needed to provide good guesses for a, b and c. The attached  
 PNG should explain what I was going after, which is the line in the  
 center of that curve. I am sorry for my confusing and perhaps  
 incorrect usage of terminology.

 Cheers,
 Michael

__
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] boxplot( ..args.., axes=FALSE, frame.plot=TRUE) doesn't frame the plot

2006-08-26 Thread Sego, Landon H
I'm running R 2.3.1 on Windows.
 
When calling boxplot(), shouldn't the axes and frame.plot arguments
get passed down to bxp()?
 
A specific example where the plot is not framed (but seems like it
should be):
 
X - data.frame(x=as.factor(rep(c(1,2,3), 10)), y=rnorm(30))
boxplot(y~x, data=X, frame.plot=TRUE, axes=FALSE)
 
And this doesn't seem to affect the default at all:
 
boxplot(y~x, data=X, frame.plot=FALSE)
 
However, this does the trick:
 
y - boxplot(y~x, data=X, plot=FALSE)
bxp(y, frame.plot=TRUE, axes=FALSE)
 
Any ideas?

[[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
and provide commented, minimal, self-contained, reproducible code.


[R] Type II and III sum of square in Anova (R, car package)

2006-08-26 Thread Amasco Miralisus
Hello everybody,

I have some questions on ANOVA in general and on ANOVA in R particularly.
I am not Statistician, therefore I would be very appreciated if you answer
it in a simple way.

1. First of all, more general question. Standard anova() function for lm()
or aov() models in R implements Type I sum of squares (sequential), which
is not well suited for unbalanced ANOVA. Therefore it is better to use
Anova() function from car package, which was programmed by John Fox to use
Type II and Type III sum of squares. Did I get the point?

2. Now more specific question. Type II sum of squares is not well suited
for unbalanced ANOVA designs too (as stated in STATISTICA help), therefore
the general rule of thumb is to use Anova() function using Type II SS
only for balanced ANOVA and Anova() function using Type III SS for
unbalanced ANOVA? Is this correct interpretation?

3. I have found a post from John Fox in which he wrote that Type III SS
could be misleading in case someone use some contrasts. What is this about?
Could you please advice, when it is appropriate to use Type II and when
Type III SS? I do not use contrasts for comparisons, just general ANOVA
with subsequent Tukey post-hoc comparisons.

Thank you in advance,
Amasco

[[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
and provide commented, minimal, self-contained, reproducible code.


[R] Capture of iterative integration output

2006-08-26 Thread Terry W. Schulz
Hello,

I am a novice R user and am having difficulty retrieving  the values  
from 21 iterations of the R function integrate.
The only way I have found is to do a write.table and then a read.table 
as shown in the code below.  I would rather capture the 21 values inside 
the braces ( sapply might work, but I can't set it up without getting an 
error in function) so I could compute the negative log-likelihood inside 
the braces.  My goal is to then use optim to find the best fitting mu 
and sigma.  In the code that follows mu and sigma are given, but 
normally they would not be known.

Any help on y$value capture would be appreciated.  I would be ecstatic 
for help on implementation of the optim function using finite 
differences for the following code. 

By the way in case anyone is interested function(x) is the 
Poisson-lognormal pdf.

q - read.table(file=c:/Bayes/FigA3-1.prn,header=TRUE)
attach(q)
V - c(volume)
c - c(count)
n - length(count)
mu - -8.202900475
sigma - 1.609437912
for(i in c(1:n))
{
integrand - function(x) 
{(x*V[[i]])^c[[i]]*exp(-x*V[[i]])/factorial(c[[i]])*0.3989422804014327/
   (x*sigma)*exp(-.5*((log(x)-mu)/sigma)^2)}
y - integrate(integrand, lower = 0, upper = Inf)
write.table(y[[1]],file=c:/Bayes/PLNA3-1.out,append=TRUE,
quote=FALSE,row.names=FALSE,col.names=FALSE)
}
z -read.table(file=c:/Bayes/PLNA3-1.out)
z - c(z)
LL - log(c(z$V1))
#negative Log-Likelihood
print(-sum(LL),digits=6)
file.remove(c:/Bayes/PLNA3-1.out)

-- 
Terry W. Schulz
Applied Statistician
1218 Pomegranate Lane
Golden, CO 80401
USA

[EMAIL PROTECTED]
(303) 526-1461

__
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] Permanently changing gui preferences

2006-08-26 Thread David Kaplan
Greetings,

I made changes to my gui preferences and saved them.  When I close and 
then open R, it reverts back to default preferences.  How do I 
permanently change gui preferences?

Thanks in advance.

David


-- 

David Kaplan, Ph.D.
Professor
Department of Educational Psychology
University of Wisconsin - Madison
Educational Sciences, Room 1061
1025 W. Johnson Street
Madison, WI 53706

email: [EMAIL PROTECTED]
Web:   http://www.education.wisc.edu/edpsych/facstaff/kaplan/kaplan.htm
Phone: 608-262-0836
Fax:   608-262-0843

__
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] Importing data from clipboard on Mac OSX

2006-08-26 Thread Rense Nieuwenhuis
Dear R users,

I am trying to get data from the clipboard into R on MacOSX. I tried  
the following, but got an error message:

read.delim(clipboard)
Error in file(file, r) : unable to open connection
In addition: Warning message:
unable to contact X11 display

Obviously, I'm not running R using X11. I'm wondering, can I import  
data from the clipboard on MacosX?

Thanks in advance,

Rense Nieuwenhuis

  version
_
platform   powerpc-apple-darwin8.6.0
arch   powerpc
os darwin8.6.0
system powerpc, darwin8.6.0
status
major  2
minor  3.0
year   2006
month  04
day24
svn rev37909
language   R
version.string Version 2.3.0 (2006-04-24)

__
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 iteratively extract elements out of a list

2006-08-26 Thread Patrick Connolly
On Sat, 26-Aug-2006 at 09:57AM +0100, Patrick Burns wrote:

|   sub.m - lapply(m, function(x)x[x2])
|   sub.m
| [[1]]
| [1] 3 4
| 
| [[2]]
| [1] 4 5
| 
| [[3]]
| [1] 4
| 
|   sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))]
| [[1]]
| [1] 3 4
| 
| [[2]]
| [1] 4 5
| 
|   sub4.m - lapply(m, function(x)x[x4])
|   sub4.m[unlist(lapply(sub4.m, function(x) length(x)  0))]
| [[1]]
| [1] 5

Or slightly shorter in this case:

sub.m[sapply(sub.m, function(x) length(x) == 2)]

etc.



-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~} Great minds discuss ideas
 _( Y )_Middle minds discuss events 
(:_~*~_:)Small minds discuss people  
 (_)-(_)   . Anon
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
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 iteratively extract elements out of a list

2006-08-26 Thread Gabor Grothendieck
On 8/26/06, Patrick Connolly [EMAIL PROTECTED] wrote:
 On Sat, 26-Aug-2006 at 09:57AM +0100, Patrick Burns wrote:

 |   sub.m - lapply(m, function(x)x[x2])
 |   sub.m
 | [[1]]
 | [1] 3 4
 |
 | [[2]]
 | [1] 4 5
 |
 | [[3]]
 | [1] 4
 |
 |   sub.m[unlist(lapply(sub.m, function(x) length(x) == 2))]
 | [[1]]
 | [1] 3 4
 |
 | [[2]]
 | [1] 4 5
 |
 |   sub4.m - lapply(m, function(x)x[x4])
 |   sub4.m[unlist(lapply(sub4.m, function(x) length(x)  0))]
 | [[1]]
 | [1] 5

 Or slightly shorter in this case:

 sub.m[sapply(sub.m, function(x) length(x) == 2)]


or even shorter:

sub.m[sapply(sub.m, length) == 2]

__
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] Capture of iterative integration output

2006-08-26 Thread Terry W. Schulz
Thanks for the reply Sego, Landon H.   The vector is created as you 
say.  However, only the first iteration of integrate is captured.
The other twenty vector values remain at 0.  Integrate output includes 
several attributes and I only want the first one which is under $names 
and called value  

Terry W. Schulz
Applied Statistician
1218 Pomegranate Lane
Golden, CO 80401
USA

[EMAIL PROTECTED]
(303) 526-1461




Sego, Landon H wrote:
 Here's one way to do this:

 Before you begin the loop, define a vector to capture the output.  Then
 each iteration of the loop will be assigned to the corresponding element
 in that vector: 

 capture - double(n) # creates a vector of length n, all with values of
 0

 for (i in 1:n) {

   _ some code _

   capture[i] - integrate( _your arguments_ )$value
   
 }

 capture should be the same as your z.

   
 -Original Message-
 From: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Terry W. Schulz
 Sent: Saturday, August 26, 2006 3:42 PM
 To: r-help@stat.math.ethz.ch
 Subject: [R] Capture of iterative integration output

 Hello,

 I am a novice R user and am having difficulty retrieving  the 
 values from 21 iterations of the R function integrate.
 The only way I have found is to do a write.table and then a 
 read.table as shown in the code below.  I would rather 
 capture the 21 values inside the braces ( sapply might work, 
 but I can't set it up without getting an error in function) 
 so I could compute the negative log-likelihood inside the 
 braces.  My goal is to then use optim to find the best 
 fitting mu and sigma.  In the code that follows mu and sigma 
 are given, but normally they would not be known.

 Any help on y$value capture would be appreciated.  I would be 
 ecstatic for help on implementation of the optim function 
 using finite differences for the following code. 

 By the way in case anyone is interested function(x) is the 
 Poisson-lognormal pdf.

 q - read.table(file=c:/Bayes/FigA3-1.prn,header=TRUE)
 attach(q)
 V - c(volume)
 c - c(count)
 n - length(count)
 mu - -8.202900475
 sigma - 1.609437912
 for(i in c(1:n))
 {
 integrand - function(x)
 {(x*V[[i]])^c[[i]]*exp(-x*V[[i]])/factorial(c[[i]])*0.39894228
 
 04014327/
   
(x*sigma)*exp(-.5*((log(x)-mu)/sigma)^2)}
 y - integrate(integrand, lower = 0, upper = Inf) 
 write.table(y[[1]],file=c:/Bayes/PLNA3-1.out,append=TRUE,
 quote=FALSE,row.names=FALSE,col.names=FALSE)
 }
 z -read.table(file=c:/Bayes/PLNA3-1.out)
 z - c(z)
 LL - log(c(z$V1))
 #negative Log-Likelihood
 print(-sum(LL),digits=6)
 file.remove(c:/Bayes/PLNA3-1.out)

 --
 Terry W. Schulz
 Applied Statistician
 1218 Pomegranate Lane
 Golden, CO 80401
 USA

 [EMAIL PROTECTED]
 (303) 526-1461

 __
 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-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] how to create many objects with sequencial names?

2006-08-26 Thread Wensui Liu
Dear Lister,

Is there a way to create many objects with sequencial names, say lm1,
lm2...lm100?

Thanks.

__
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 create many objects with sequencial names?

2006-08-26 Thread jim holtman
Yes there is with statements like:

assign(paste('m', i, sep='', value)

But I would suggest that you put the values in a list to make it
easier to access since all the data is in a single object.  You could
do it in a loop:

result - list()
for(i in 1:100){
..computation.
result[[i]] - yourResult
}


On 8/26/06, Wensui Liu [EMAIL PROTECTED] wrote:
 Dear Lister,

 Is there a way to create many objects with sequencial names, say lm1,
 lm2...lm100?

 Thanks.

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



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

What is the problem you are trying to solve?

__
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 create many objects with sequencial names?

2006-08-26 Thread Wensui Liu
Hi, Jim,

It is you again. I couldn't remember how many times you answered my
silly questions. ^_^

I am not sure assign() is what I want. Say, if I want to create 1000
linear model objects with names lm1, lm2lm1000, it seems assign
can't solve it.

But your second solution is close to what I am looking for.

Any idea?

Thanks.

wesui
On 8/26/06, jim holtman [EMAIL PROTECTED] wrote:
 Yes there is with statements like:

 assign(paste('m', i, sep='', value)

 But I would suggest that you put the values in a list to make it
 easier to access since all the data is in a single object.  You could
 do it in a loop:

 result - list()
 for(i in 1:100){
 ..computation.
 result[[i]] - yourResult
 }


 On 8/26/06, Wensui Liu [EMAIL PROTECTED] wrote:
  Dear Lister,
 
  Is there a way to create many objects with sequencial names, say lm1,
  lm2...lm100?
 
  Thanks.
 
  __
  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.
 


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

 What is the problem you are trying to solve?



-- 
WenSui Liu
(http://spaces.msn.com/statcompute/blog)
Senior Decision Support Analyst
Health Policy and Clinical Effectiveness
Cincinnati Children Hospital Medical Center

__
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] Simulations in R during power failure

2006-08-26 Thread bbvaughn
Hi everyone,

I recently ran a simulation on a computer using R that was hooked up to a UPS.  
There was one time when the power was out for length and the computer shut 
down.  I was worried that I had lost the simulation, but upon booting the 
machine up, I heard the processor kick in.  It sounded like the simulation had 
resumed.

Does anyone have any experience with this?  Because I live in Florida and there 
is a possibility of another hurricane.  And I am running a huge simulation 
which may or may not finish before this hurricane finishes.

What options are there in saving a simulation and it picking up where it left 
off?

Thanks,
Brandon

__
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] Students With Writing Difficulties?

2006-08-26 Thread [Academics] Discourse Consultants
If you have undergraduates who have trouble writing a decent term paper, or 
postgraduates who are having problems with their theses or publications, we may 
be able to help you. We offer a personalised, online, tuition service designed 
to help students and academics produce solid, well argued texts that are fit 
for their intended purpose and appropriate for their intellectual discourse 
community, whether it be mechanical engineering or medieval Danish.

For more information about who we are and what we do, click here 

http://www.discourse.com.ar/  

Best Regards

Eamonn MacDonagh


-- 

If you never want to hear from us again just click on the link below. 
If you´d  rather we did it for you,  just ask.  

http://www.dscr.com.ar/dada/mail.cgi/u/dis_cons_01/

If the above URL is inoperable, make sure that you have copied the 
entire address. Some mail readers will wrap a long URL and thus break
this automatic unsubscribe mechanism. 

If you're still having trouble, please contact us at: 

mailto:[EMAIL PROTECTED]

__
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] Modifying the embed-results

2006-08-26 Thread Atte Tenkanen
Thanks! 
I see, that do.call-function is used often in R-algorithms...  Looking over 
some extra do.call-examples seems useful. This tail-function is also new for 
me. 

Is there some reason to use seq(along = VECTOR) instead of 1:length(VECTOR)?
Atte

 You can replace the for with lapply like this:
 
 VECTOR - c(0, 3, 6, 3, 11, 2, 11, 4, 3, 4, 7, 7, 6, 4, 8)
 f - function(i) unique(tail(VECTOR, length(VECTOR)-i+1))[1:5]
 out - do.call(rbind, lapply(seq(along = VECTOR), f))
 na.omit(rbind(rep(NA, 5), out))
 
 Note that  a matrix with zero rows is returned in the case
 that VECTOR has zero length and in the case that VECTOR
 has fewer than 5 unique elements.
 
 
 On 8/26/06, Atte Tenkanen [EMAIL PROTECTED] wrote:
  Again my example was't very clear: there were not enough same 
 numbers in the VECTOR.
  What I need is something like this:
 
  VECTOR-c(0,3,6,3,11,2,11,4,3,4,7,7,6,4,8)
  MATRIX-c()
  for(i in 1:length(VECTOR)){
 v-(unique(VECTOR[i:length(VECTOR)])[1:5])
 MATRIX-rbind(MATRIX,v)
  }
 
  MATRIX-na.omit(MATRIX)
  MATRIX
 
   data.frame(MATRIX, row.names=NULL)
   X1 X2 X3 X4 X5
  1   0  3  6 11  2
  2   3  6 11  2  4
  3   6  3 11  2  4
  4   3 11  2  4  7
  5  11  2  4  3  7
  6   2 11  4  3  7
  7  11  4  3  7  6
  8   4  3  7  6  8
  9   3  4  7  6  8
 
  So, there are no duplicates in rows.
  VECTOR is always scanned forward as long as the number of  items 
 (here 5) becomes full.
 
  Atte
 
   Try:
  
   embed(VECTOR, 5)[,5:1]
  
   On 8/25/06, Atte Tenkanen [EMAIL PROTECTED] wrote:
Hi,
   
Here is a vector and the result from the embed-command:
   
VECTOR=c(0,3,6,3,11,2,4,3,7,6,4,5,10,2,3,5,8)
   
 embed(VECTOR, dimension=5)
 [,1] [,2] [,3] [,4] [,5]
 [1,]   113630
 [2,]2   11363
 [3,]42   1136
 [4,]342   113
 [5,]7342   11
 [6,]67342
 [7,]46734
 [8,]54673
 [9,]   105467
[10,]2   10546
[11,]32   1054
[12,]532   105
[13,]8532   10
   
Is there a way to little modify the algorithm so that the result
   looks like this:
   
[1]  0  3  6 11  2 - beginning from the first number of the 
 VECTOR   [1]  3  6 11  2  4 - beginning from the second number 
 of the
   VECTOR etc
[1]  6  3 11  2  4
[1]  3 11  2  4  7
[1] 11  2  4  3  7
[1] 2 4 3 7 6
[1] 4 3 7 6 5
[1] 3 7 6 4 5
[1]  7  6  4  5 10
[1]  6  4  5 10  2
[1]  4  5 10  2  3
[1]  5 10  2  3  8
[1] 10  2  3  5  8
   
Every row consists of next five unique(!) member of the 
 VECTOR. I
   made this example result with a time consuming algorithm which
   uses for-loops
and whiles.
   
How to do this better??
   
Thanks in advance!
   
Atte Tenkanen
University of Turku
   
__
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-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] Permanently changing gui preferences

2006-08-26 Thread mel
David Kaplan a écrit :

 Greetings,
 I made changes to my gui preferences and saved them.  When I close and 
 then open R, it reverts back to default preferences.  How do I 
 permanently change gui preferences?

one way is using options()
and also using your Rprofile.site file (in ~/etc).
edit it.
See also .First(), source(), etc

you may also directly edit your Rconsole file (also in ~/etc)

or use the config editor in R and save

hih

__
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] Simulations in R during power failure

2006-08-26 Thread mel
[EMAIL PROTECTED] a écrit :

 Hi everyone,
 I recently ran a simulation on a computer using R that was hooked up to a 
 UPS.  There was one time when the power was out for length and the computer 
 shut down.  I was worried that I had lost the simulation, but upon booting 
 the machine up, I heard the processor kick in.  It sounded like the 
 simulation had resumed.
 Does anyone have any experience with this?  Because I live in Florida and 
 there is a possibility of another hurricane.  And I am running a huge 
 simulation which may or may not finish before this hurricane finishes.
 What options are there in saving a simulation and it picking up where it left 
 off?
 Thanks,
 Brandon

write your results, let's say every hour or 30' on your disk.
something like
if (substr(date(),...)=='30') write.table(...)
where it could take place

__
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] Type II and III sum of square in Anova (R, car package)

2006-08-26 Thread Mark Lyman
 1. First of all, more general question. Standard anova() function for lm()
 or aov() models in R implements Type I sum of squares (sequential), which
 is not well suited for unbalanced ANOVA. Therefore it is better to use
 Anova() function from car package, which was programmed by John Fox to use
 Type II and Type III sum of squares. Did I get the point?
 
 2. Now more specific question. Type II sum of squares is not well suited
 for unbalanced ANOVA designs too (as stated in STATISTICA help), therefore
 the general rule of thumb is to use Anova() function using Type II SS
 only for balanced ANOVA and Anova() function using Type III SS for
 unbalanced ANOVA? Is this correct interpretation?
 
 3. I have found a post from John Fox in which he wrote that Type III SS
 could be misleading in case someone use some contrasts. What is this about?
 Could you please advice, when it is appropriate to use Type II and when
 Type III SS? I do not use contrasts for comparisons, just general ANOVA
 with subsequent Tukey post-hoc comparisons.
 
There are many threads on this list that discuss this issue. Not being a great
statistician myself, I would suggest you read through some of these as a start.
As I understand, the best philosophy with regards to types of sums of squares is
to use the type that tests the hypothesis you want. They were developed as a
convenience to test many of the hypotheses a person might want automatically,
and put it into a nice, neat little table. However, with an interactive system
like R it is usually even easier to test a full model vs. a reduced model. That
is if I want to test the significance of an interaction, I would use
anova(lm.fit2, lm.fit1) where lm.fit2 contains the interaction and lm.fit2 does
not. The anova function will return the appropriate F-test. The danger with
worrying about what type of sums of squares to use is that often we do not think
about what hypotheses we are testing and if those hypotheses make sense in our
situation.

Mark Lyman

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