1
$\begingroup$

Can linear regression be used when both the dependent and independent variable are categorical?

i am looking at word-frequency distribution among a series of texts, and want to show that there is a correlation/association between the frequency of word1 and word2. this a made-up summary of the data:

         word1freq word2freq

text1 .25 .30

text2 .30 .55

text3 .45 .75

text4 .55 .80

my concern is that behind the word frequency is a categorical variable (i.e., 1 for "the word occurs" and 0 for "the word does not occur). does this make a difference with linear models? if linear models are not appropriate here, what test should i be using=?

$\endgroup$
1
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because the grammar is not up to the minimum standard expected for this site. $\endgroup$
    – Ben
    Commented Dec 10, 2019 at 7:40

1 Answer 1

0
$\begingroup$

In this case, you seem to be interested in the association between two documents' word frequencies, without specifying which is supposedly an "outcome" variable as would be required when using linear regression.

Your best, and simplest bet for measuring the association between two word frequencies is a correlation coefficient. This works whether you have reduced the words to relative frequencies within texts or counted word occurrence as a binary variable. In case of the latter, Pearson's R is equivalent to something called the phi coefficient, with largely the same interpretation (but with a maximum value determined by the relative distribution of your 1s and 0s).

I note an ambiguity in your question, which is: Why are the values in your columns (apparently) proportions if your word frequency is, as you state in the text, 1 and 0 for occurrence or non-occurrence? 1/0 values are word frequency, it's a binary measure of word occurrence. As you will see from my example below, it matters in what sort of answer you get (since the questions are different). It does concern me a bit that your values in the example are not even relative word frequency, however, since they do not (cannot) sum to 1.0 across rows or even columns.

To demonstrate, I will use the quanteda package from R. Here I had to coerce the "document-feature matrix" object into a correlation for the ifelse and cor functions to work.

> require(quanteda)
> myDfm <- dfm(inaugTexts, stem = TRUE)
Creating a dfm from a character vector ...
   ... lowercasing
   ... tokenizing
   ... indexing documents: 57 documents
   ... indexing features: 9,214 feature types
   ... stemming features (English), trimmed 3793 feature variants
   ... created a 57 x 5421 sparse dfm
   ... complete. 
Elapsed time: 0.192 seconds.
> 
> wordMatCounts <- as.matrix(myDfm[, c("state", "citizen")])
> wordMatCounts[1:10, ]
                 features
docs              state citizen
  1789-Washington     2       4
  1793-Washington     0       1
  1797-Adams         12       3
  1801-Jefferson      3       5
  1805-Jefferson     12      10
  1809-Madison        5       0
  1813-Madison        5       3
  1817-Monroe        29       9
  1821-Monroe        27      14
  1825-Adams          8       3
> 
> wordMatRelFreq <- as.matrix(weight(myDfm, "relFreq")[, c("state", "citizen")])
> wordMatBinary <- ifelse(as.matrix(wordMatCounts) > 0, 1, 0)
> 
> cor(wordMatCounts)
            state   citizen
state   1.0000000 0.6338093
citizen 0.6338093 1.0000000
> cor(wordMatRelFreq)
             state    citizen
state   1.00000000 0.03351566
citizen 0.03351566 1.00000000
> cor(wordMatBinary)
            state   citizen
state   1.0000000 0.2668415
citizen 0.2668415 1.0000000
$\endgroup$
3
  • 1
    $\begingroup$ thanks for your help! in the simplified data above, i would like to fit a regression to the word frequency data, but im just not sure if its legitimate. what i would like to show if possible is that the frequency of word2 is the dependent variable and the frequency of word1 the independent variable. can i use a regression model on this kind of data? $\endgroup$
    – corpusling
    Commented Sep 28, 2015 at 16:16
  • $\begingroup$ the values do not sum to 1 in either the rows or the columns because i summed the tokens of 1 and then divided the number by the total number of observations (=words in the text). of your three cor() values, which is the most appropriate? $\endgroup$
    – corpusling
    Commented Sep 28, 2015 at 16:28
  • $\begingroup$ My vote would be for relative frequency. $\endgroup$
    – Ken Benoit
    Commented Sep 28, 2015 at 17:14

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