1
$\begingroup$

I'm trying to fit a Logistic Model using LogitModelFit in Mathematica.

The data I'm using contain qualitative variables that I have to encode.

I've searched in the documentation but I haven't been able to find any such function. Does Mathematica have such a function? If not, why?¹

{{"No", 729.526, 44361.6, "No"},
 {"Yes", 817.18, 12106.1,  "No"},
 {"No", 1073.55, 31767.1, "No"},
 {"No", 529.251, 35704.5, "No"},
 {"No", 785.656, 38463.5, "No"},
 {"Yes", 919.589, 7491.56, "No"},
 {"No", 825.513, 24905.2, "No"},
 {"Yes", 808.668, 17600.5, "No"},
 {"No", 1161.06, 37468.5, "No"},
 {"No", 0, 29275.3, "No"}}

¹ I know it's possible to write one, but I'm trying to figure out why wouldn't Mathematica have something like that (if in fact it doesn't).

$\endgroup$
  • 1
    $\begingroup$ The function is ReplaceAll. Assuming that the encoding that you want is {"Yes"->1, "No"->0} just use list /. {"Yes"->1, "No"->0}. $\endgroup$ – Bob Hanlon Feb 12 '15 at 16:13
  • $\begingroup$ I was using Map[{#[[1]], #[[2]], #[[3]], <|"No" -> 0, "Yes" -> 1|>@#[[4]]} &, data];. That's even simpler, thanks!! Please make that an answer and I'll mark it as correct. $\endgroup$ – ivanmp Feb 12 '15 at 16:16
5
$\begingroup$

The function is ReplaceAll. Assuming that the encoding that you want is {"Yes"->1, "No"->0}:

list = {{"No", 729.526, 44361.6, "No"},
   {"Yes", 817.18, 12106.1, "No"},
   {"No", 1073.55, 31767.1, "No"},
   {"No", 529.251, 35704.5, "No"},
   {"No", 785.656, 38463.5, "No"},
   {"Yes", 919.589, 7491.56, "No"},
   {"No", 825.513, 24905.2, "No"},
   {"Yes", 808.668, 17600.5, "No"},
   {"No", 1161.06, 37468.5, "No"},
   {"No", 0, 29275.3, "No"}};

encodedList = list /. {"Yes" -> 1, "No" -> 0}

{{0, 729.526, 44361.6, 0}, {1, 817.18, 12106.1, 0}, {0, 1073.55, 31767.1, 0}, {0, 529.251, 35704.5, 0}, {0, 785.656, 38463.5, 0}, {1, 919.589, 7491.56, 0}, {0, 825.513, 24905.2, 0}, {1, 808.668, 17600.5, 0}, {0, 1161.06, 37468.5, 0}, {0, 0, 29275.3, 0}}

$\endgroup$
3
$\begingroup$

ReplaceAll is highly concise, which I like, but as an alternative you may consider applying a specific transformation function to only the parts of your data that need it using MapAt:

(* list = your data *)

enc["Yes"] = 1;
enc["No"]  = 0;

MapAt[enc, list, {All, {1, 4}}]
{{0, 729.526, 44361.6, 0}, {1, 817.18, 12106.1, 0}, {0, 1073.55, 31767.1, 0}, . . .}
$\endgroup$
  • $\begingroup$ Interesting suggestion! I definitely see a need for this in some cases. $\endgroup$ – ivanmp Feb 12 '15 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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