Thursday, April 28, 2011

Fitting a density curve to a histogram in R

Is there a function in R that fits a curve to a histogram?

Let's say you had the following histogram

hist(c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))

It looks normal, but it's skewed. I want to fit a normal curve that is skewed to wrap around this histogram.

This question is rather basic, but I can't seem to find the answer for R on the internet.

From stackoverflow
  • If I understand your question correctly, then you probably want a density estimate along with the histogram:

    X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4))
    hist(X, prob=TRUE)            # prob=TRUE for probabilities not counts
    lines(density(X))             # add a density estimate with defaults
    lines(density(X, adjust=2), lty="dotted")   # add another "smoother" density
    
  • Here's the way I do it:

    foo <- rnorm(100,mean=1,sd=2)
    hist(foo,prob=TRUE)
    curve(dnorm(x,mean=mean(foo),sd=sd(foo),add=TRUE)
    

    A bonus exercise is to do this with ggplot2 package ...

    John Johnson : However, if you want something that is skewed, you can either do the density example from above, transform your data (e.g. foo.log <- log(foo) and try the above), or try fitting a skewed distribution, such as the gamma or lognormal (lognormal is equivalent to taking the log and fitting a normal, btw).
    Dirk Eddelbuettel : But that still requires estimating the parameters of your distribution first.
    John Johnson : This gets a bit far afield from simply discussing R, as we are getting more into theoretical statistics, but you might try this link for the Gamma: http://en.wikipedia.org/wiki/Gamma_distribution#Parameter_estimation For lognormal, just take the log (assuming all data is positive) and work with log-transformed data. For anything fancier, I think you would have to work with a statistics textbook.
    Dirk Eddelbuettel : I think you misunderstand how both the original poster as well as all other answers are quite content to use non-parametric estimates -- like an old-school histogram or a somewhat more modern data-driven densisty estimate. Parametric estimates are great if you have good reason to suspect a distribution. But that was not the case here.
  • Such thing is easy with ggplot2

    library(ggplot2)
    dataset <- data.frame(X = c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)))
    ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..)) + geom_density()
    

    or to mimic the result from Dirk's solution

    ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..), binwidth = 5) + geom_density()
    

Are there simple http services out there to check domain name availability

Hi. Im looking to call a simple http service from my Java servlet to check the price an availability of domain names from my site.

Basically, i want to do something like this checker here... http://www.123-reg.co.uk/

I know i can just to a get on the target url and parse the response, but I'm looking for an exising service this just gives me an XML response. Even a webserivce if available.

Has anyone come across any companies that expose their domain checkers as a service?

Thanks.

From stackoverflow
  • What you're looking for is a whois lookup. Here is a GNU licensed Java whois package. And here is another Java whois package.

    Asaph : That depends on what registrar you choose. The price can vary. What registrar will you be purchasing domain names through?
    rmeador : different registrars will likely give you different prices. You'll have to pick who you want to buy it through and ask them.
  • you gotta have a try with domainr and its api.

Display offscreen Google Earth on a panel?

Is it possible to capture the graphics object created by the Google Earth browser plugin from a .NET WebBrowser control?

With this graphics object I could create an image to use as the background image for a panel that I can then draw on top of.

You cannot just use a WebBrowser control under a Panel control as the Google Earth plugin does not work.

Getting the browser graphics object just returns blank. The browser DrawToBitmap method (no intelisense) returns the web page but without the Google Earth image.

Any ideas?

From stackoverflow
  • My apologies if I misunderstood the question - are you trying to display Google Earth in a Windows Forms program? If so, have a look at Google Earth COM API.

    Google Earth COM API

    Stevo3000 : No, that I can do. What I want to be able to do is to get the graphics object to use as the background image for a panel control. The question is quite clear on this.
    Khadaji : Ah well, as I said, my apologies. I wish you luck in your search.
  • Take a look at this project of mine: http://code.google.com/p/winforms-geplugin-control-library/ it will go some way to helping. The trick here is to capture the current image from the plugin as a bitmap and then use this (like double buffering).

    Especially see the 'ScreenGrabButton_Click' method in 'GEToolStrip.cs' to see how to capture the image.

    Stevo3000 : I like your project, but your screen grab relies on the plugin being shown on the screen through a web browser that is visible. I want to be able to get a graphics object/image without having a visible web browser.
    Fraser : Stevo, you should re-phrase your question then! But AFAIK the answer is no.
  • After some more research and trial and error I am forced to admit defeat on this one, it seems imposible to show google earth on a panel when the webbrowser control hosing it is offscreen.

    The solution is either to live with the limitation:

    • Set the form you wish to draw on background to transparent
    • Create a new form to be the parent of the drawing form
    • Display google earth on the new form
    • Align the forms correctly and allow the child to drive the parent

    Or to switch to Microsoft Live Earth as there is already a proof of concept that works for WPF and winforms.

