Skip to main content
MedCalc
Mail a PDF copy of this page to:
(Your email address will not be added to a mailing list)
working
Show menu Show menu

Regression - confidence intervals

Scripts example - Regression - create table with confidence intervals

This script calculates the confidence interval for a range of values, based on linear regression analysis. The results are presented in a formatted table.

// regional settings
$decimalsymbol=".";
$listseparator=",";
//
cls;
clear data;
<data>
Timepoint	Result
0	0.11
3	0.17
6	0.31
9	0.4
12	0.5
18	1.03
24	1.06
</data>
varx		=TimePoint;
vary		=Result;
slope		=VSLOPE(varx,vary);
intercept	=VINTERCEPT(varx,vary);
n			=VCOUNT(varx,ISNUMBER(vary));
meanX		=VAVERAGE(varx);
devsqX		=VDEVSQ(varx);
steyx		=VSTEYX(vary,varx);
alpha		=0.05;
t			=TINV(alpha,n-2);

table(tab);
AppendRow(tab,"x","y","CI Low","CI High");
x=0;
while (x<=24) {
	y=intercept+x*slope;
	s=steyx*sqrt(1/n+(x-meanX)^2/devsqX);
	lo=y-t*s;
	hi=y+t*s;
	AppendRow(tab,x,y,lo,hi);
	x=x+2;
}
FormatTable(tab,"3");
FormatTableColumn(tab,1,"0");
FormatTableRow(tab,1,"shaded bold 0");
?tab;

Output

xyCI LowCI High
00.053-0.1050.212
20.1420.0030.282
40.2310.1080.354
60.3210.2110.430
80.4100.3100.510
100.4990.4030.595
120.5880.4900.686
140.6770.5710.783
160.7660.6470.885
180.8550.7200.990
200.9440.7911.097
221.0330.8611.206
241.1220.9291.316

See also