Re: [R] Fitting models in a loop

2006-08-01 Thread Gesmann, Markus
Murray,

How about creating an empty list and filling it during your loop:

 mod - list()
 for (i in 1:6) {
mod[[i]] - lm(y ~ poly(x,i))
print(summary(mod[[i]]))
}

All your models are than stored in one object and you can use lapply to
do something on them, like:
 lapply(mod, summary) or lapply(mod, coef)


Kind Regards

Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
[EMAIL PROTECTED]
Sent: 01 August 2006 06:16
To: [EMAIL PROTECTED]; r-help@stat.math.ethz.ch
Subject: Re: [R] Fitting models in a loop


Murray,

Here is a general paradigm I tend to use for such problems.  It extends
to fairly general model sequences, including different responses, c

First a couple of tiny, tricky but useful functions:

subst - function(Command, ...) do.call(substitute, list(Command,
list(...)))

abut - function(...)  ## jam things tightly together
  do.call(paste, c(lapply(list(...), as.character), sep = )) 

Name - function(...) as.name(do.call(abut, list(...)))

Now the gist.

fitCommand - quote({
MODELi - lm(y ~ poly(x, degree = i), theData)
print(summary(MODELi))
})
for(i in 1:6) {
thisCommand - subst(fitCommand, MODELi = Name(model_, i), i =
i)
print(thisCommand)  ## only as a check
eval(thisCommand)
}

At this point you should have the results and

objects(pat = ^model_)

should list the fitted model objects, all of which can be updated,
summarised, plotted, c, because the information on their construction
is all embedded in the call.

Bill.

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen
Sent: Tuesday, 1 August 2006 2:09 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Fitting models in a loop

If I want to display a few polynomial regression fits I can do something

like

for (i in 1:6) {
mod - lm(y ~ poly(x,i))
print(summary(mod))
}

Suppose that I don't want to over-write the fitted model objects, 
though. How do I create a list of blank fitted model objects for later 
use in a loop?

Murray Jorgensen
-- 
Dr Murray Jorgensen  http://www.stats.waikato.ac.nz/Staff/maj.html
Department of Statistics, University of Waikato, Hamilton, New Zealand
Email: [EMAIL PROTECTED]Fax 7 838 4155
Phone  +64 7 838 4773 wkHome +64 7 825 0441Mobile 021 1395 862

__
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.
**
The information in this E-Mail and in any attachments is CON...{{dropped}}

__
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] trim function in R

2006-07-24 Thread Gesmann, Markus
See ?substr and ?nchar
Try:
substr(Hello World, 0, nchar(Hello World)-2)

Regards

Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Wade Wall
Sent: 24 July 2006 14:42
To: r-help@stat.math.ethz.ch
Subject: [R] trim function in R


Hi all,
I am looking for a function in R to trim the last two characters of an
8 character string in a vector.  For example, I have the codes
37-079-2, 370079-3,37-079-8 and want to trim them to 37-079 by
removing the last two characters.  Is sub the correct function to use,
and if so how can I specify trimming the last 2 characters?  I have
read the help file, but can't quite figure out how to do it.
Thanks,
Wade

__
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.
**
The information in this E-Mail and in any attachments is CON...{{dropped}}

__
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] about overdispersed poisson model

2006-07-10 Thread Gesmann, Markus
Dear Chi Kai,

Three years ago there was a similar thread. 
At that time David Firth offered a solution for the quasipoission
problem with negative observations, see:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/16143.html
I remember that his code gave you slightly different answers than the
example in England and Verrall's paper.

Kind Regards

Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of g0354502
Sent: 10 July 2006 05:26
To: r-help@stat.math.ethz.ch
Subject: [R] about overdispersed poisson model


Dear R users

  I have been looking for functions that can deal with overdispersed
poisson
models. According to actuarial literature (England  Verall, Stochastic
Claims 
Reserving in General Insurance , Institute of Actiuaries 2002) this can
be handled through the
use of quasi likelihoods instead of normal likelihoods. However, we see
them frequently
in this type of data, and we would like to be able to fit the model
anyway.
If it is possible, would you please show me how to find the
corresponding package and utilize them?

Best Regards,

Chi Kai

**
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


Re: [R] The beautiful math plot

2006-04-26 Thread Gesmann, Markus
See:
?image


Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Daniel Yang
Sent: 26 April 2006 13:39
To: R-help@stat.math.ethz.ch
Subject: [R] The beautiful math plot


Dear R-help,

