# Transform a variable to desired Median and IQR

I am looking for a method to transform a variable ($Y_{old}$) into a new variable ($Y_{new}$) with a target median ($Median_{new}$) and IQR ($IQR_{new}$).

The following transformation seems to work, but I don't understand why. Can anyone provide a mathematical explanation?

$Y_{new} = Median_{new} + (Y_{old} - Median_{old})*(IQR_{new}/IQR_{old})$

• Note that your equation is simply a rearrangement of: $\frac{Y_\text{new}-\tilde{Y}_\text{new}}{q_\text{new}}=\frac{Y_\text{old}-\tilde{Y}_\text{old}}{q_\text{old}}$ (where $\tilde{Y}$ denoted the median and $q$ denotes the interquartile range). That is you are equating a standardized version of the new and the old variables, so that corresponding values are each the same number of IQRs from the median. Jun 24 '17 at 0:49

First, suppose $Y^{(1)}_{old}<Y^{(2)}_{old}$. If $f$ is the transformation, then $$f(Y^{(2)}_{old})-f(Y^{(1)}_{old})=(Y^{(2)}_{old}-Y^{(1)}_{old})*(IQR_{new}/IQR_{old})>0$$ and so $f(Y^{(1)}_{old})<f(Y^{(2)}_{old})$ and so the transformation preserves the order of observations. This is important since it means if $Y$ is the $n$th quartile before the transformation, then $f(Y)$ is the $n$th quartile after the transformation.
Now observe that $$f(Median_{old})=Median_{new}-(Median_{old}-Median_{new})*(IQR_{new}/IQR_{old})$$ $$=Median_{new}$$ Similarly, if $Q_{1}$ and $Q_3$ are the old 1st and 3rd quartiles, you can show that $IQR_{new}=f(Q_3)-f(Q_1)$.