Jump to content


Photo

Tip: How to Bin a continuous variables into intervals of your choosing

interoccasion binning recoding variable recoding variables occasion

  • Please log in to reply
1 reply to this topic

#1 smouksassi1

smouksassi1

    Advanced Member

  • Members
  • PipPipPip
  • 169 posts
  • LocationMontreal

Posted 12 January 2016 - 03:31 AM

Dear Users I thought this tip  might be useful for those who use Phoenix built in data tranformations.
I searched the forums and could not find something similar.
In this example I illustrate how to use the & operator in a custom transformation with nested if statements applied to coding an Occasion variable.
 
Suppose you have a variable here agemonths ranging from 0 to 25.
You want to cut it into intervals of three months each. We can call this equal width binning. It might be useful for graphical purposes or for occasion modeling or something else !
Here is how to write your cutom transformation note that I named the intervals 1 ,2,3 etc. you might want to recode to the middle of the bin or something else.
 
 
if(   (agemonths< 3 )                    ,1,
      if( ( (agemonths>=3 ) & (agemonths <6 ) ),2,
          if( ( (agemonths>=6 ) & (agemonths <9 ) ),3,
              if( ( (agemonths>=9 ) & (agemonths <12) ),4,
                  if( ( (agemonths>=12) & (agemonths <15) ),5,
                      if( ( (agemonths>=15) & (agemonths <18) ),6,
                          if( ( (agemonths>=18) & (agemonths <21) ),7,
                              if( ( (agemonths>=21) & (agemonths <24) ),8,
                                  9
                              ))))))))
 
it is very important to have each condition in parentheses:
  (agemonths>=3 ) & (agemonths <6 )
and then an outer parentheses:
 ( (agemonths>=3 ) & (agemonths <6 ) )
for each condition.
Hope this help.
Samer

 
  


#2 smouksassi1

smouksassi1

    Advanced Member

  • Members
  • PipPipPip
  • 169 posts
  • LocationMontreal

Posted 13 January 2016 - 02:57 PM

Dear Users,
my colleague Simon pointed out that the example above is over complicated we can just write:
if(agemonths  <3,1,

if(agemonths  <6,2,

if(agemonths  <9,3,

if(agemonths <12,4,

if(agemonths <15,5,

if(agemonths <18,6,

if(agemonths <21,7,

if(agemonths <24,8, 9)

)))))))

This givse the same results as above.
My intention is to illustrate how to use  the & operator which can help in other situations e.g. in the conditions below we are  more concise. Both versions  give the same answer it is a matter of style on which one you choose:
 

if(agemonths  <9,0,

       if(agemonths  <12,1,

              if(agemonths  <18,0,

                     if(agemonths<25,1,0))))

 

                                    

if( (agemonths  >9) & (agemonths  <12), 1,

       if((agemonths  >18) & (agemonths  <25), 1,0))

 

 

in R: ifelse does what if does and you don't need parentheses

agemonths <- seq( 0,24,1)

agemonths

 

ifelse(agemonths  <3,1,

       ifelse(agemonths  <6,2,

              ifelse(agemonths  <9,3,

                     ifelse(agemonths <12,4,

                            ifelse(agemonths <15,5,

                                   ifelse(agemonths <18,6,

                                          ifelse(agemonths <21,7,

                                                 ifelse(agemonths <24,8, 9)

                  )))))))

 

ifelse(agemonths  <3,0,

       ifelse(agemonths  <6,1,

              ifelse(agemonths  <9,0,

                     ifelse(agemonths <12,1,

                            ifelse(agemonths <15,0,

                                   ifelse(agemonths <18,1,

                                          ifelse(agemonths <21,0,

                                                 ifelse(agemonths <24,1, 1)

                                          )))))))

 

ifelse(agemonths  <9,0,

       ifelse(agemonths  <12,1,

              ifelse(agemonths  <18,0,

                     ifelse(agemonths<25,1,0))))

 

                                    

ifelse(agemonths  >9 & agemonths  <12, 1,

       ifelse(agemonths  >18 & agemonths  <25, 1,0))

              

 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users