16
$\begingroup$

It seems to me that to choose the right statistical tools, I have to firstly identify if my dataset is discrete or continuous.

Could you mind to teach me how can I test whether the data is discrete or continuous with R?

$\endgroup$
3
  • $\begingroup$ Do you mean whether certain variables need to be added as continuous or as categorical (discrete) predictors in a regression type model? $\endgroup$
    – Nick Sabbe
    Jun 23, 2011 at 8:45
  • $\begingroup$ How data were collected and how variables were recorded will likely give you some clues about that; in addition, it is likely to depend on whether you want to model your data as continuous or discrete ones (see e.g., question related to Likert items and discrete scales analysis). Unrelated point: It would be good if you could register your account once for all, and maybe consider accepting answers or revising your earlier questions. $\endgroup$
    – chl
    Jun 30, 2011 at 20:46
  • $\begingroup$ do a qqnorm and if the points are all along the diagonal the data is continuous (if it is in horiontal lines it is discrete) $\endgroup$
    – user222362
    Oct 2, 2018 at 21:36

1 Answer 1

17
$\begingroup$

The only reason I can immediately think of to require this decision, is to decide on the inclusion of a variable as continuous or categorical in a regression.

First off, sometimes you have no choice: character variables, or factors (where someone providing the data.frame has made the decision for you) are obviously categorical.

That leaves us with numerical variables. You may be tempted to simply check whether the variables are integers, but this is not a good criterion: look at the first line of code below (x1): these are 1000 observations of only the two values $-1.5$ and $2.5$: even though these are not integers, this seems an obvious categorical variable. What you could do for some x is check how many different values are in your data, though any threshold you might use for this will be subjective, I guess:

x1<-sample(c(-1.5, 2.5), 1000)
length(unique(x1)) #absolute number of different variables
length(unique(x1))/length(x1) #relative
x2<-runif(1000)
length(unique(x2)) #absolute number of different variables
length(unique(x2))/length(x2) #relative

I would tend to say that a variable that has only 5% unique values could be safely called discrete (but, as mentioned: this is subjective). However: this does not make it a good candidate for including it as a categorical variable in your model: If you have 1000000 observations, and 5% unique values, that still leaves 50000 'categories': if you include this as categorical, you're going to spend a hell of a lot of degrees of freedom.

I guess this call is even more subjective, and depends greatly on sample size and method of choice. Without more context, it's hard to give guidelines here.

So now you probably have some variables that you could add as categorical in your model. But should you? This question can be answered (though it really depends, again, on your goal) with a likelihood ratio test: The model where the variable is categorical is a supermodel of the model with the variable as a continuous covariate. To see this, imagine a linear regression on a variable xthat hold three values 0, 1 and 2. Fitting a model: $$E[y] = \beta_0 + \beta_11 x_{1} + \beta_12 x_{2}$$ where the $x_i$ is a dummy variable indicator (it is equal to 1 if $x==i$) is just a more flexible way of fitting a model $$E[y] = \beta_0 + \beta_1 x$$ because the last one is equivalent to $$E[y] = \beta_0 + \beta_1 x_{1} + 2 \beta_1 x_{2}$$

With super/submodel structure, you can find out whether there is evidence in the data that the more complex structure is necessary, by doing a likelihood ratio test: -2 times the difference in log maximum likelihood (typically indicated as deviance in R) will follow a $\chi^2$ distribution with df= the difference in number of parameters (in the example above: 4 parameters - 3 parameters).

$\endgroup$
3
  • 3
    $\begingroup$ +1 Nice example of how to improve odd question with great answer. $\endgroup$
    – user88
    Jun 23, 2011 at 10:32
  • 1
    $\begingroup$ Well in fact any continuous can be discretesized, making histograms just show how it is done in practice. Probably I mixed up count data (integer value data) with categorical... though my first guess was about discrete and continuous distributions, not just data points (and crazy researchers that assign real values to categories), so... deleted mine anyway, since don't think it solves the problem (+1) $\endgroup$ Jun 23, 2011 at 13:46
  • 1
    $\begingroup$ it seems that @Dmitrij removed his answer, could you please reedit your answer to reflect that? It is a great answer (+1), so that reference to non-existing content sticks out a bit. $\endgroup$
    – mpiktas
    Jun 30, 2011 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.