Re: [R] [Q] Bayeisan Network with the

2006-04-13 Thread Claus Dethlefsen
Dear Young-Jin Lee

Please note that there is a 'deal' mailing list, see 
http://www.math.aau.dk/~dethlef/novo/deal

R I followed the manual and paper from the author's web site to learn it, as
R shown below, but I could not figure out how to access the local and
R posterior probability of the nodes in the constructed network.


To access the prior and posterior distributions, use
localprior( nodes(ksl.nw)[[1]] )
localposterior( nodes(ksl.nw)[[1]] )

Best,
Claus

__
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] Time Series information in formulae

2006-04-12 Thread Claus Dethlefsen
Dear List

The UKgas data is stored as an object of class 'ts'. I am trying to use UKgas 
in a formula as argument to a function. However, I do not know how to access 
the 'time series' information in the response (such as start() end() etc.).

Here is a boiled down example. 

ssm -  function(formula, data = list(),subset=NULL) {
cl - match.call()
  if (missing(data)) 
data - environment(formula)
  mf - match.call(expand.dots = FALSE)
  mf$drop.unused.levels - TRUE
  mf[[1]] - as.name(model.frame)
  mf - eval(mf, .GlobalEnv)
  mt - attr(mf, terms)
  y - model.response(mf, numeric)
  print(class(y))
  }

R class(UKgas)
[1] ts
R ssm(UKgas~1)
[1] numeric
R ssm(log10(UKgas)~1)
[1] numeric

I want the latter two to be ts. I have tried putting any in place 
of numeric in the call to model.response, but it does not change anything for 
me.

How do I retrieve the response from a formula without loosing its ts status?

Best,
Claus

Claus Dethlefsen, MSc, PhD
Biostatistician at Center for Cardiovascular Research
Aalborg Hospital, Aarhus University Hospital, Denmark

__
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] Kalman Smoothing - time-variant parameters (sspir)

2005-12-01 Thread Claus Dethlefsen / Aalborg Sygehus
Dear Tariq Khan
 
The initial conditions m0 and C0 can be specified according to your needs. If 
you are a Bayesian (as in WestHarrison 1997), you will use m0 and C0 to 
express your prior information. If you use a vague prior, you will give a high 
weight to your observations in the beginning, and the influence of the prior 
will die out fast. 
 
The values of m0 and C0 could also stem from several time-series and express a 
random effect of the level of the individual series.
 
Finally, you may estimate m0 and C0 using maximum likelihood estimation. This 
is not done in sspir (but the log-likelihood value is provided from a run of 
the filter).
 
One crude way of specifying m0 and C0 would be to use the estimates from a 
static model, i.e.
 
ss$ss$m0[1:2,] - coef(lm(y~x,data=dfrm))
ss$ss$C0[1:2,1:2] - summary(lm(y~x,data=dfrm))$cov.unscaled
smooth.params3 - kfs(ss)$m
ts.plot(t(smooth.params3))

Note that the 'kfs' function is a shortcut for using smoother(kfilter()).
 
Note also, that your variance parameters are both set to unity. Again, you may 
discuss how to set these either by previous knowledge or by maximum likelihood 
estimation. It is set using
 
ss$ss$phi[1]   - 2 # observational variance
ss$ss$phi[2]   - .5# variance of the beta-parameter
 
Hope this helps,
 
Claus
 

Claus Dethlefsen, Msc, PhD
Statistiker ved Kardiovaskulært Forskningscenter

 
Forskningens Hus
Aalborg Sygehus 
Sdr. Skovvej 15
9000 Aalborg

Tlf:   9932 6863
email: [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] 



