Macroplot plotting is controlled by the macros in the text area provided.
Each macro must occupy its own line. If the first character of a macro is not A-Z, the line will be considered a comment and ignored
The first macro, which is obligatory, initializes the plot. The macro is
Bitmap Initialize width(in pixels), height(in pixels), red(0-255) blue(0-255), green(0-255) transparency(0-255)
Example : Bitmap Initialize 700 500 255 255 255 255 which provides a landscape area 700 pixels wide, 500 pixel high, with white background
The following are default settings when the bitmap is initiated.
Lines are black (0 0 0 255) and 3 pixels in width
Fill color for bars and dots are black (0 0 0 255), and the fill type is set to fill only (1) (see Fill Type)
Dots (circl and square) are set to 5 pixels radius (diameter=11 pixels)
Fonts are set as follows
Font face is set to sans-serif. Serif, sans-serif, and monospace are available to all browsers, user can use any font available to his/her browser
Font size is set to 16 pixels high
Font color, both line and fill are set to black (0 0 0 255), and fill type to 1 (fill only) (see Font Type)
Macros for plotting on the bitmap begin with the keyword Bitmap, and the coordinates are x=number of pixels from the left border and y=number of pixels from the top border
A central plotting area is also defined
By default, at initialization, as 15% from the left and bottom, 5% from right and top
defined by user as Plot Pixels left top right bottom, these being number of pixels from the left and top border
e.g. Plot Pixels 105 25 665 425 would be the same as the default setting for a bitmap of 700 pixels wide and 500 pixels high
The values of the data used in plotting in this central area can be defined as follows
Plot Values left top right bottom, these being the extreme values used in the data
e.g.Plot Values 0 100 10 50 represents x values of 0 on the left to 10 to the right, and y values of 50 at the bottom to 100 to the top
After the values are declared, all plotting in the central area uses macros beginning with the keyword Plot, and the coordinates are the values in the data
Macros
This panel lists and describes all macros used in this version of MacroPlot by Javascript. They are divided into the following sub-panels
Initialization and settings
Plotting areas, coordinates used, and drawing of x and y axis
Drawing lines, bars, dots, text, and other shapes
Initialization
This sub-panel lists those macros that initialized the bitmap, and set the parametrs for drawing
Initialize Plotting
Bitmap Initialize w h r g b t is the first and obligatory macro, which Initializes the bitmap
w and h are width and height of the bitmap in number of pixels. The most common dimensions are
w=700 and h= 500 for landscape orientation
w=500 and h=700 for portrait orientation
Both 500 for square bitmap
r g b t represents red, green, blue and transparency values for the background, each value is 0 for non-existence to 255 for maximum intensity. The most commonly used background is white (255 255 255 255)
For most plotting programs in StatsToDo the macro used is Bitmap Initialize 700 500 255 255 255 255, a landscape orientation with white background
Settings for lines
The settings provide parameters for all subsequent plotting until the parameter is reset
Line Color r g b t sets the line color of red, green, blue and transparency values, each value is 0 for non-existence to 255 for maximum intensity. On initialization of the bitmap, line color is lines is set by default to black (0 0 0 255)
Line Thick p sets the thickness of lines to p pixels. On initialiszation, the default setting is 3 pixels for line thickness
Settings for fills
When bars, dots, arcs and wedges are plotted, the interior of these symbols are called fills, and they are set as follows
Fill Color r g b t sets the filling color of red, green, blue and transparency values, each value is 0 for non-existence to 255 for maximum intensity. On initialization of the bitmap, fill color is lines is set by default to black (0 0 0 255).
Fill Type t sets how the fills are to be used, t can be one of the following
t=0: only the outline, defined by the line parameters, are plotted. Fill is ignored
t=1: only fill is carried out, outline is ignored
t=2: both outline and fill are plotted
When the plot is initialized, the default setting for fill type is t=1
Settings for fonts
These set the font characteristics for text output. Please note: settings for lines and fills for fonts are separate and independent to those for general line and shape plottings
Font Face name sets the font face. The program will accept all fonts supported by the user's border. The 3 fonts accepted by all browsers are serif, sans-serif, and monospace. On initialization, sans-serif is set by default
Font Style s where s can be either normal or bold. On initialization the default setting is bold
Font Size h where h is the height of the text in pixels. On initialization, the default font size is set to 16
Font Thick p where p is the thickness of the outline of the font. On initialization, this is set to p=1
Font LColor r g b t sets the color of the outline of the font. On initialization this is set to black (0 0 0 255)
Font FColor r g b t sets the fill color of the of the font. On initialization this is set to black (0 0 0 255)
Font Color r g b t sets both LColor and FColor to the same color. On initialization this is set to black (0 0 0 255)
Font Type t where t determines which part of the font is drawn, and can be one of the following
t=0: only the outline of the font, defined by the thick and LColor parameter is drawn
t=1: only the fill of the font is drawn
t=2: both outline and fill are drawn
When the plot is initialized, the default setting for Font type is t=1
Please Note: When the bitmap is initialized, the default settings, which are suitable for most situations, are automatically set, so users need not worry about these settings unless he/she has a different preference.
Axis & Coordinates
This sub-panel presents macros that define the plotting areas, and creating the x and y axis for plotting
Drawing on the bitmap
When plotting on the initialized bitmap
the horizontal coordinate x is the number of pixels from the left border
the vertical coordinate y is the number of pixels from the top border
The macro used begins with the keyword Bitmap
Drawing on the plotting area
In most cases, there is a need to draw and label the x and y axis, and drawing coordinates used are the actual values of the data. The macros used for these all begins with the keyword Plot, and are purposes are as follows
Plot Pixels lp tp rp bp defines an area for plotting
lp defines the left border of the plotting area, in the number of pixels from the left border of the bitmap. In most cases this is 15% of the bitmap's width
tp defines the top of the plotting area, in the number of pixels from the top border of the bitmap. In most cases this is 5% of the height
rp defines the right border of the plotting area, in the number of pixels from the left border of the bitmap. In most cases this is 95% of the width (or 5% from the right border of the bitmap)
bp defines the bottom border of the plotting area, in the number of pixels from the top border of the bitmap. In most cases this is 85% of the height (or 15% from the bottom)
An example is that is that, in a landscape orientated bitmap of 700 pixels width and 500 pixel height, Plot Pixels 105 25 665 425 sets the central area for plotting that is 15% from the left and bottom, and 5% from the top and right.
This macro is usually not necessary if the 5%/15% setting suits the user, as this is the default setting when the bitmap is initialized
Plot Values lv tv rv bv defines the data values to be used in plotting
lv is the extreme data value for the horizontal variable x on the left
tv is the extreme data value for the vertical variable y at the top
rv is the extreme data value for horizontal variable x on the right
bv is the extreme data value for the vertical variable y at the bottom
Plot Logx 1 sets the horizontal x axis to the log scale. Normal scale is set on initialization, or reset by Plot Logx 0
Plot Logy 1 sets the vertical y axis to the log scale. Normal scale is set on initialization, or reset by Plot Logy 0
Plot XLabel label distance places the label for the horizontal x axis, below the bottom of the plotting area
lable is a single word text string, using the underscore _ to represent spaces if necessary
space is the number of pixels between the bottom of the plot area and the label text string
Plot YLabel label distance places the label for the vertical y axis, on the left of plotting area
lable is a single word text string, using the underscore _ to represent spaces if necessary
space is the number of pixels between the left of the plot area and the label text string
The quickest and easiest way to draw axis
The following 4 macros are sufficient to draw the x and y axis under most circumstances
Plot XAxis y nsIntv nbIntv len gap line will mark out and numerate the horizontal x axis
y is the y value on which the x axis lie
nsIntv is the number of small intervals between the vertical line marks, 10 to 20 are recommended
nbIntv is the number of big intervals between the numerical scales, 5 to 10 are recommended
len is the length of the mark in pixels, +ve value downwards and negative value upwards. -10 is recommended
gap is the number of pixels between the numerical scaling text and the y value of the axis, +ve values for text below axis and negative value for text above axis. 3 is recommended
Line determines the axis line is drawn, 0 for no line, 1 for line
Plot YAxis x nsIntv nbIntv len gap line will mark out and numerate the vertical y axis
x is the x value on which the y axis lie
nsIntv is the number of small intervals between the horizontal line marks, 10 to 20 are recommended
nbIntv is the number of big intervals between the numerical scales, 5 to 10 are recommended
len is the length of the mark in pixels, +ve value to the right and negative value to the left. 10 is recommended
gap is the number of pixels between the numerical scaling text and the y value of the axis, +ve values for text to the right of axis and negative value for text to the left of axis. -3 is recommended
Line determines the axis line is drawn, 0 for no line, 1 for line
Plot AutoXLogScale y len gap line will mark and numerate the x axis if it is in log scale
The x axis must be set to the log scale by Plot Logx 1. If axis not set to log this macro will abort
y is the y value on which the x axis lie
len is the length of the mark in pixels, +ve value downwards and negative value upwards. -10 is recommended
gap is the number of pixels between the numerical scaling text and the y value of the axis, +ve values for text below axis and negative value for text above axis. 3 is recommended
Line determines the axis line is drawn, 0 for no line, 1 for line
Plot AutoYLogScale x len gap line will mark and numerate the y axis if it is in log scale
The y axis must be set to the log scale by Plot Logy 1. If axis not set to log this macro will abort
x is the x value on which the x axis lie
len is the length of the mark in pixels, +ve value downwards and negative value upwards. -10 is recommended
gap is the number of pixels between the numerical scaling text and the y value of the axis, +ve values for text below axis and negative value for text above axis. 3 is recommended
Line determines the axis line is drawn, 0 for no line, 1 for line
Other methods of drawing axis
Users may wish to draw individual part of the axis, and the following macros can be used
Plot XLine y Draws the horizontal x axis line at the y value y
Plot YLine x Draws the vertical y axis line at the x value y
Plot XMark y begin interval len marks the horizontal x axis with a series of vertical marks
y is the y value where the axis is to be marked
begin is the value for the first mark
interval is the interval between marks
len is the length of the mark line in pixels, +ve downwards, -ve upwards
Plot YMark x start interval len marks the vertical y axis with a series of horizontal marks
x is the x value where the axis is to be marked
start is the value for the first mark
interval is the interval between marks
len is the length of the mark line in pixels, +ve to the right, -ve to the left
Plot XScale y start interval gap writes the numerical scales for the horizontal x axis
y is the y value for the axis
start is the first value to be written
interval is the interval between numerical scales
gap is the space in pixels between the scale text and the axis, +ve for text below axis, -ve for text above axis
The number of decimal points in the scale is the same as that of the interval value
Plot YScale x start interval gap writes the numerical scales for the vertical y axis
x is the x value for the axis
start is the first value to be written
interval is the interval between numerical scales
gap is the space in pixels between the scale text and the axis, +ve for text to the right of axis, -ve for text to the left of axis
The number of decimal points in the scale is the same as that of the interval value
Plot XMarkIntv y interval len marks the horizontal x axis with a series of vertical marks
y is the y value of the axis
interval is the interval between the marks, beginning at 0 and while in range
len is the length of the mark line in pixels, +ve downwards, -ve upwards
Plot YMarkIntv x interval len marks the vertical y axis with a series of horizontal marks
x is the x value of the axis
interval is the interval between the marks, beginning at 0 and while in range
len is the length of the mark line in pixels, +ve to the right, -ve to the left
Plot XScaleIntv y interval gap writes the numerical scales for the horizontal x axis
y is the y value of the axis
interval is the interval between the numerical scales, beginning at 0 and while in range
gap is the space in pixels between the scale text and the axis, +ve for text below axis, -ve for text above axis
The number of decimal points in the scale is the same as that of the interval value
Plot YScaleIntv x interval gap writes the numerical scales for the vertical y axis
x is the x value of the axis
interval is the interval between the numerical scales, beginning at 0 and while in range
gap is the space in pixels between the scale text and the axis, +ve for text to the right of axis, -ve for text to the left of axis
The number of decimal points in the scale is the same as that of the interval value
Drawings
This sub-panel describes those macros that draws the plotting objects. Drawing are performed in two environments
Macros that begins with the keyword Bitmap uses pixel values as coordinates, where x is the number of pixels from the left border, and y the number of pixels from the top border
Macros that begins with the keyword Plot uses actual data values (as defined in the Plot Values lv tv rv bv macro, as coordinates
Drawing lines
The thickness and color of any line drawn is set by the Line macros (see setting sub-panel). The default setting is black line 3 pixels in width
Bitmap Line x1 y1 x2 y2 draws the line from x1y1 to x2y2
x1 and x2 are number of pixels from the left border
y1 and y2 are number of pixels from the top border
Plot Line x1 y1 x2 y2 draws the line from x1y1 to x2y2
x1 and x2 are data values for the horizontal variable x
y1 and y2 are data variables for the vertical variable y
Plot PixLine x y hpix vpix draws a line
x and y are data values for the horizonal x value and verticsl y value. This defines the coordinate at the origin of the line
hpix is the number of pixels horizontally from the origin, +ve value to the right, -ve value to the left
vpix is the number of pixels vertically from the origin, +ve value downwards, -ve value upwards
The line is then drawn between the origin and that defined by hpix and vpix
Drawing bars
The color and thickness of the outline are defined in the Line macro. The color of the fill is defined in the fill color and Fill Type macro. The default setting is black (0 0 0 255) for both line and fill color, and the Fill type is set to 1, only the fill and no outlines. These settings are suitable for most circumstances, but user can change them is so required.
Bitmap Bar x1 y1 x2 y2 draws a bar the corner of which are x1y1 and x2y2. X and y are number of pixels from the left and top border of the bitmap
Plot Bar x1 y1 x2 y2 draws a bar the corner of which are x1y1 and x2y2. X and y are data values as defined in Plot Values lv tv rv bv
Bar Wide w sets the width / height of bars for Plot VBar and Plot HBar
w is the half width of the bar, so a VBar is 2w+1 pixels in width, and HBar is 2w+1 pixels in height
The default value for w is 7 pixels (making width/height of 15 pixels), unless the user changes it
Plot VBar x y1 y2 hshift draws a vertical bar
x is the data value for the horizontal x variable. The is the center of the vertical bar
y1 and y2 are values for the vertical y variable. They define the vertical ends of the bar
hshift is the number of pixels the whole bar is shefted horizontally, +ve value to the left and +ve value to the right. In most cases this is 0 (no shift). However, if there are more than 1 bar in the same position, shifting some of them will avoid the bars overlapping and obscuring each other
The width of the vertical bar is set by default at 7, (width of bar=15 pixels)
Plot HBar x1 x2 y vshift draws a horizontal bar
x1 and x2 are data values for the horizontal x variable. They define the horizontal ends of the bar
y is the value for the vertical y variable, and defines and center of the horizontal bar
vshift is the number of pixels the whole bar is shefted vertically, -ve value upwards and +ve value downwards. In most cases this is 0 (no shift). However, if there are more than 1 bar in the same position, shifting some of them will avoid the bars overlapping and obscuring each other
Theheight of the horizontal bar is set by default at 7, (height of bar=15 pixels)
Drawing dots
There are only 2 dot types, circle and square. If more than 2 tyoes of dats are required, they can be distinguished by the colours of the outline and fill, and by their sizes. Settingsd for dot parameters are in the settings sub-panel
Bitmap Circle x y radius and Bitmap Square x y radius draws a circle or a square dot
x and y are the number of pixels from the left and top border
Radius is in number of pixels. The diameter of the dot is 2Radius+1 pixels
Plot Circle x y radius hshift vshift and Plot Square x y radius hshift vshift draws a circle or a square dot
x and y are the data values of the horizontal x variable and vertical y variable, as defined by Plot Values lv tv rv bv
Radius is in number of pixels. The diameter of the dot is 2Radius+1 pixels
hshift is the number of pixels the dot is shifted horizontally, -ve value to the left, +ve value to the right
vshift is the number of pixels the dot is shifted vertically, -ve value upwards, +ve value downwards
In most cases there is no shift (0 0), but id there are more than 1 dot in the same position, shifting avoids the dots superimposing over and obscuring each other
Dot Radius r sets the radius of the dot in pixels. The diameter of the dot is 2radius+1 pixels. The default radius is 5
Dot Type t where t is either circle or square. The default setting is circle
Plot Dot x y hshift vshift draws the dot, with its parameters (shape size color outline fill) already pre-set
x and y are the data values of the horizontal x variable and vertical y variable, as defined by Plot Values lv tv rv bv
hshift is the number of pixels the dot is shifted horizontally, -ve value to the left, +ve value to the right
vshift is the number of pixels the dot is shifted vertically, -ve value upwards, +ve value downwards
In most cases there is no shift (0 0), but if there are more than 1 dot in the same position, shifting avoids the dots superimposing over and obscuring each other
Drawing text
The color, outline, fill, font, and weight of text are preset (see settings). The default settinfs are sans-sherif, black fill only, and 16pxs high
Bitmap HText x y ha va txt draws text horizontally on the bitmap
x and y are number of pixels fom the left and top borders, and together being the reference coordinate of the text
ha is horizontal adjust
ha=0: the left end of the text is at the x coordinate
ha=1: the center of the text is at the x coordinate
ha=2: the right end of the text is at the x coordinate
va is vertical adjust
va=0: the top of the text is at the y coordinate
va=1: the center of the text is at the x coordinate
va=2: the bottom end of the text is at the x coordinate
txt is the text to be drawn. It must be a single word with no gaps. Spaces can be represented by the underscore _
Plot HText x y ha va txt hshift vshift draws text horizontally on the bitmap
x and y are data values as defined by Plot Values lv tv rv bv, and together being the reference coordinate of the text
ha is horizontal adjust
ha=0: the left end of the text is at the x coordinate
ha=1: the center of the text is at the x coordinate
ha=2: the right end of the text is at the x coordinate
va is vertical adjust
va=0: the top of the text is at the y coordinate
va=1: the center of the text is at the x coordinate
va=2: the bottom end of the text is at the x coordinate
txt is the text to be drawn. It must be a single word with no gaps. Spaces can be represented by the underscore _
hshift is the number of pixels the text is shifted horizontally, -ve value to the left, +ve value to the right
vshift is the number of pixels the text is shifted vertically, -ve value upwards, +ve value downwards
In most cases there is no shift (0 0), but if there are other structures in the same position, shifting avoids the text and structures obscuring each other
Bitmap VText x y ha va txt draws text vertically (90 degrees anticlockwise from horizontal) on the bitmap
x and y are number of pixels fom the left and top borders, and together being the reference coordinate of the text
ha is horizontal adjust
ha=0: the left end of the text is at the x coordinate
ha=1: the center of the text is at the x coordinate
ha=2: the right end of the text is at the x coordinate
va is vertical adjust
va=0: the top of the text is at the y coordinate
va=1: the center of the text is at the x coordinate
va=2: the bottom end of the text is at the x coordinate
txt is the text to be drawn. It must be a single word with no gaps. Spaces can be represented by the underscore _
Plot VText x y ha va txt hshift vshift draws text vertically (90 degrees anticlockwise from horizontal) on the bitmap
x and y are data values as defined by Plot Values lv tv rv bv, and together being the reference coordinate of the text
ha is horizontal adjust
ha=0: the left end of the text is at the x coordinate
ha=1: the center of the text is at the x coordinate
ha=2: the right end of the text is at the x coordinate
va is vertical adjust
va=0: the top of the text is at the y coordinate
va=1: the center of the text is at the x coordinate
va=2: the bottom end of the text is at the x coordinate
txt is the text to be drawn. It must be a single word with no gaps. Spaces can be represented by the underscore _
hshift is the number of pixels the text is shifted horizontally, -ve value to the left, +ve value to the right
vshift is the number of pixels the text is shifted vertically, -ve value upwards, +ve value downwards
In most cases there is no shift (0 0), but if there are other structures in the same position, shifting avoids the text and structures obscuring each other
Other miscellaneous drawings
Bitmap Arc x y radius startDeg endDeg rotate draws an arc.
x and y are number of pixels from the left and top border, and together form the center of the arc
radius is the radius of the arc, in number of pixels
startDeg and endDeg are the degrees (360 degrees in full circle) of the arc
rotate defines the direction of the arc, 0 for clockwise, 1 for anti-clockwise
Bitmap Wedge x y radius startDeg endDeg shift rotate draws a wedge, essentially an arc with lines to the center
x and y are number of pixels from the left and top border, and together form the center of the wedge
radius is the radius of the edge, in number of pixels
startDeg and endDeg are the degrees (360 degrees in full circle) of the wedge
shift is the number of pixels that the wedge is moved centrifugally (away from the center). This is used in pie charts to separate the wedges of the pie
rotate defines the direction of the wedge, 0 for clockwise, 1 for anti-clockwise
Plot Curve a b1 b2 b3 b4 b5 x1 x2 draws a polynomial curve
The curve is y=a + b1x + b2x^{2} + b3x^{3} + b4x^{4} + b5x^{5}. Where higher power is not needed, 0 is used to represent the the coefficient b
The curve is drawn from data value x from x1 to x2
Plot Normal mean sd height draws a normal distribution curve
mean and sd (Standard Deviation) are as in the data horizontal variable variable x
height is the maximum height (where x=mean) of the curve as in the vertical variable y
Color Palettes
Plain Colors
0 0 0 #000000
0 0 63 #00003f
0 0 127 #00007f
0 0 191 #0000bf
0 0 255 #0000ff
0 63 0 #003f00
0 63 63 #003f3f
0 63 127 #003f7f
0 63 191 #003fbf
0 63 255 #003fff
0 127 0 #007f00
0 127 63 #007f3f
0 127 127 #007f7f
0 127 191 #007fbf
0 127 255 #007fff
0 191 0 #00bf00
0 191 63 #00bf3f
0 191 127 #00bf7f
0 191 191 #00bfbf
0 191 255 #00bfff
0 255 0 #00ff00
0 255 63 #00ff3f
0 255 127 #00ff7f
0 255 191 #00ffbf
0 255 255 #00ffff
63 0 0 #3f0000
63 0 63 #3f003f
63 0 127 #3f007f
63 0 191 #3f00bf
63 0 255 #3f00ff
63 63 0 #3f3f00
63 63 63 #3f3f3f
63 63 127 #3f3f7f
63 63 191 #3f3fbf
63 63 255 #3f3fff
63 127 0 #3f7f00
63 127 63 #3f7f3f
63 127 127 #3f7f7f
63 127 191 #3f7fbf
63 127 255 #3f7fff
63 191 0 #3fbf00
63 191 63 #3fbf3f
63 191 127 #3fbf7f
63 191 191 #3fbfbf
63 191 255 #3fbfff
63 255 0 #3fff00
63 255 63 #3fff3f
63 255 127 #3fff7f
63 255 191 #3fffbf
63 255 255 #3fffff
127 0 0 #7f0000
127 0 63 #7f003f
127 0 127 #7f007f
127 0 191 #7f00bf
127 0 255 #7f00ff
127 63 0 #7f3f00
127 63 63 #7f3f3f
127 63 127 #7f3f7f
127 63 191 #7f3fbf
127 63 255 #7f3fff
127 127 0 #7f7f00
127 127 63 #7f7f3f
127 127 127 #7f7f7f
127 127 191 #7f7fbf
127 127 255 #7f7fff
127 191 0 #7fbf00
127 191 63 #7fbf3f
127 191 127 #7fbf7f
127 191 191 #7fbfbf
127 191 255 #7fbfff
127 255 0 #7fff00
127 255 63 #7fff3f
127 255 127 #7fff7f
127 255 191 #7fffbf
127 255 255 #7fffff
191 0 0 #bf0000
191 0 63 #bf003f
191 0 127 #bf007f
191 0 191 #bf00bf
191 0 255 #bf00ff
191 63 0 #bf3f00
191 63 63 #bf3f3f
191 63 127 #bf3f7f
191 63 191 #bf3fbf
191 63 255 #bf3fff
191 127 0 #bf7f00
191 127 63 #bf7f3f
191 127 127 #bf7f7f
191 127 191 #bf7fbf
191 127 255 #bf7fff
191 191 0 #bfbf00
191 191 63 #bfbf3f
191 191 127 #bfbf7f
191 191 191 #bfbfbf
191 191 255 #bfbfff
191 255 0 #bfff00
191 255 63 #bfff3f
191 255 127 #bfff7f
191 255 191 #bfffbf
191 255 255 #bfffff
255 0 0 #ff0000
255 0 63 #ff003f
255 0 127 #ff007f
255 0 191 #ff00bf
255 0 255 #ff00ff
255 63 0 #ff3f00
255 63 63 #ff3f3f
255 63 127 #ff3f7f
255 63 191 #ff3fbf
255 63 255 #ff3fff
255 127 0 #ff7f00
255 127 63 #ff7f3f
255 127 127 #ff7f7f
255 127 191 #ff7fbf
255 127 255 #ff7fff
255 191 0 #ffbf00
255 191 63 #ffbf3f
255 191 127 #ffbf7f
255 191 191 #ffbfbf
255 191 255 #ffbfff
255 255 0 #ffff00
255 255 63 #ffff3f
255 255 127 #ffff7f
255 255 191 #ffffbf
255 255 255 #ffffff
Color Palletes
Table of colors used on this web site
255 255 255 #ffffff
224 224 224 #e0e0e0
128 128 128 #808080
128 0 0 #800000
255 0 0 #ff0000
96 48 96 #603060
48 16 64 #301040
96 96 160 #6060a0
160 160 96 #a0a060
160 160 0 #a0a000
153 191 164 #99bfa4
160 160 96 #a0a060
97 24 0 #611800
204 63 200 #cc3fc8
224 224 224 #e0e0e0
Patterns of complementary colors
A
105 93 70 #695d46
255 113 44 #ff712c
207 194 145 #cfc291
161 232 217 #a1e8d9
255 246 197 #fff6c5
B
115 0 70 #730046
201 60 0 #c93c00
232 136 1 #e88801
255 194 0 #ffc200
191 187 17 #bfbb11
C
97 24 0 #611800
140 115 39 #8c7327
71 164 41 #47a429
153 191 164 #99bfa4
242 239 189 #f2efbd
D
20 87 110 #14576e
140 33 90 #8c215a
230 133 38 #e68526
195 102 163 #c366a3
242 207 242 #f2cff2
E
64 1 1 #400101
48 115 103 #307367
96 166 133 #60a685
242 236 145 #f2ec91
229 249 186 #e5f9ba
F
55 89 21 #375915
166 60 60 #a63c3c
115 108 73 #736c49
166 157 129 #a69d81
242 224 201 #f2e0c9
G
115 36 94 #73245e
166 69 33 #a64521
217 182 78 #d9b64e
242 218 145 #f2da91
242 242 242 #f2f2f2
H
255 77 0 #ff4d00
102 87 71 #665747
125 179 0 #7db300
153 138 122 #998a7a
217 195 98 #d9c362
I
128 0 38 #800026
128 128 83 #808053
92 153 122 #5c997a
163 204 143 #a3cc8f
255 230 153 #ffe699
Explanations
A common statistical problem is to describe a relationship between two measurements that is not linearly related
(not in a straight line).
When such a relationship can be mathematically defined, such as one is the square or square root of the other, the variables can be transformed mathematically before a relationship established by linear regression
Often however, a curved relationship that exists may appear regular and consistent, but a mathematical definition of that relationship is not available, and an empirical "best fit" algorithm is required. A common method of doing this is the polynomial cuve fit, with the formula
y = a + b_{1}x + b_{2}x^{2} + b_{3}x^{3} + b_{4}....
Each increase in power bends the relationship into a sharper curve, the combination of all the coefficients will be able to produce a curve of potentially any level of complexity. In bio-social science, however, curve fitting beyond the third power is seldom necessary or meaningful.
Polynomial curve fitting can be carried out using multiple regression, where the x variable is expanded by its powers. This is a common procedure in the biochemical laboratory, where the depth of a color reaction is related to the concentration of a chemical of interest
When polynomial curve fitting is used to created reference charts such as growth charts for weights and heights of children, more than the mean regression curve line is required. There is a need to also knowing the Standard Error (SE), so that deviation from that central line can be meaningfully interpreted.
Using the traditional analysis of variance to estimate SE is problemattical for polynomial curve fit. Firstly, the variances from the coefficients of different power need to be integrated, and the complexity involved may themselves generate random variations. Secondly, in many growth type situations, the SE changes with size, and relying on the assumptions of the analysis of variance model may produce misleading interpretations.
Altman (see reference) described a two stage procedure that side stepped these problem. Instead of estimating SE from a theoretical construct, the algorithm firstly curve fit the mean regression line, then a second curve fit to estimate the SE around that mean line. In other words, the curve fit of the mean curve line and its Standard Error are both modelled from the data itself.
The algorithm is clearly described in Altman's paper, and also in the javascript and R programs presented on this page. The example from this page may help to follow these procedures.
Example
Please note: that the data in this example is artificially generated to demonstrate the procedures and not real. Also the sample size is deliberately small to make visualization easier. In any growth chart, hundreds if not thousands of cases are required.
X (weeks)
Y (grams)
Fitted Y
SE Y
CI Low
CI High
z
Percentile
37
3048
3080.64
103.72
2877.35
3283.92
-0.31
37.7
36
2813
2795.18
98.64
2601.85
2988.52
0.18
57.2
41
3622
3626.30
45.76
3536.60
3715.99
-0.09
46.3
36
2706
2795.18
98.64
2601.85
2988.52
-0.90
18.3
35
2581
2428.70
85.74
2260.65
2596.76
1.78
96.2
39
3442
3451.29
90.39
3274.13
3628.45
-0.10
45.9
40
3453
3557.90
71.99
3416.80
3699.00
-1.46
7.3
37
3172
3080.64
103.72
2877.35
3283.92
0.88
81.1
35
2386
2428.70
85.74
2260.65
2596.76
-0.50
30.9
39
3555
3451.29
90.39
3274.13
3628.45
1.15
87.4
37
3029
3080.64
103.72
2877.35
3283.92
-0.50
30.9
37
3185
3080.64
103.72
2877.35
3283.92
1.01
84.3
36
2670
2795.18
98.64
2601.85
2988.52
-1.27
10.2
38
3314
3295.77
100.97
3097.88
3493.66
0.18
57.2
41
3596
3626.30
45.76
3536.60
3715.99
-0.66
25.4
38
3312
3295.77
100.97
3097.88
3493.66
0.16
56.4
38
3414
3295.77
100.97
3097.88
3493.66
1.17
87.9
41
3667
3626.30
45.76
3536.60
3715.99
0.89
81.3
40
3643
3557.90
71.99
3416.80
3699.00
1.18
88.1
33
1398
1409.86
36.47
1338.38
1481.34
-0.33
37.3
38
3135
3295.77
100.97
3097.88
3493.66
-1.59
5.6
39
3366
3451.29
90.39
3274.13
3628.45
-0.94
17.3
The table to the right contains input data and calculated results (to 2 decimal point precision for clarity)
Example: We wish to develop a growth chart for birth weights of babies (in grams), according to the gestational age (in weeks). For this exercise, we used the data from 22 new born babies, the measurements are as presented in the columns 1 and 2 of the table to the right. The independent variable, X, is gestational age in weeks since the beginning of pregnancy. The dependent variable, Y, is birth weight in grams.
To set the parameters for calculations, we understand that the relationship between gestation and weight may be a complex curve, and that variation may also changed with gestation. After some trials and errors, we decided to curve fit Y (grams) against X (weeks) to the power of 3, and the Standard Error around that curve to the power of 2. We then described the 95%
confidence of the fitted line.
Step 1: Curve fit X and Y: We perform multiple regression curve fit to the power of 3 between X (gestational age in weeks) and Y (birth weight in grams), and produced the following euation
The fitted values for each x values are in column 3 of the table to the right
Step 2: Curve fit X and Standard Error (SE) around Fitted Y: The absolute difference between each Y and fitted Y values are used to curve fit agains X to the power of 2. The result coefficients are then multiploed by π/2 to produce the coefficients for Standard Error around the fitted Y. The result coefficients are
SE of fitted Y: SE Y = -5295.4235 + 290.6838x^{1} + -3.9125x^{2}
From the SE values, the 95% confidence interval (±1.96SE) can also be calculated.
The SE and the two limits of the 95% confidence interval are then calculated and presented as columns 4, 5, and 6 of the table above and to the right.
Step 3: Evaluate any x, y pair using the curve fitted coefficients: The following procedures can be used to correct any outcome y value by its paired x value, using the two sets of curve fit coefficients. The values from the first row of the table above and to the right is used in the following demonstration
The gestation in weeks (X) = 37, the birth weight in grams (Y) = 3848
Fitted SE Y = -5295.4235 + 290.6838(37^{1}) + -3.9125(37^{2}) = 103.7172
The relationship between Y and Fitted Y, z = (y - Fitted Y) / SE Y = (3848 - 3080) / 103.7172 = - 0.3147. In other words, Y is 0.31 Standard Errors less than the fitted Y value
z can be transformed into probability, hence to percentile. p(z=-0.31) = 0.377 = 37.7 percentile. In other words, in the context of the curve fit, 3048 grams is 37.7 percentile (mean being z=0 and 50 percentile)
All the input values of X and Y are so evaluated, and the results are in columns 7 and 8 of the table above and to the right.
Step 4: A Javascript Code to calculate Y from X: The whole point of performing a curve fit is to produce coefficients from a set of reference data, and use these to calculate y values from x values in the future. Although the calculations are conceptually simple, they are nevertheless tedious, especially if they have to be carried out repeatedly. The program on this page therefore produces a short Javascript program which will do these calculations, including the coefficients calculated from the input data, to produce Y and its 95% confidence interval from any X value.
User can copy and paste the codes into any text editor, and save the file as a html page. The program can then be used on any web browser. Some users may further elaborate on the codes to improve its presentation, and to adapt it to use in bulk calculations.
Step 5: Plotting the data and the results: The data points, and the 3 curve fitted lines (mean and the confidence limits) can be plotted in a scatter plot, as shown to the right
References
Altman DG (1993) Constructing age-related reference centiles using absolute residuals. Statistics in Medicine 12(10):917-924
Data Entry: Data is a table with 2 columns
- Each row a data point
- Col 1 is the x or independent variable
- Col 2 is the y or dependent variable
Two power parameters for the polynomial fit are provided
one for the mean, the other for Standard Error
Note: Power >3 seldom needed for bio-social data
Power for curve fit of mean
Power for curve fit of SE
Percentage Confidence Interval
MacroPlot Code
R Code
Curvefit (ROC)Altman's algorithm Ref: Altman DG (1993) Constructing age-related reference centiles using absolute residuals.
Statistics in Medicine 12(10):917-924
The following is a single program, but divided into parts so it is easier to follow
Part 1: Subroutine to calculate cueve fit
The function calculates a vector of Y values from a vector of curve fit coefficients and a vector of x values, where
y = coef[0] + coef[1]x^{1} + coef[2]x^{2} + coef[3]x^{3} ...
# subroutine to calculare vector of Y from vector of X using coefficient vector
CalCurveFitValues <- function(coefVec, datVec)
{
f = length(coefVec) # length of coefficient vector
n = length(datVec) # length of data vector (X)
vecResult <- vector() # result vector (Y)
for ( i in 1:n)
{
x = datVec[i]
y = coefVec[1] + coefVec[2] * x
for (j in seq(3,f, by=1)){ y = y + coefVec[j] * x^(j-1) }
vecResult[i] = y
}
vecResult
}
Part 2: Main program starts: Data entry
# data entry
myDat = ("
X Y
37 3048
36 2813
41 3622
36 2706
35 2581
39 3442
40 3453
37 3172
35 2386
39 3555
37 3029
37 3185
36 2670
38 3314
41 3596
38 3312
38 3414
41 3667
40 3643
33 1398
38 3135
39 3366
")
# Set power of polynomial curve fitting
pwLine = 3 # power of fitting the line # power of curve fit for line
pwSE = 2 # power of fitting the SE # power of curve fit for Standard Error
cfInt = 95 # % confidence interval # % of confidence for confidence intervals
# Calculate z for 2 tails
z = qnorm((100 - (100 - cfInt) / 2) / 100) # converting % confidence into z for calculating confidence intervals
# create dataframe from input data dfis the dataframe
df <- read.table(textConnection(myDat),header=TRUE)
#df # optional display of input data
Part 3: Curve fit the main line
# Curve fit the line
resLine<-lm(formula = df$Y ~ poly(df$X, pwLine, raw=TRUE))
# summary(resLine) # optional R display of curve fit results
# Extract Coefficients
coefLine <- coef(summary(resLine))[1:(pwLine+1)]
coefLine # output coefficients for fitted Y value
The vector is displayed as follows. The first item is the constant, the rest the coefficients for x^{1}, x^{2}, x^{3}
> coefLine # output coefficients for fitted Y value
[1] -144612.43666 10177.18075 -233.18266 1.78399
The regressed values (RegY) for each x value of the input data is then calculated and add to the data frame
# calculate regressed Y value
df$RegY <- CalCurveFitValues(coefLine,df$X)
#df optional testing showing the fitted Y value
Part 3: Curve fit the Standard Error
The absolute difference between the Y value and fitted Y value (RegY) is used as the dependent variable, and curve fitted against X. The result formula is then multiplied by π/2 to result in the coefficients for the Standard Error
# Curve fit SE
vecAbsDif <- abs(df$RegY - df$Y) # absolute difference between fitted Y and Y
#vecAbsDif # optional test for difference
resSE<-lm(formula = vecAbsDif ~ poly(df$X, pwSE, raw=TRUE)) # curve fit abs(difference) to X
#summary(resVar) # optional test show interim results
# Extract Coefficients
coefSE <- coef(summary(resSE))[1:(pwSE+1)]
coefSE <- coefSE * sqrt(pi / 2) # coefficients for SE at x level
coefSE # output coefficients for calculate SE depending on x
The coefficients for calculating SE of fitted Y according to x values are shown.
The first item is the constant, the rest the coefficients for x^{1}, x^{2}
> coefSE # output coefficients for calculate SE depending on x.
[1] -5295.423424 290.683788 -3.912461
Part 4: Interpret input data using the fitted coefficients
#Calculate SE z and percentile of data for each input pair of x and y
df$SE <- CalCurveFitValues(coefSE,df$X) # SE for x
df$CILow <- df$RegY - z * df$SE # confidence interval for fitted Y (low)
df$CIHigh <- df$RegY + z * df$SE # confidence interval for fitted Y (high)
df$z <- (df$Y - df$RegY) / df$SE # (Y - fitted Y) / SE
df$Pctile <- round(pnorm(df$z) *100, 1) # z converted to percentile
df # show input data and all calculated values
The results are the appended table, as shown
X and Y the entered values of x and y
RegY is the curve fitted y value for x
SE is the Standard Error of RegY at that x value
CIlow and CIHigh are the two ends of the confidence interval of RegY at that x value
z is the standard deviate of the original y value compared to RegY z = (Y - RegY) / SE
The data points and the regression line _ confidence intervals are plotted
# Plotting
# Create vectors of x and y for calculation of line coordinates
# divided into 50 intervals
#Produce table
minv = min(df$X)
maxv = max(df$X)
arX <- seq(from = minv, to = maxv, length.out = 50) # 50 x values from min to max in sequence
arY <- CalCurveFitValues(coefLine, arX) # Fitted y values to x
arSE <- CalCurveFitValues(coefSE, arX) # Standard Error according to x
arYLow <- arY - z * arSE # array of y for lower end of confidence interval
arYHi <- arY + z * arSE # array of y for higher end of confidence interval
# plotting begins
par(pin=c(4.2, 3)) # set plotting window to 4.2x3 inches
plot(x = df$X, # x
y = df$Y, # y
pch = 16, # size of dot
xlab = "X", # x label
ylab = "Y") # y lable
lines(arX,arY) # line for fitted y RegY
lines(arX,arYLow) # line for lower end of confidence interval
lines(arX,arYHi) # line for higher end of confidence interval
The result plot is as shown in the following plot
The dots are the input values of x and y
The 3 lines are the fitted y (RegY) values in the center, and the confidence intervals. In this example it is the 95% confidence interval