Content Disclaimer Copyright @2020. All Rights Reserved. |

**Links : **Home
Index (Subjects)
Contact StatsToDo

MacroPlot Resources
Orientation
Macroplot plotting is controlled by the macros in the text area provided.
Macros
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)
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
**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
Plot, and the coordinates are the values in the data
This panel lists and describes all macros used in this version of MacroPlot by Javascript. They are divided into the following sub-panels
Color Palettes
- Initialization and settings
- Plotting areas, coordinates used, and drawing of x and y axis
- Drawing lines, bars, dots, text, and other shapes
This sub-panel lists those macros that initialized the bitmap, and set the parametrs for drawing
Axis & Coordinates
## Initialize PlottingBitmap 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 linesThe settings provide parameters for all subsequent plotting until the parameter is reset
## Settings for fillsWhen bars, dots, arcs and wedges are plotted, the interior of these symbols are called fills, and they are set as follows
- 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 fontsThese 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
- 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.
This sub-panel presents macros that define the plotting areas, and creating the x and y axis for plotting
Drawings
## Drawing on the bitmapWhen 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 areaIn 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 keywordPlot, and are purposes are as follows
- 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
- 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
- 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 axisThe following 4 macros are sufficient to draw the x and y axis under most circumstances
- 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 axisUsers may wish to draw individual part of the axis, and the following macros can be used
- 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
- 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
- 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
- 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
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 linesThe thickness and color of any line drawn is set by theLine macros (see setting sub-panel). The default setting is black line 3 pixels in width
- x1 and x2 are number of pixels from the left border
- y1 and y2 are number of pixels from the top border
- 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 barsThe color and thickness of the outline are defined in theLine 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.
- 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
- 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)
- 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 dotsThere 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
- 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
- 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
- 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 textThe color, outline, fill, font, and weight of text are preset (see settings). The default settinfs are sans-sherif, black fill only, and 16pxs high
- 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 _
- 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
- 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 _
- 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
- 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 drawingsBitmap 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
Plain Colors
Table of colors used on this web site
Patterns of complementary colors
Explanations
Introduction
Javascript Program
General discussions on sequential analysis are presented in Sequential_Exp.php and
quality statisitcs in the Quality_Exp.php, and they are not repeated here.
SPRT for a Mean
This page presents the The model aims to determine the quality of a batch of products by minimal sampling. The idea is to sample the batch sequentially until a decision can be made whether the batch conforms to specification and can be accepted, or that the specification is significantly violated and the batch should be rejected.
**α :**, also represented as alpha, or p, is the probability of Type I Error. Commonly, p<0.05 or p<0.01 is used as the criteria to reject the null hypothesis**β :**is the probability of Type II Error. Commonly, β<0.2 is used at the planning stage to determine sample size or calculating borders for sequential analysis.**Power :**is 1 - β, a concept intuitively easier to understand, and represents the ability to detect a difference, if its really there. A power of 0.8 (80%) is usually used as this is the same as β=0.2**Value to accept null hypothesis :**is the lower value (proportion or mean) below which the decision to accept the null hypothesis (not significantly different from zero) can be made.**Value to reject null hypothesis :**is the higher value (proportion or mean) above which the decision to reject the null hypothesis (significantly different from zero) can be made.**k :**was used by Wald to represent the effect size, and is equivalent to θ which is now more commonly used.**Decision borders :**are two parallel lines drawn on the sequential chart. Data are plotted as they are sampled. Sampling continues while the plot coordinates are between the two decision lines. Sampling stops when the plot coordinates are outside of the two lines. If it is above the rejection line then the null hypothesis is rejected. If it is below the acceptance line then the null hypothesis is accepted.**Truncation :**This is the maximum number of samples. Sampling stops at this point even if the coordinate still fall between the two lines. The null hypothesis is usually then accepted. In some algorithms (such as that on these pages, Lines are drawn between the two borders to a midpoint at truncation, and the decision to reject or accept the null hypothesis is made according to which line the last data point crosses.
## Sequential Probability Ratio Test (SPRT) for a MeanData Input : In addition to α and power that are common inputs to both models, the following inputs are used
**Standard Deviation**is the expected Standard Deviation of the samples to be taken. In quality control this value is usually known from past history**Mean (accept null hypothesis):**is the mean value below which a decision that the mean is null (zero) can be made. In quality control, to detect defects in production, this is the mean of departure from expected measurements that can be accepted as normal variations.**Mean (reject null hypothesis):**is the mean value above which a decision that the mean is not null (zero) can be made. In quality control, to detect defects in production, this is the mean value of departure from expected values which will trigger an alert that something is amiss.
Example:
We purchase ball bearings in batches in our manufacturing business. Each batch is inspected to make sure the quality complies with our needs. The ball bearing should be 1cm in diameter, with an expected Standard Deviation of 1mm. We decided thst if the average departure from 1cm is less than 0.5 mm, we will accept the batch. However if the departures are greater than 1.5mm, we will reject the batch. We will use α=0.05 and power of 0.8 for testing. From the parameters, the decision borders can be drawn. Each batch is then randomly sampled, and the cumulative departure from 1 cm plotted and compared with the borders. The testing for a batch is shown in this example. The departure from 1cm in the samples are as shown in the column to the right. The plot for decision borders and the cumulative sum of departures are shown in the diagram to the left It can be seen, on the 15th sample, the cumulative sum line crossed the lower acceptance border. The sampling therefore can stop, and the batch accepted as conforming to specifications. ## Sequential Probability Ratio Test (SPRT) for a proportion
Data Input : In addition to α and power that are common inputs to both models, the following inputs are used
**Proportion (accept null hypothesis) p0:**is the proportion of positives below which a decision that the proportion is null (zero) can be made. In quality control, to detect defective items in a batch of products, this is the proportion of defectives below which the batch can be accepted by the client.**Proportion (reject null hypothesis) p1:**is the proportion of positives above which a decision that the proportion is not null (zero) can be made. In quality control, to detect defective items in a batch of products, this is the proportion of defectives above which the batch will not be accepted by the client.
Example: We will quality control the shape of the ball bearings we purchased using SPRT for proportions.The ball bearings are sampled for defects. We decided that we will rejct the batch if the defective rate exceeds 10% (0.1), and accept the batch if the defective rate is less than 1% (0.01). We will use α=0.05 and power=0.8 The decision borders can be drawn using the parameters. The cumulative number of defective items can then be ploted and compared to the border. The column to the right shows the results of random sampling, 0=non-defective and 1=defective. The diagram to the left shows the borders and the cumulative number of defective items samples from a batch It can be seen that the line for cumulative number of defective ball bearings crosed the rejection line at the 30th sample, when the third defective item was found. At that point sampling can stop, and the batch rejected as having more defective items than specified. ## ReferencesWald A (1947) Sequential Analysis. John Wiley & Son, Inc, New York. (Original book)https://ia601603.us.archive.org/11/items/in.ernet.dli.2015.510091/2015.510091.Sequential-Analysis.pdf Wald's book as pdf file on internet archive https://en.wikipedia.org/wiki/Sequential_probability_ratio_test is a complete and excellent explanation of Wald's SPRT, including the formulae used.
MacroPlot Code
This panel provides R codes for the two Sequential Probability Ratio Tests (SPRT)
## SPRT for mean# Pgm 1: SPRT for means # Parameters alpha = 0.05 # Probability of Type I Error (α) power = 0.8 # power (1 - β) sd = 1.0 # Expected Standard Deviation of the measurement reject = 1.5 # mean of measurements for rejecting null hypothesis (exceed tolerable error) accept = 0.5 # mean of measurements for accepting null hypothesis (within tolerable error) # data entry # Sequence of measurements arVal <- c(1.4,1.1,0.7,0.2,1.3,1.4,1.2,2.0,0.3,0.1,1.4,1.2,0.3,1.1,0.5) # values # calculations rows = length(arVal) # sample size beta = 1 - power # Probability of Type II Error β top = -log(beta / (1.0 - alpha)) * log((1 - beta) / alpha) bot = (reject - accept)^2 n = ceiling(top / bot * sd^2) # expected sample size x 3 xend = n * 3 # truncated for sequential testing maxx = xend # max x for plotting if(rows>maxx) maxx = rows; # Coefficients s = (accept + reject) / 2.0 # slope of decision border h0 = sd^2 / (reject - accept) * log(beta / (1.0 - alpha)) # constant for acceptance border h1 = sd^2 / (reject - accept) * log((1.0 - beta) / alpha) # constant for rejecton border # Create vectors for plotting decision borders # rejection line rejX <- c(0, (xend - 1)) # rejection line x1, x2 rejY <- c(h1, (h1 + s * (xend - 1))) # rejection line y1, y2 maxy = rejY[2] # max y for plot # acceptance line #xstart = 0 #if (h0<0){ xstart = abs(h0 / s) } # accptance line begins where y=0 #xstart = 0 xstart = abs(h0 / s) # accptance line begins where y=0 accX <- c(xstart, (xend - 1)) # acceptance line x1, x2 accY <- c(0, (h0 + s * (xend-1))) # acceptance line y1, y2 # Extension of decision border beyund truncation if no decision at truncation y1X <- vector() y1Y <- vector() y2X <- vector() y2Y <- vector() y3X <- vector() y3Y <- vector() y1 = h1 + s * (xend - 1) y2 = h0 + s * (xend - 1) y3 = (h1 + h0) / 2 + s * xend y1X <- c((xend-1), xend) # x1, x2 for rejection line converging towards average with acceptance line y1Y <- c(y1, y3) # y1, y2 for rejection line converging towards average with acceptance line y2X <- c((xend-1), xend) # x1, x2 for acceptance line converging towards average with rejection line y2Y <- c(y2, y3) # y1, y2 for acceptance line converging towards average with rejection line if(rows>xend) # Single decision line following end of truncation { y3X <- c(xend, rows) # x1, x2 for single decision line y3Y <- c(y3, ((h1 + h0) / 2 + s * rows)) # y1, y2 for single decision line } # Create data plotting array arX <- vector() # x values, number of observations arY <- vector() # y value, cumulative sum of measurements cusum = 0 for(i in 1 : length(arVal)) { arX <- append(arX,i) cusum = cusum + arVal[i] arY <- append(arY,cusum) } y = arY[length(arY)] if(y>maxy){maxy = y} #arX# adjust max y for plotting # Output results of calculations c(rows, xend) # sample size and end of plot c(s, h0, h1) # slope, constant(accept) , constant (reject) of decision borderThe initial output of the decision borders are as follows > # Output results of calculations > c(rows, xend) # sample size and end of plot [1] 15 15 > c(s, h0, h1) # slope, constant(accept) , constant (reject) of decision border [1] 1.000000 -1.558145 2.772589 Plotting: The decision borders and the cumulative sums are plotted as follows
# plot all calculations par(pin=c(4.2, 3)) # set plotting window to 4.2x3 inches plot( xlim = c(0,maxx), ylim = c(0,maxy), x = arX, # x = n observations y = arY, # y = cum sum type = "b", pch = 16, # size of dot xlab = "Number of Measurements", # x label ylab = "Cumulative Sum") # y lable #lines(arX, arY) # line joining dots lines(rejX, rejY) # rejection line lines(accX, accY) # acceptance line line lines(y1X, y1Y) # reject tail lines(y2X, y2Y) # accept tail lines(y3X, y3Y) # beyound tailThis results in the plot to the right ## Program 2: SPRT for proportion# Program 2: SPRT proportions # parameters alpha = 0.05 # Probability of Type I Error ?? power = 0.8 # power (1 - ??) accept = 0.01 # proportion for accepting null hypothesis (within tolerable error) reject = 0.1 # proporiion for rejecting null hypothesis (exceed tolerable error) arVal <- c(0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1) # 0=negative 1 = positive # calculations rows = length(arVal) # sample size beta = 1 - power # probability of Type II error ?? top = -log(beta / (1.0 - alpha)) * log((1 - beta) / alpha) bot = log(reject / accept) * log((1.0 - accept) / (1.0 - reject)) n = ceiling(top / bot) # expected truncation for decisions xend = n * 3; maxx = xend if(rows>maxx){ maxx = rows} # adjusted to sample size k = log((reject * (1.0 - accept)) / (accept * (1.0 - reject))) s = log((1.0 - accept) / (1.0 - reject)) / k s # slope of decision border h0 = -log((1.0 - alpha) / beta) / k # constant for acceptance line h1 = log((1.0 - beta) / alpha) / k # constant for rejection line rejX <- c(0, (xend - 1)) # rejection line x1, x2 rejY <- c(h1, (h1 + s * (xend - 1))) # rejection line y1, y2 maxy = rejY[2] # max y for plot # acceptance line xstart = abs(h0 / s) # accptance line begins where y=0 accX <- c(xstart, (xend - 1)) # acceptance line x1, x2 #accY <- c((s * xstart - h0), (s * (xend - 1) - h0)) # acceptance line y1, y2 accY <- c(0, (s * (xend - 1) - h0)) # acceptance line y1, y2 # Extension of decision border beyund truncation if no decision at truncation y1X <- vector() y1Y <- vector() y2X <- vector() y2Y <- vector() y3X <- vector() y3Y <- vector() y1 = h1 + s * (xend - 1) y2 = s * (xend - 1) + h0 y3 = (h1 + h0) / 2 + s * xend y1X <- c((xend-1), xend) # x1, x2 for rejection line converging towards average with acceptance line y1Y <- c(y1, y3) # y1, y2 for rejection line converging towards average with acceptance line y2X <- c((xend-1), xend) # x1, x2 for acceptance line converging towards average with rejection line y2Y <- c(y2, y3) # y1, y2 for acceptance line converging towards average with rejection line if(rows>xend) # Single decision line following end of truncation { y3X <- c(xend, rows) # x1, x2 for single decision line y3Y <- c(y3, ((h1 + h0) / 2 + s * rows)) # y1, y2 for single decision line } # plotting array arX <- vector() arY <- vector() cusum = 0 for(i in 1 : length(arVal)) { arX <- append(arX,i) # x = number of observarions cusum = cusum + arVal[i] arY <- append(arY,cusum) # y = cumulative sum of numbers with positives } y = arY[length(arY)] if(y>maxy){maxy = y} # adjust max y value for plotting # Output results of calculations c(rows, xend) # sample size and end of plot c(s, h0, h1) # slope, constant(accept) , constant (reject) of decision borderThe initial output of the decision borders are as follows > # Output results of calculations > c(rows, xend) # sample size and end of plot [1] 30 60 > c(s, h0, h1) # slope, constant(accept) , constant (reject) of decision border [1] 0.03974743 -0.64979678 1.15625931 Plotting
# plot all calculations par(pin=c(4.2, 3)) # set plotting window to 4.2x3 inches plot( xlim = c(0,maxx), ylim = c(0,maxy), x = arX, # x = n observations y = arY, # y = cum sum type = "b", pch = 16, # size of dot xlab = "Number of Measurements", # x label ylab = "Cumulative Sum") # y lable #lines(arX, arY) # line joining dots lines(rejX, rejY) # rejection line lines(accX, accY) # acceptance line line lines(y1X, y1Y) # reject tail lines(y2X, y2Y) # accept tail lines(y3X, y3Y) # beyound tailThe plot is as shown to the right |