[R-bloggers] How to use Hierarchical Bayes for Choice Modeling In R (and 4 more aRticles) |
- How to use Hierarchical Bayes for Choice Modeling In R
- Judging Freehand Circle Drawing Competitions
- New R package debugr – use automatic debug messages to improve your code
- A Certification for R Package Quality
- R Consortium Proposal Accepted!
How to use Hierarchical Bayes for Choice Modeling In R Posted: 31 Jul 2018 05:00 AM PDT (This article was first published on R – Displayr, and kindly contributed to R-bloggers) There are multiple add-on packages available in R to fit choice models in a Bayesian framework. These include bayesm, choiceModelR, and flipChoice. In this article, I fill focus on the use of flipChoice, which relies on the state-of-the-art probabilistic programming language stan to fit the choice model. Getting StartedI assume you are already familiar with choice models so I'll be focusing solely on their estimation using flipChoice. You can install flipChoice from GitHub using the devtools package but you may need to first install devtools using the command install.packages("devtools") in R. You can then install flipChoice using devtools::install_github("Displayr/flipChoice"). The main function that handles fitting of choice models in the package is FitChoiceModel. FitChoiceModel offers great flexibility in the types of inputs it accepts. Data can be in any of five different formats, including Sawtooth CHO file format, Q experiment question format, a ChoiceModelDesign output from flipChoice::ChoiceModelDesign, Sawtooth Dual file format, and JMP file format. Inputs/Function ArgumentsI'll key you in on some of the arguments/parameters of the FitChoiceModel function you should be aware of to fit your own choice model.
Additional parameters are discussed in the documentation; type ?flipChoice::FitChoiceModel at the R prompt to view it. The function also accepts arguments which will be passed on to rstan::sampling and rstan::stan. Example dataI'll walk you through the rest of this method using data from a discrete choice experiment. This experiment investigated consumer preferences when purchasing a 12-pack of chicken eggs from a supermarket. Each of the 380 respondents were asked eight questions requiring them to choose between three alternative 12-packs of eggs. The alternatives varied in their levels of seven attributes:
I've provided an example question below: Fitting your Choice ModelThe eggs data is included in the package in multiple formats, so you can experiment and try out the different input formats accepted by FitChoiceModel. To fit a choice model to the eggs data in Cho file format, we can use the following commands: library(flipChoice) The function will issue a warning if it detects any issues with sampling but we should also check for convergence by examining the effect sample sizes and Rhat values for the mean and standard deviation parameters. These are included in the output of summary statistics using ExtractParameterStats(fit). We can examine trace plots for the same parameters using TracePlots(fit). Estimates of the posterior medians and 95% confidence bands for these parameters can be plotted using the function PlotPosteriorIntervals. My colleague, Justin, has discussed the diagnostics in more detail (including images) for a very similar model (MaxDiff) here. We hope this post has helped you use Hierarchical Bayes to create your choice model. But if you're feeling a bit confused and overwhelmed, it's OK! Feel free to check out our guide on how to use Hierarchical Bayes to create a choice model in Displayr, it's easier, we promise.
To leave a comment for the author, please follow the link and comment on their blog: R – Displayr. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more... This posting includes an audio/video/photo media file: Download Now |
Judging Freehand Circle Drawing Competitions Posted: 30 Jul 2018 03:00 PM PDT (This article was first published on Theory meets practice..., and kindly contributed to R-bloggers) Abstract:In 2007 Alexander Overwijk went viral with his 'Perfect Circle' video. The same year a World Freehand Circle Drawing Championship was organized, which he won. In this post we show how a mobile camera, R and the imager package can be used to develop an image analysis based method to judge future instances of the championship. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. The markdown+Rknitr source code of this blog is available under a GNU General Public License (GPL v3) license from github. IntroductionA few years back I watched with awe the 2007 video of Alexander Overwijk's freehand drawing a 1m diameter circle: Note: Depending on your browser you might need to click "Watch on Youtube" to see the video. Ever since watching that video I have wondered how one would go about to judge the winner of such an alleged World Freehand Circle Drawing Championship (WFHCDC). While researching for this post I finally figured it out. On his web page Alexander in the story behind the video "reveals": They have a laser machine called the circleometer that creates the perfect circle closest to the one you drew. The circleometer then calculates the difference in area between the laser circle and the circle that you drew. The machine then calibrates the area difference as if you had drawn a circle with radius one meter. The person with the smallest area difference is declared the world freehand circle drawing champion. Aha! Imaginary circleometers are expensive and my dean of study most likely isn't open for investing in perfect circle measurement equipment… So here is a cheaper solution involving a mobile device camera, R and the
We start by loading the screenshot into R using Image RectificationThe image clearly suffers from perspective distortions caused by the camera being positioned to the right of the circle and, hence, not being orthogonal to the blackboard plane. Furthermore, small lense distortions are also visible – for example the right vertical line of the blackboard arcs slightly. Since the video contains no details about what sort of lense equipment was used, for the sake of simplicity, we will ignore lense distortions in this post. If such information is available one can use a program such as RawTherapee (available under a GNU GPL v3 license) to read the EXIF information in the meta data of the image and automatically correct for lens distortion. To rectify the image we estimate the parameters of the 2D projection based on 4 ground control points (GPC). We use R's These points are now used to rectify the image by a Direct Linear Transformation (DLT) based on exactly 4 control points (Hartley and Zisserman 2004, Chapter 4)1. That is the parameters of the 3×3 transformation matrix \(H\) in homogeneous coordinates are estimated such that \(p' = H p\), see the code on github for details. We can implement the rectifying transformation using the The result looks as follows: Please notice the different x-axes of the two images when comparing them. For faster computation and better visualization in the remainder of this post, we crop the x-axis of the image to the relevant parts of the circle. Freehand circle identificationAs described in the To detect the circle from this we specify a few seed points for a watershed algorithm with a priority map inverse proportional to gradient magnitude. This includes a few points outside the circle and a few points inside the circle. Note: a perfect circle would have no border, but when drawing a circle with a piece of chalk it's destined to have a thin border line. We can now extract the circle by: Perfect circle estimationOnce the freehand circle path in the image has been identified, we need to find the best fitting perfect circle matching this path. This problem is elegantly solved by Coope (1993), who formulates the problem as finding center and radius of the circle minimizing the squared Euclidean distance to \(m\) data points \(a_j\), \(j=1,\ldots,m\). Denoting by \(c\) the center of the circle and by \(r>0\) the radius we want to find the solution of \[ and \(||x||_2\) denotes Euclidean distance. Because the curve fitting minimizes the distance between an observed point \(a_j\) and its closest point on the circle and thus involves both the \(x\) and the \(y\) direction , this is a so called total least squares problem. The problem is non-linear and can only be solved by iterative numerical methods. However, the dimension of the parameter space can be reduced by one, because given the center \(c\) we can determine that \(r(c)=\frac{1}{m} \sum_{j=1}^m ||c-a_j||_2\). We illustrate the freehand circle (in black) and the fitted circle (magenta) on top of each other using the alpha channel. You have to study the image carefully to detect differences between the two curves! Quantifying the circularness of the freehand circleWe quantify the circularness of the freehand circle by contrasting the area covered by it with the area of the fitted perfect circle. The closer this ratio is to 1 the more perfect is the freehand circle. Yup, it's a pretty perfect circle! Note also that the calibration is to a circle with an area of 1 pixel unit and not a circle with diameter of 1m as described in the above text about the "circleometer". Since the fitted circle already takes the desired shape into account, my intuition is that this ratio is a pretty good way to quantify circularness. However, to avoid measurehacks, we use as backup measure the circleometer approach: for each point on the freehand circle we measure its distance to the freehand circle and integrate/sum this up over the path of the freehand circle. We can approximate this integration using image pixels as follows. The image below illustrates this by overlaying the result on top of the distance map. For better visualization we zoom in on the 270-300 degree part of the circle (i.e. the bottom right). In magenta is the fitted perfect circle, in gray the freehand circle and the area between the two paths is summed up over the entire path of the freehand circle: We obtain DiscussionWe took elements of computer vision, image analysis and total least squares to segment a chalk-drawn circle on a blackboard and provided measures of it's circularness. Since we did not have direct access to the measurements of the blackboard in object space, a little guesstimation was necessary, nevertheless, the results show that it was a pretty circular freehand circle! With the machinery in place for judging freehand circles, its time to send out the call for contributions to the 2nd World Freehand Circle Drawing Championship (online edition!). Stay tuned for the call: participants would upload their photo plus minor modifications of a general analysis R-script computing the two area ratios measures and submit their contribution by a pull request the github WFHCDC repository. You can spend the anxious waiting time practicing your freehand 1m diameter circles – it's a good way to loosen up long & unproductive meetings! AppendixIf we instead of the total sum of squares criterion involving \(F_j(c,r)\) mentioned in the text solve the related criterion \[ \[ over the measurement point. In order to obtain the solution write the residuals \(f_j\) as \(f_j(c,r) = c^T c – 2 c^T a_j + a_j^T a_j – r^2\) and perform a change of variables from \((c_1, c_2, r)'\) to \[ In other words: the results are nearly identical. LiteratureCoope, I.D. 1993. "Circle Fitting by Linear and Nonlinear Least Squares." Journal of Optimization Theory and Applications 76 (2): 381–88. doi:10.1007/BF00939613. Hartley, R., and A. Zisserman. 2004. Multiple View Geometry in Computer Vision. 2nd ed. Cambridge University Press. http://www.robots.ox.ac.uk/~vgg/hzbook/.
To leave a comment for the author, please follow the link and comment on their blog: Theory meets practice.... R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more... This posting includes an audio/video/photo media file: Download Now |
New R package debugr – use automatic debug messages to improve your code Posted: 30 Jul 2018 02:41 PM PDT (This article was first published on Topics in R, and kindly contributed to R-bloggers) debugr is a new package designed to support debugging in R. It mainly provides the dwatch() function which prints a debug output to the console or to a file. A debug output can consist of a static text message, the values of one or more objects (potentially transformed by applying some functions) or the value of one or multiple (more complex) R expressions. Whether or not a debug message is displayed can be made dependent on the evaluation of a criterion phrased as an R expression. Generally, debug messages are only shown if the debug mode is activated. The debug mode is activated and deactivated with debugr_switchOn() and debugr_switchOff(), respectively, which change the logical debugr.active value in the global options. Since debug messages are only displayed in debug mode, the dwatch() function calls can even remain in the original code as they remain silent and won't have any effect until the debug mode is switched on again. Read more:
To leave a comment for the author, please follow the link and comment on their blog: Topics in R. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more... This posting includes an audio/video/photo media file: Download Now |
A Certification for R Package Quality Posted: 30 Jul 2018 09:35 AM PDT (This article was first published on Revolutions, and kindly contributed to R-bloggers) There are more than 12,000 packages for R available on CRAN, and many others available on Github and elsewhere. But how can you be sure that a given R package follows best development practices for high-quality, secure software? Based on a recent survey of R users related to challenges in selecting R packages, the R Consortium now recommends a way for package authors to self-validate that their package follows best practices for development. The CII Best Practices Badge Program, developed by the Linux Foundation's Core Infrastructure Initiative, defines a set of criteria that open-source software projects should follow for quality and security. The criteria relate to open-source license standards, documentation, secure development and delivery practices, version control and bug reporting practices, build and test processes, and much more. R packages that meet these standards are a signal to R users that can be relied upon, and as the standards document notes:
Developers can self-validate their packages (free of charge) using an online tool to check their adherence to the standards and get a "passing", "silver" or "gold" rating. Passing projects are eligible to display the badge (shown above) as a sign of quality and security, and almost 200 projects qualify as of this writing. A number of R packages have already gone through or started the certification process, including ggplot2, R Consortium projects covr and DBI, and the built-in R package Matrix Matrix. For more details on how R packages can get to a passing certification, and the R Consortium survey they led to the recommendation, see the R Consortium blog post at the link below. R Consortium: Should R Consortium Recommend CII Best Practices Badge for R Packages: Latest Survey Results
To leave a comment for the author, please follow the link and comment on their blog: Revolutions. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more... This posting includes an audio/video/photo media file: Download Now |
R Consortium Proposal Accepted! Posted: 30 Jul 2018 06:00 AM PDT (This article was first published on R – AriLamstein.com, and kindly contributed to R-bloggers) Today I am happy to announce that my proposal to the R Consortium was accepted! I first announced that I was submitting a proposal back in March. As a reminder, the proposal has two distinct parts:
If you'd like to read the proposal in its entirety, you can do so here. I am currently planning to develop the "Census Guide" in public using github. If you'd like to follow along with the development, you can do so by visiting the github respository and clicking the "watch" button:
The post R Consortium Proposal Accepted! appeared first on AriLamstein.com.
To leave a comment for the author, please follow the link and comment on their blog: R – AriLamstein.com. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more... This posting includes an audio/video/photo media file: Download Now |
You are subscribed to email updates from R-bloggers. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google, 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |
Comments
Post a Comment