Re: [R] Strange error while passing string as an argument to the function in bnlearn package
Hello Marco, By any chance, are using that for prediction? Yes, I am using it for prediction. To compute P(A = a | whatever you conditioned on), just sum the corresponding weights over the total weight mass. Since no language trickery is involved, this works reliably. Thank you! It's exactly what I needed. --- BR, Alexandr On 11 November 2014 23:33, Marco Scutari marco.scut...@gmail.com wrote: Hi Alexandr, On 11 November 2014 00:10, Alexandr M rus...@gmail.com wrote: Sorry that I formulated my question not very accurately. I form expressions/(logic conditions for parameters evidence and event) dynamically inside the loop and they are sometimes quite long. By any chance, are using that for prediction? Because predict(..., method = bayes-lw) does posterior predictions from any set of variables. As an alternative, you can do cpdist(..., method = lw) which also generates from the posterior distribution: str(cpdist(fit, node = A, evidence = list(B = b), method = lw)) Classes ‘bn.cpdist’ and 'data.frame':1 obs. of 1 variable: $ A: Factor w/ 3 levels a,b,c: 1 2 2 3 1 1 1 2 1 1 ... - attr(*, weights)= num 0.114 1 1 0.428 0.114 ... - attr(*, method)= chr lw To compute P(A = a | whatever you conditioned on), just sum the corresponding weights over the total weight mass. Since no language trickery is involved, this works reliably. Cheers, Marco -- Marco Scutari, Ph.D. Lecturer in Statistics, Department of Statistics University of Oxford, United Kingdom -- Best regards, Alexander Maslov [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Strange error while passing string as an argument to the function in bnlearn package
Hello everybody, I am working with the package bnlear, but, probably, error is caused not by the package itself. There is a function cpquery() to which I pass argument str2: str2=(lag1=='s') prob.s = cpquery(fitted1, event=eval(parse(text=(M=='s'))), evidence=eval(parse(text=str2))) - Error in parse(text = str2) : object 'str2' not found fitted1 is a fitted model But if I put value of str2 directly into function call: prob.s = cpquery(fitted1, event=eval(parse(text=(M=='s'))), evidence=(lag1.M1=='s')) it works fine. Does anybody have any idea how to fix it? PS: - I used documentation to the package: http://cran.r-project.org/web/packages/bnlearn/bnlearn.pdf function cpquery() - I also trued to use backslashes \' \ - doesn't work too -- Best regards, Alexandr [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Strange error while passing string as an argument to the function in bnlearn package
Hi Marco, Thanks for your reply! Logic sampling in cpquery() relies on handling unevaluated expressions, so it is a tad fragile in any complex setting (inside loops and function calls, for example). Actually I am doing it inside the loop. Inside the loop I determine important features and form expressions dynamically in the string format: for( i in 1:N ) { ... varnames = names(newrow) # new piece of data in a form of the row with variables names # like in the documentation: event parameter str1 = paste((, var[1] , ==', as.character(newrow[1, 1]), '), sep = ) # evidence parameter: str2 = paste((, var[-1], ==', sapply(newrow[1,-1], as.character), '), sep = , collapse = ) # estimate conditional probability cpquery(fitted.model, event = eval(parse(text=str1)), evidence = eval(parse(text=str2))) ... } For the simple query you are trying to do, just use likelihood weighting Sorry that I formulated my question not very accurately. I form expressions/(logic conditions for parameters evidence and event) dynamically inside the loop and they are sometimes quite long. -- Best regards, Alexandr On 10 November 2014 23:28, Marco Scutari marco.scut...@gmail.com wrote: Hi Alexandr, On 10 November 2014 17:46, Alexandr M rus...@gmail.com wrote: I am working with the package bnlear, but, probably, error is caused not by the package itself. Logic sampling in cpquery() relies on handling unevaluated expressions, so it is a tad fragile in any complex setting (inside loops and function calls, for example). On its own, the eval(parse(...)) trick works if you do it in the global environment, or in relatively simple scripts. For the simple query you are trying to do, just use likelihood weighting: prob.s = cpquery(fitted1, event=eval(parse(text=(M=='s'))), evidence=list(lag1.M1='s'), method = lw) passing str2 as a list. Cheers, Marco -- Marco Scutari, Ph.D. Lecturer in Statistics, Department of Statistics University of Oxford, United Kingdom -- Best regards, Alexander Maslov [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Shewhart Control Charts for Time Series data (qcc package)
Hello everybody! I am trying to apply qcc function to time series data. There is an example from documentation: ### library(qcc) data(pistonrings) attach(pistonrings) diameter - qcc.groups(diameter, sample) qcc(diameter[1:25,], type=xbar) detach(pistonrings) ### So, we have 5 iteams at each row - sample size =5 But what should I do if I have just a one dimensional time series data? ### out.m - aggregate(pistonrings$diameter, list(sample = pistonrings$sample), FUN=mean) qcc(out.m$x, type=xbar) - Error: group sizes must be larger than one ### Probably in this case samples are a consecutive subsets of the data but they are not grouped as in an example. I would like to apply qcc() like rollapply(width = sample_size, by = step) in such a way that 'width' would stands for a samples taken from an input data. Thank you in advance ! -- Kind regards, Alexandr [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.