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?
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?
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 x
that 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).