How can I replicate the beautiful math plot found in the right-hand side
of http://www.r-project.org/screenshots/desktop.jpg? I tried the
following code but didn't obtain something as beautiful.

r - seq(-10, 10, len=100)
y - cos(r^2)*exp(-r/6)
par(pty=s)
plot(r,y,type=l)



Thanks in advance!
Yung-jui Yang
[[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
**
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


Re: [R] footnote in postscript lattice

2006-01-27 Thread Gesmann, Markus
Here is one example using the page option in xyplot:

library(lattice)
library(grid)
 add.footnote - function(string=Hello World, col=grey,
lineheight=0.5, cex=0.7){
   grid.text(string,
  x=unit(1, npc) - unit(1, mm),
  y=unit(1, mm), just=c(right, bottom),
  gp=gpar(col=col,lineheight=lineheight, cex=cex))
  }
  
 xyplot(1~1, page=function(n){ add.footnote()})


Markus Gesmann
FPMA
Lloyd's Market Analysis
Lloyd's * One Lime Street * London * EC3M 7HA
Telephone +44 (0)20 7327 6472
Facsimile +44 (0)20 7327 5718
http://www.lloyds.com


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Dean Sonneborn
Sent: 26 January 2006 23:46
To: r-help@stat.math.ethz.ch
Subject: [R] footnote in postscript lattice


I would like to add a footnote to this graph but do not see a footnote
command in the package:lattice documentation. I would like to note the
span=.8
 as the footnote.

postscript(file= C:/Documents and Settings/dsonneborn/My
Documents/Slovak/output/pcb_tables/smooth_PCB_lines_four.ps,
bg=transparent, onefile=FALSE, pointsize=20,paper=letter,
horizontal=TRUE, family=Helvetica,font=Helvetica)

xyplot(AWGT ~ lipid_adj_lpcb2_cent | malex*romanix, data=pcb_graph3,

   auto.key = list(lines = TRUE, points = TRUE), ylab=Birth
Weight, xlab=Lipid Adjusted PCB,

   par.settings =

   list(superpose.symbol = list(col = colr, pch = plotchar),

superpose.line = list(col = colr, pch = plotchar, lty = 1)),

type=c(p, smooth), span=.8)

dev.off()
thanks

-- 
Dean Sonneborn, MS
Programmer Analyst
Department of Public Health Sciences
University of California, Davis
(530) 754-9516

__
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

LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


[R] change some levels of a factor column in data frame according to a condition

2005-11-14 Thread Gesmann, Markus
Dear R-users,

I am looking for an elegant way to change some levels of a factor column
in data frame according to a condition.
Lets look at the following data frame:

 data.frame(crit1=gl(2,5), crit2=factor(letters[1:10]), x=rnorm(10))
   crit1 crit2   x
1  1 a -1.06957692
2  1 b  0.24368402
3  1 c -0.24958322
4  1 d -1.37577955
5  1 e -0.01713288
6  2 f -1.25203573
7  2 g -1.94348533
8  2 h -0.16041719
9  2 i -1.91572616
10 2 j -0.20256478

Now I would like to find for each level in crit1 the two smallest values
of x and change the levels of crit2 to small, so the result would look
like this:

   crit1 crit2   x
1  1 small -1.06957692
2  1 b  0.24368402
3  1 c   -0.24958322
4  1 small  -1.37577955
5  1 e  -0.01713288
6  2 f   -1.25203573
7  2 small  -1.94348533
8  2 h   -0.16041719
9  2 small  -1.91572616
10 2 j  -0.20256478

Thank you for advice!

Markus Gesmann

LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


RE: [R] GLM question

2005-05-31 Thread Gesmann, Markus
It appears that you try to do some insurance reserving analysis. 
Have a look at http://finzi.psych.upenn.edu/R/Rhelp02a/archive/15315.html, this 
might be helpful.

Regards

Markus


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Laetitia Mestdagh
Sent: 31 May 2005 14:44
To: r-help@stat.math.ethz.ch
Subject: [R] GLM question



I am unfamiliar with R and I'm trying to do few statistical things like GLM and 
GAM with it. I hope my following questions will be clear enough:

 

My datas ( y(i,j ))are run off triangles for example :

 

 

J=1

J=2

J=3

I=1

1

2

3

I=2

 

4

5

 

I=3

6

 

 

 

 

My model is :

 

E[y(i,j)] =m(i,j)

Var[y(i,j)] =constant *m(i,j)

 

Log(m(i,j)) = eta (i,j)

 

eta (i,j) = c + alpha(i) + beta(j)

 

The y(i,j) are the response and they have no specified distribution.

 

Here is what I did and I'm not getting the right results:

 

 y1-c(1,0,0,0,0)

 y2-c(1,0,0,1,0)

 y3-c(1,0,0,0,1)

 y4-c(1,1,0,0,0)

 y5-c(1,1,0,1,0)

 y6-c(1,0,1,0,0)

 C-matrix(nrow = 6, ncol = 5, byrow= TRUE)

  C[1,]-y1

   C[2,]-y2

   C[3,]-y3

   C[4,]-y4

   C[5,]-y5

   C[6,]-x6

 

 m-c(1,2,3,4,5,6)

 Cdata-data.frame(C[,1],C[,2],C[,3],C[,4],C[,5])

fmp-glm(m~C,family = quasipoisson(link = log),data=Cdata)

 fitted.values(fmp)

   123456 

1.25 1.75 3.00 3.75 5.25 6.00

 

So my question are : - Why are the fitted wrong (except for 3 and 6)?

- Is the quasipoisson the right family for my model?

 

I am a little bit lost and not an expert of R, so I thank in advance for any 
kind of advice

 

Laetitia



-

ils, photos et vidéos !

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





LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


RE: [R] R -SQL

2005-05-18 Thread Gesmann, Markus
Your code looks more like Visual Basic rather than R.
What you want is:

for(j in 1:length(criteria$Title)){

sqlstring - paste(select q.type,crit.Title, r.Value from criteria crit, reply 
r,question_reply qr, question q, question_criteria qc, form_question fq where 
qr.reply=r.ID and qr.question=q.ID and qc.question=q.ID and crit.ID=qc.criteria 
and fq.question=q.ID and fq.form=4 and crit.Title=', criteria$Title[j], ';, 
sep=)

graphe_par-sqlQuery(channel,sqlstring)

}

See ?paste

Regards

Markus

Your SQL statement seems to be wrong. 

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Navarre Sabine
Sent: 18 May 2005 13:03
To: r-help@stat.math.ethz.ch
Subject: [R] R -SQL


Hello,
 
I've got a problem in a sql query!


for(j in 1:length(criteria$Title))

{
graphe_par-sqlQuery(channel,select q.type,crit.Title, r.Value from 
criteria crit, reply r,question_reply qr, question q, question_criteria qc, 
form_question fq where qr.reply=r.ID and qr.question=q.ID and qc.question=q.ID 
and crit.ID=qc.criteria and fq.question=q.ID and fq.form=4 and crit.Title=  
criteria$Title[j] ;)

}


 criteria$Title
[1] Content   Logistic  Trainer   Supply
User contribution
Levels: Content Logistic Supply Trainer User contribution


The error is:
Error in select q.type,crit.Title, r.Value from criteria crit, reply 
r,question_reply qr, question q, question_criteria qc, form_question fq where 
qr.reply=r.ID and qr.question=q.ID and qc.question=q.ID and crit.ID=qc.criteria 
and fq.question=q.ID and fq.form=4 and crit.Title=   : 
operations are possible only for numeric or logical types
In addition: Warning message:
 not meaningful for factors in: Ops.factor(select q.type,crit.Title, r.Value 
from criteria crit, reply r,question_reply qr, question q, question_criteria 
qc, form_question fq where qr.reply=r.ID and qr.question=q.ID and 
qc.question=q.ID and crit.ID=qc.criteria and fq.question=q.ID and fq.form=4 and 
crit.Title=, 

please help me,

Thanks a lot!

Sabine



-

ils, photos et vidéos !

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





LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


[R] Legend in xyplot two columns

2005-04-14 Thread Gesmann, Markus
Dear R-Help

I have some trouble to set the legend in a xyplot into two rows.
The code below gives me the legend in the layout I am looking for, I
just rather have it in two rows.

library(lattice)
schluessel - list(
   points=list( col=red, pch=19, cex=0.5 ),
   text=list(lab=John),
   lines=list(col=blue),
   text=list(lab=Paul),
   lines=list(col=green),
   text=list(lab=George),
   lines=list(col=orange),
   text=list(lab=Ringo),
   rectangles = list(col= #CC, border=FALSE),
   text=list(lab=The Beatles),
   )
 
xyplot(1~1, key=schluessel)

The next code gives me two rows, but repeates all the points,lines, and
rectangles.
 
schluessel2 - list(
   points=list( col=red, pch=19, cex=0.5 ),
   lines=list(col=c(blue, green, orange)),
   rectangles = list(col= #CC, border=FALSE),
   text=list(lab=c(John,Paul,George,Ringo, The
Beatles)),
   columns=3,
   )
 
xyplot(1~1, key=schluessel2)

So I think each list has to have 6 items, but some with no content.
How do I do this?

Thank you very much!

Markus

LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


Re: [R] Legend in xyplot two columns

2005-04-14 Thread Gesmann, Markus
Thanks Deepayan!

Your solution does excatly what I want. 
Further experiments and thoughts on my side brought me also to a
solution. 
If I use the option rep=FALSE, and plot the bullit with lines and
split the lines argument into two groups it gives me the same result,
as every item in the key list starts a new column.

library(lattice)
key - list( rep=FALSE,
   lines=list(col=c(red, blue), type=c(p,l),
pch=19),
   text=list(lab=c(John,Paul)),
   lines=list(col=c(green, red), type=c(l, l)),
   text=list(lab=c(George,Ringo)),
   rectangles = list(col= #CC, border=FALSE),
   text=list(lab=The Beatles),
   )

xyplot(1~1, key=key)


But your solution is much more felxible!

Kind Regards

Markus

-Original Message-

LNSCNTMCS01***
The information in this E-Mail and in any attachments is CONFIDENTIAL and may 
be privileged.  If you are NOT the intended recipient, please destroy this 
message and notify the sender immediately.  You should NOT retain, copy or use 
this E-mail for any purpose, nor disclose all or any part of its contents to 
any other person or persons.

Any views expressed in this message are those of the individual sender, EXCEPT 
where the sender specifically states them to be the views of Lloyd's.

Lloyd's may monitor the content of E-mails sent and received via its
network for viruses or unauthorised use and for other lawful
business purposes.

Lloyd's is authorised under the Financial Services and Markets Act 2000


From: Deepayan Sarkar [mailto:[EMAIL PROTECTED] 
Sent: 14 April 2005 16:01
To: r-help@stat.math.ethz.ch
Cc: Gesmann, Markus
Subject: Re: [R] Legend in xyplot two columns


On Thursday 14 April 2005 05:30, Gesmann, Markus wrote:
 Dear R-Help

 I have some trouble to set the legend in a xyplot into two rows.
 The code below gives me the legend in the layout I am looking for, I
 just rather have it in two rows.

 library(lattice)
 schluessel - list(
points=list( col=red, pch=19, cex=0.5 ),
text=list(lab=John),
lines=list(col=blue),
text=list(lab=Paul),
lines=list(col=green),
text=list(lab=George),
lines=list(col=orange),
text=list(lab=Ringo),
rectangles = list(col= #CC, border=FALSE),
text=list(lab=The Beatles),
  )

 xyplot(1~1, key=schluessel)

 The next code gives me two rows, but repeates all the points,lines,
 and rectangles.

 schluessel2 - list(
points=list( col=red, pch=19, cex=0.5 ),
lines=list(col=c(blue, green, orange)),
rectangles = list(col= #CC, border=FALSE),
text=list(lab=c(John,Paul,George,Ringo, The
 Beatles)),
columns=3,
   )

 xyplot(1~1, key=schluessel2)

 So I think each list has to have 6 items, but some with no content.
 How do I do this?

You could try using col=transparent to suppress things, but that's not

a very satisfactory solution. The function to create the key is simply 
not designed to create unstructured legends like this. However, you can 
create an use an arbitrary ``grob'' (grid graphics object) for a 
legend, e.g.:

##-

library(grid)
library(lattice)

fl -
grid.layout(nrow = 2, ncol = 6,
heights = unit(rep(1, 2), lines),
widths =
unit(c(2, 1, 2, 1, 2, 1),
 c(cm, strwidth, cm,
   strwidth, cm, strwidth),
 data = list(NULL, John, NULL,
 George, NULL, The Beatles)))

foo - frameGrob(layout = fl)
foo - placeGrob(foo,
 pointsGrob(.5, .5, pch=19,
gp = gpar(col=red, cex=0.5)),
 row = 1, col = 1)
foo - placeGrob(foo,
 linesGrob(c(0.2, 0.8), c(.5, .5),
   gp = gpar(col=blue)),
 row = 2, col = 1)
foo - placeGrob(foo,
 linesGrob(c(0.2, 0.8), c(.5, .5),
   gp = gpar(col=green)), 
 row = 1, col = 3)
foo - placeGrob(foo,
 linesGrob(c(0.2, 0.8), c(.5, .5),
   gp = gpar(col=orange)), 
 row = 2, col = 3)
foo - placeGrob(foo,
 rectGrob(width = 0.6, 
  gp = gpar(col=#CC,
  fill = #CC)), 
 row = 1, col = 5)
foo - placeGrob(foo,
 textGrob(lab = John), 
 row = 1, col = 2)
foo - placeGrob(foo,
 textGrob(lab = Paul), 
 row = 2, col = 2)
foo - placeGrob(foo,
 textGrob(lab = George), 
 row = 1, col = 4)
foo - placeGrob(foo

RE: [R] 'pch' plot symbol with more than one character

2005-03-14 Thread Gesmann, Markus

This might do what you want:

 plot.new()
 plot.window(xlim=c(1,10), ylim=c(1,10))
 text(x=1:10, y=1:10, labels=Hallo)

Look at ?text

Regards

Markus


-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Luis Ridao Cruz
Sent: 14 March 2005 10:37
To: r-help@stat.math.ethz.ch
Subject: [R] 'pch' plot symbol with more than one character


R-help,

Argument 'pch' in 'plot' can only represent a single character.
Is it possible to represent, let's say, two instead?

Thanks in advance.

I'm running on Windows Xp

 version
 _  
platform i386-pc-mingw32
arch i386   
os   mingw32
system   i386, mingw32  
status  
major2  
minor0.1
year 2004   
month11 
day  15 
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

LNSCNTMCS01***
The information in this E-Mail and in any attachments is CON...{{dropped}}

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


[R] To convert an adjacency list model into a nested set model

2005-03-08 Thread Gesmann, Markus
Dear R-help

I am wondering if somebody wrote some code to convert an adjacency list
model into a nested set model.
In principal I want to do the same as John Celko mentioned it here with
SQL:
http://groups.google.co.uk/groups?hl=enlr=lang_enselm=8j0n05%24n31%241
%40nnrp1.deja.com

Assume you have a tree structure like this
Albert 
 /   \
   /   \
 BertChuck 
   /|   \
 /  | \
   /|   \
 /  | \
DonnaEddie   Fred

in an adjacency list model:

 emp=c(Albert, Bert, Chuck, Donna, Eddie, Fred)
 boss=c(NA, Albert, Albert, Chuck, Chuck, Chuck)
 print(Personnel-data.frame(emp, boss))
 emp   boss
1 Albert   NA
2   Bert Albert
3  Chuck Albert
4  Donna  Chuck
5  Eddie  Chuck
6   Fred  Chuck

Then it is quite hard to find the all the supervisors of one employee.
John's suggestion is to convert the adjacency list model into a nested
set model.
The organizational chart would look like this as a directed graph:

Albert (1,12)
/\
  /\
Bert (2,3)Chuck (4,11)
   /|   \
 /  | \
   /|   \
 /  | \
Donna (5,6)  Eddie (7,8)  Fred (9,10)

The data is than stored in the following form:

 lft=c(1,2,4,5,7,9)
 rgt=c(12,3,11,6,8,10)
 print(Per-data.frame(emp, lft, rgt))
  emp lft rgt
1 Albert   1  12
2   Bert   2   3
3  Chuck   4  11
4  Donna   5   6
5  Eddie   7   8
6   Fred   9  10

To find now the supervisor of an employee all you have to do is to look
where the employees lft figure is between lft and rgt. The supervisors
of Eddie are therefore
 subset(Per, lft  7  rgt  7)
 emp lft rgt
1 Albert   1  12
3  Chuck   4  11

In the site mentioned above John provides also some code to transform a
adjacency list model into a nested set model. 
Does somebody know if there is already a package for this in R? 

Kind Regards

Markus Gesmann



LNSCNTMCS01***
The information in this E-Mail and in any attachments is CONFIDENTIAL and may 
be privileged.  If you are NOT the intended recipient, please destroy this 
message and notify the sender immediately.  You should NOT retain, copy or use 
this E-mail for any purpose, nor disclose all or any part of its contents to 
any other person or persons.

Any views expressed in this message are those of the individual sender, EXCEPT 
where the sender specifically states them to be the views of Lloyd's.

Lloyd's may monitor the content of E-mails sent and received via its
network for viruses or unauthorised use and for other lawful
business purposes.

Lloyd's is authorised under the Financial Services and Markets Act 2000

__
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