0

R documentation says that nnet::class.ind() generates a class indicator function from a given factor.

Does it convert a factor to some binary classification?

When and why do we use this function? Please give me some examples.

Any help appreciated. Thank you.

smci
  • 32,567
  • 20
  • 113
  • 146
user4745212
  • 51
  • 1
  • 3
  • 11
  • The doc says it generates a class indicator *matrix*, not a function. It's the big matrix of one-hot 1/0 indicator dummy variables corresponding to the factor level values in the original matrix. – smci Feb 01 '17 at 09:58

1 Answers1

2

Yes. It creates indicator/dummy variables from a factor:

> set.seed(1)
> x <- factor(sample(1:3, 10, TRUE))
> nnet::class.ind(x)
      1 2 3
 [1,] 1 0 0
 [2,] 0 1 0
 [3,] 0 1 0
 [4,] 0 0 1
 [5,] 1 0 0
 [6,] 0 0 1
 [7,] 0 0 1
 [8,] 0 1 0
 [9,] 0 1 0
[10,] 1 0 0

It would be essentially the same as using model.matrix:

> model.matrix(~0+x)
   x1 x2 x3
1   1  0  0
2   0  1  0
3   0  1  0
4   0  0  1
5   1  0  0
6   0  0  1
7   0  0  1
8   0  1  0
9   0  1  0
10  1  0  0
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Thanks, do you a example with categorical variables. Why create dummy variables ? Why and when do you use this function ? – user4745212 Apr 04 '15 at 15:47
  • I'm slightly confused as to why you are interested if you dont have an application in mind. It is useful primarily for specifying certain regression models. – Thomas Apr 04 '15 at 20:28
  • My apologies, please look at this link http://www.jeffheaton.com/2013/06/basic-classification-in-r-neural-networks-and-support-vector-machines/. "The neural network requires that the species be normalized using one-of-n normalization. We will normalize between 0 and 1 ". I guess I don't understand why 0 and 1's will normalize it. – user4745212 Apr 04 '15 at 21:24