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
3 replies to this topic

#1 smouksassi1

smouksassi1

    Advanced Member

  • Members
  • PipPipPip
  • 198 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
  • 198 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))

              

 

 



#3 Simon Davis

Simon Davis

    Advanced Member

  • Administrators
  • 1,230 posts

Posted 20 April 2022 - 10:25 AM

Here is a nice example of building nested IF statements: https://www.linkedin...4304647168-pLKZ



#4 Simon Davis

Simon Davis

    Advanced Member

  • Administrators
  • 1,230 posts

Posted 22 April 2022 - 10:36 AM

This method could be applied within PML for binning too.

 

The if method will work inside sequence statements, that is sequence() or dobefore() or doafter() or simulate()

 

outside the sequences the other method, ternary operator, should be applied

agemonths<3?1:

         agemonths<6?2:

                  agemonths<9?3:

                           agemonths<12?4:

                                    agemonths<15?5:

                                             agemonths<18?6:

                                                      agemonths<21?7:

                                                               agemonths <24?8:9

 

The conditional (ternary) operator takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is true followed by a colon ( : ), and finally the expression to execute if the condition is false.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users