Tcl Library Source Code

View Ticket
Login
Ticket UUID: 4dd68ce4e76841f439a76a7eae154b5865604477
Title: ::math::statistics::linear-model does not return correct t value
Type: Bug Version: 1.6.1
Submitter: torstenberg Created on: 2026-03-25 14:56:23
Subsystem: math :: statistics Assigned To:
Priority: 5 Medium Severity: Severe
Status: Open Last Modified: 2026-04-21 10:23:47
Resolution: None Closed By: nobody
    Closed on:
Description:
Comparing what ::math::statistics::linear-model does with what other packages do (R, Excel).

Tcl has 

```
set x {0.471879494 0.728760378 0.367830807 0.61828918 0.489196527 0.917088418 0.968941501 0.423200725 0.987511476 0.583237722 0.757929926 0.480875506 0.619316266 0.5936312 0.468497921 0.726142026 0.346356636 0.16034445 0.734479481 0.165877529 0.676545099}
set y {0.641566349 0.64989831 0.352843078 0.628259922 0.086219655 0.918209315 1.012767099 0.423609071 0.651334843 0.575540881 0.487839245 0.459034775 0.679854928 0.381313691 0.430965814 0.45235075 0.350328068 0.176799222 0.486165879 0.325428968 0.551472929}

set linmod1 [::math::statistics::linear-model $x $y]

lassign $linmod1 intercept slope - Rsqr ndf SEintercept - SEslope tval

proc  pvalue {t ndf {twotailed 1}} {
	# t   		- t-value (e.g. returned by ::math::statistics::linear-model)
	# ndf 		- number of degrees of freedom
	# twotailed - if the test is two-tailed
	
	# currently, this procdure assumes a two-tailed t-test is used, 
	# as in the linear-model procedure. if this procedure is to be 
	# used for one-tailed t-values, then it needs to be modified!
	
	set t [expr {abs($t)}]
	set cdf [::math::statistics::cdf-students-t $ndf $t]
	set p [expr {2 * (1 - $cdf)}]
	
	return $p
}

puts "tval:        $tval"
puts "pval:        [pval $tval $ndf]"
```

and the output is:

- tval:        21.648211295413816
- pval:        0.0


From R, I get:

- tval: 4.966
- pval: 8.57e-05

and from Excel:

- tval: 4.96644
- pval: 0.00008569028918


So, the t value is not correct and leads to a wrong p value.

The fix is easy, I will provide a branch for this.
User Comments: oehhar added on 2026-04-21 10:23:47:

Looks good. Maybe, Arjen may double check the math? Otherwise, merging is ok.

Thanks for all, Harald


torstenberg added on 2026-03-25 15:05:58:

I have provided a fix for this in branch tkt-4dd68ce4e76841f.

A test case has been introduced into statistics.test as well.

I would appreciate if someone could review my change and approve this patch.