Fra: ¨Tariq Khan [mailto:[EMAIL PROTECTED]
Sendt: to 01-12-2005 13:12
Til: R-help@stat.math.ethz.ch; [EMAIL PROTECTED]
Cc: Claus Dethlefsen / Aalborg Sygehus
Emne: Kalman Smoothing - time-variant parameters (sspir)



Dear R-brains,

I'm rather new to state-space models and would benefit from the extra
confidence in using the excellent package sspir.

In a one-factor model, If I am trying to do a simple regression where
I assume the intercept is constant and the 'Beta' is changing, how do
I do that? How do i Initialize the filter (i.e. what is appropriate to
set m0, and C0 for the example below)?

The model I want is: y = alpha + beta + err1; beta_(t+1) = beta_t + err2

I thought of the following:
library(mvtnorm) # (1)
library(sspir)
# Let's get some data so we can all try this at home
dfrm - data.frame(
   y =
c(0.02,0.04,-0.03,0.02,0,0.01,0.04,0.03,-0.01,0.04,-0.01,0.05,0.04,
 
0.03,0.01,-0.01,-0.01,-0.03,0.02,-0.04,-0.05,-0.02,-0.04,0,0.02,0,

-0.01,-0.01,0.01,0.09,0.03,0.03,0.05,0.04,-0.01,0.05,0.03,0.01,
  0.04,0.01,-0.01,-0.02,-0.01,-0.01,
0.06,0.03,0.02,0.03,0.03,0.04,
  0.03,0.04,-0.02,-0.03,0.04,0.03,0.05,0.02,0.03,-0.1),
   x = c(-0.03,-0.01,0.07,-0.03,-0.07,0.05,0.02,-0.05,-0.04,
   -0.02,-0.19,0.07,0.09,0.01,0.01,0,0.05,0,-0.02,-0.09,
   -0.12,-0.01,-0.13,0.04,0.04,-0.07,-0.05,-0.03,
   -0.01,0.11,0.06,0.03,0.06,0.06,-0.01,0.07,0.01,
  
0,0.07,0.04,-0.02,0,-0.03,0.04,-0.04,-0.01,0.03,0.02,0.05,0.04,
0.05,0.03,0,-0.04,0.05,0.05,0.06,0.02,0.04,-0.06)
)
ss - ssm(y ~ tvar(x), time = 1:nrow(dfrm), family=gaussian(link=identity),
   data=dfrm)
smooth.params - smoother(kfilter(ss$ss))$m

(1) I read in http://ww.math.aau.dk/~mbn/Teaching/MarkovE05/Lecture3.pdf
that this is requred as there is a bug in sspir.

To what should I set ss$ss$m0 and ss$ss$C0? (I did notice that
smoother() replaces these, but it still matters what I initialize it
to in the first place)

Many thanks!

Tariq Khan

__
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] Advice on parsing formulae

2004-12-16 Thread Claus Dethlefsen
Thank you for the advice. I have now boiled my problem down to the
following:

How do I create fm2 from fm1 ?

fm1 -  Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3))
fm2 -  Y ~ 1 + x:A + z + u + B + poly(v, 3)

Thus, how do I simply remove tvar( * ) from a formula? Do I have to write a
function to parse the string and then re-create the formula? Is there an
easy way of doing this?


When my above problem is solved, I can (with the help from Heather Turner
and Chuck Berry) do the following

## define som data
x - z - u - v - rnorm(5)
A - B - factor( rep( c(1,2), c(3,2) ) )

## define my formula fm1 and manually create fm2.
fm1 -  Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3))
fm2 -  Y ~ 1 + x:A + z + u + B + poly(v, 3)

## extract the term.labels from fm2, make the design matrix and extract
'assign'
term.labels - unname(sapply(attr(terms(fm2), term.labels),
 removeSpace))
X - model.matrix(fm2,keep.order=TRUE)
pAssign - attr(X, assign)

## Now, extract the tvar-terms from fm1
tvar.terms - terms( fm1, specials = tvar,keep.order=TRUE )
idx - attr(tvar.terms,specials)$tvar
if (attr(tvar.terms,intercept)) idx - idx -1
tvar - attr(terms(fm2,keep.order=TRUE),term.labels)[idx]
tvar - unname( sapply( tvar, removeSpace) )

## Finally, combine the information to get the vector I asked for
tvarAssign - match(pAssign, sort(match(tvar, term.labels)))
tvarAssign[is.na(tvarAssign)] - 0

 

 -Original Message-
 From: Heather Turner [mailto:[EMAIL PROTECTED] 
 Sent: 15. december 2004 11:41
 To: [EMAIL PROTECTED]
 Subject: Re: [R] Advice on parsing formulae
 
 
 I think this will do what you want:
 
 # Need this function to remove spaces from term labels later on
  removeSpace -  function(string) gsub([[:space:]], , string)
 
 # Specify which terms are in a tvar group
 # (could remove spaces separately)
  tvar - unname(sapply(c(x:A, z, B, poly(v,3)), removeSpace))
 
 # Use terms to get term labels from formula
  formula - Y ~ 1 + x:A + z + u + B + poly(v,3)
  term.labels - unname(sapply(attr(terms(formula), 
 term.labels), removeSpace))
  tvar
 [1] x:A   z B poly(v,3)
  term.labels
 [1] z u B poly(v,3) x:A
 
 # Get assign variable for parameters
 # (You would use first two lines, but I don't have data so 
 defined assign variable myself)
  #X - model.matrix(formula)
  #pAssign - attr(X, assign)
  pAssign - c(0,1,2,3,4,4,4,5,5)  
 
 # Define tvarAssign
  tvarAssign - match(pAssign, sort(match(tvar, term.labels)))
  tvarAssign[is.na(tvarAssign)] - 0
  tvarAssign
 [1] 0 1 0 2 3 3 3 4 4
 
 HTH
 
 Heather
 
 Mrs H Turner
 Research Assistant
 Dept. of Statistics
 University of Warwick
 
  Claus Dethlefsen [EMAIL PROTECTED] 12/13/04 04:10pm 
 Dear list
 
 I would like to be able to group terms in a formula using a 
 function that I
 will call tvar(), eg. the formula
 
 Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3))
 
 where x,u and v are numeric and A and B are factors - binary, say.
 
 As output, I want the model.matrix as if tvar had not been 
 there at all. In
 addition, I would like to have information on the grouping, 
 as a vector as
 long as ncol( model.matrix ) with zeros corresponding to 
 terms outside tvar
 and with an index grouping the terms inside each tvar(). In the (sick)
 example:
 
 
  model.matrix(Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + 
 tvar(poly(v,3)))