Force relink when building in QT Creator

Greetings,

I have a subdirs project which wraps a couple libraries and a main application. When I change something in one of the libraries the main application does not relink with them.. does anyone have a trick for getting an application to relink with its statically linked libs automatically when using QT Creator?

-Dan O

From stackoverflow
  • There is a workaround for this and also an interesting discussion on the subject (qmake seems to be the problem here) on the Qt Creator mailing list.

    The workaround is to add a PRE_TARGETDEPS command to your main applications .pro file, e.g.:

    PRE_TARGETDEPS += /path/to/your/lib.a
    

    This forces the relink.

    Dan O : Thanks a ton, had found some discussions on the mailing list but had not stumbled across the workaround.

Graphing Data with Java Processing

I'm looking at creating a program with Processing (processing.org) in Java. The program will involve graphing a large amount of 2D data. I would like for the points to be displayed to fill the window. I've looked at their libraries and I don't see anything for data visualization. Am I missing something?

From stackoverflow
  • I've always used JFreechart or, for more complex graphing exporting to a text flie and then gnuplot.

  • another vote for JFreeChart. Although for more complex graphing I've written my own (AWT).

  • JUNG Is a favorite of mine.

WPF Validating unbound textbox

Hello, is it possible to use validation without the Binding part? The thing is my textbox is not bound to any object, but I still want to validate it's content. The only way I've found so far is this:

 <TextBox Grid.Row="0" Grid.Column="1" MaxLength="50" x:Name="textBoxTubeName" Margin="5,5,0,5">
  <TextBox.Text>
   <Binding Path="Name" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" NotifyOnValidationError="True">
    <Binding.ValidationRules>
     <validation:InvalidCharactersRule />
    </Binding.ValidationRules>
   </Binding>
  </TextBox.Text>
 </TextBox>

But again, it only works when the TextBox.Text is bound to something (in this case, the Name property), how would I go about this without binding?

Thanks!

From stackoverflow
  • According to the MSDN forums it's not possible yet but it is planned (Note: this is an old post). However, I still can't find a way to do it so it may not be implemented yet.

    Carlo : Yeah, I think I'm going to have to do the code myself. I think Microsoft idea to concentrate on binding is good, but limiting the validation this way due to that purpose, is not so good. Thanks Lucas.
    Lucas McCoy : @Carlo: Totally agreed.

How to add html_entity_decode to array?

I am making a content entry with TinyMCE in codeigniter. However the output source is like the following and does not show < and >. Instead it shows HTML enties like &lessthan; and &greaterthan; etc.

The entry is made by admin after logged in.

Output comes from database.

I took out escape in model, but it still does the same thing.

Also I have a config setting, $config['global_xss_filtering'] = FALSE;

So I want to add html_entity_decode. But the $page_data is an array. The array has id, title, content and slug which is used for page item.

Could anyone tell me how to do it please?


Output example:

&lt;p&gt;&lt;img src=&quot;images/icon1.png&quot; border=&quot;0&quot;
alt=&quot;icon&quot; width=&quot;48&quot; height=&quot;48&quot; /&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit.


Model code:

<?php

class Pagemodel extends Model 
{
....
...

/** 
* Return an array of a page — used in the front end
*
* @access public
* @param string
* @return array
*/
function fetch($slug)
{
 $query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
 return $query->result_array();
}


...
...

}

?>

Controller code:

function index()
{
 $page_slug = $this->uri->segment('2'); // Grab the URI segment

 if($page_slug === FALSE)
 {
  $page_slug = 'home';
 }

$page_data = $this->pages->fetch($page_slug); // Pull the page data from the database

 if($page_data === FALSE)
 {
  show_404(); // Show a 404 if no page exists
 }
 else
 {
  $this->_view('index', $page_data[0]);
 }
}
From stackoverflow
  • If I got you correctly you want to pass 'html_entity_decode.' to all fields that are returned from your database. You can easily add something to your fetch function:

    function fetch($slug)
    {
        $query = $this->db->query("SELECT * FROM `pages` WHERE `slug` = '$slug'");
        for($i=0; $i<$query->num_rows(); $i++)
        {
            $html_decoded[$i]['id'] = html_entity_decode($query->id);
            $html_decoded[$i]['title'] = html_entity_decode($query->title);
            $html_decoded[$i]['content'] = html_entity_decode($query->content);
            $html_decoded[$i]['slug'] = html_entity_decode($query->slug);
        }
    
        return  $html_decoded;
    }
    

    If I got your question right that should do what you want.