(Intercept) z u B2 poly(v, 3)1 poly(v, 3)2 poly(v, 
 3)3  x:A1
 x:A2
 11 -1.55 -1.03  0   0.160  -0.350  
 -0.281  0.66
 0.00
 21 -1.08  0.55  0  -0.164  -0.211   
 0.340  0.91
 0.00
 31  0.29 -0.26  0  -0.236  -0.073   
 0.311 -1.93
 0.00
 41 -1.11  0.96  0   0.222  -0.285  
 -0.385 -0.23
 0.00
 51  0.43 -0.76  1  -0.434   0.515  
 -0.532  0.22
 0.00
 
 I would like the vector
 
 c(0,1,0,2,3,3,3,4,4)
 
 pointing to the tvar-grouped terms.
 
 Thus what I would like, looks a bit like the 'assign' attribute of the
 model.matrix() output. I have not figured out a way of doing 
 this in a nice
 way and would like some help, please.
 
 I hope somebody can help me (or point the manual-pages I should read),
 
 Best, 
 
 Claus Dethlefsen
 --
 Assistant Professor, Claus Dethlefsen, Ph.D.
 mailto:[EMAIL PROTECTED], http://www.math.auc.dk/~dethlef 
 Dpt. of Mathematical Sciences, Aalborg University
 Fr. Bajers Vej 7G, 9220 Aalborg East
 Denmark
 

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.

__
[EMAIL PROTECTED] 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] Advice on parsing formulae

2004-12-13 Thread Claus Dethlefsen
Dear list

I would like to be able to group terms in a formula using a function that I
will call tvar(), eg. the formula

Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3))

where x,u and v are numeric and A and B are factors - binary, say.

As output, I want the model.matrix as if tvar had not been there at all. In
addition, I would like to have information on the grouping, as a vector as
long as ncol( model.matrix ) with zeros corresponding to terms outside tvar
and with an index grouping the terms inside each tvar(). In the (sick)
example:


 model.matrix(Y ~ 1 + tvar(x:A) + tvar(z) + u + tvar(B) + tvar(poly(v,3)))
   (Intercept) z u B2 poly(v, 3)1 poly(v, 3)2 poly(v, 3)3  x:A1
x:A2
11 -1.55 -1.03  0   0.160  -0.350  -0.281  0.66
0.00
21 -1.08  0.55  0  -0.164  -0.211   0.340  0.91
0.00
31  0.29 -0.26  0  -0.236  -0.073   0.311 -1.93
0.00
41 -1.11  0.96  0   0.222  -0.285  -0.385 -0.23
0.00
51  0.43 -0.76  1  -0.434   0.515  -0.532  0.22
0.00

I would like the vector

c(0,1,0,2,3,3,3,4,4)

pointing to the tvar-grouped terms.

Thus what I would like, looks a bit like the 'assign' attribute of the
model.matrix() output. I have not figured out a way of doing this in a nice
way and would like some help, please.

I hope somebody can help me (or point the manual-pages I should read),

Best, 

Claus Dethlefsen

---
Assistant Professor, Claus Dethlefsen, Ph.D.
mailto:[EMAIL PROTECTED], http://www.math.auc.dk/~dethlef
Dpt. of Mathematical Sciences, Aalborg University
Fr. Bajers Vej 7G, 9220 Aalborg East
Denmark

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.

__
[EMAIL PROTECTED] 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] [R-pkgs] Package deal version 1.2-17

2004-08-20 Thread Claus Dethlefsen
A new version of the package deal is now available on CRAN. The package is
for learning (parameters and structure) of Bayesian networks and provide an
interface to Hugin. In the new version there is an interface to the package
dynamicGraph which allows for editing and callbacks of graphs in the
displayed window.

Try
 install.packages(c(dynamicGraph,deal))
 library(deal)
 demo(reinis)

Best,
Claus

---
Assistant Prof. Claus Dethlefsen, PhD
Department of Mathematical Sciences, Aalborg University 
Fredrik Bajers Vej 7G, DK-9220 Aalborg, Denmark
[EMAIL PROTECTED]; www.math.aau.dk/~dethlef
Ph: +45 9635 8878; Fax: +45 9815 8129
 

---

___
R-packages mailing list
[EMAIL PROTECTED]
https://stat.ethz.ch/mailman/listinfo/r-packages

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