Linear Regression Calculator

Simple Linear Regression Calculator, helps to calculate the relationship between two variables by the given equation.

Features

  • Access the association between two variables in a straight line known as Line of Best fit.
  • Computes the value of slope, y-intercept and regression equation.
  • The calculation is based on x and y variables where one variable is dependent and another is independent.
  • Simple, responsive and easy to integrate.

Preview

Calculate Simple / Linear Regression Line

To Calculate Regression:
X Value Y Value
Add Remove
©h

Downloads

Code


<!-- Script by hscripts.com -->
<!-- More scripts @www.hscripts.com -->
<html>
<head>
<title>Linear Regression Calculator | Simple Linear Regression in Javascript </title>
<script LANGUAGE="JavaScript">
function chk(){
var sds = document.getElementById('dum');
if(sds == null){alert("You are using a free package.\n You are not allowed to remove the tag.\n");
document.getElementById("maindiv").style.visibility="hidden";
}
var sdss = document.getElementById("dumdiv");
if(sdss == null){alert("You are using a free package.\n You are not allowed to remove the tag.\n");}
element = document.getElementById('dell');
element.style.display = "none";
}
window.onload=chk;
function checnum(as)
{
var dd = as.value;
if(dd.lastIndexOf(" ")>=0){dd = dd.replace(" ","");as.value = dd;
}
if(isNaN(dd))
{
dd = dd.substring(0,(dd.length-1));
as.value = dd;
}
}
var x=2;
function add()
{
element = document.getElementById('dell');
element.style.display = "block";
var s = document.getElementById("a").getElementsByTagName("TBODY")[0];
var trow = document.createElement("tr");
var tcol1 = document.createElement("td");
var tcol2 = document.createElement("td");
var d = document.createElement("input");
d.setAttribute("type","text");
d.setAttribute("size","10");
d.setAttribute("id","xvalu"+x);
d.setAttribute("onkeyup","checnum(this)");
d.style.backgroundColor= "#ffffff";
d.style.color = "#000000";
var e = document.createElement("input");
e.setAttribute("type","text");
e.setAttribute("size","10");
e.setAttribute("id","yvalu"+x);
e.setAttribute("onkeyup","checnum(this)");
e.style.backgroundColor= "#ffffff";
e.style.color = "#000000";
tcol1.appendChild(d);
tcol2.appendChild(e);
trow.appendChild(tcol1);
trow.appendChild(tcol2);
s.appendChild(trow);
if(x>20){
element = document.getElementById('addd');
element.style.display = "none";
}
x++;
}
function del()
{
element = document.getElementById('dell');
element.style.display = "block";
if (x<=3) {
element = document.getElementById('dell');
element.style.display = "none";
}
if(x>2){
var len = x.length;
var s = document.getElementById("a").getElementsByTagName("TBODY")[0];
s.removeChild(s.lastChild);
x=x-1;
}
}

function calculate()

{aTemp = document.getElementById("xvalu0").value;

bTemp = document.getElementById("yvalu0").value;

if((aTemp=="")||(bTemp=="")){alert("Enter All Fields");return false;}

else{
var aa = 0;var bb = 0;var cc = 0;var dd = 0;var ee = 0;for(var y=0;y<x; y++){var xxx = document.getElementById("a").getElementsByTagName("TBODY")[0];var a = xxx.getElementsByTagName("input").item(y*2).value;var b = xxx.getElementsByTagName("input").item(1+y*2).value;if((a=="")||(b=="")){alert("Enter All Fields");return false;}else{aa = aa + parseFloat(a);bb = bb + parseFloat(b);cc = cc + (a*b);dd = dd + (a*a);
var f = ((x*cc)-(aa*bb));var g = ((x*dd)-(aa*aa));if(g==0){document.getElementById("r1").value = x;
document.getElementById("r2").value = "Invalid";

document.getElementById("r3").value = "Invalid";

document.getElementById("r4").value = "Invalid";}else{

var ff = f/g;var gg = (bb-(ff*aa))/x;

var ff1 = Math.round(ff*100000)/100000;

var gg1 = Math.round(gg*100000)/100000;

if(ff < 0)var hh = Math.round(gg1*1000)/1000 + "" + Math.round(ff1*1000)/1000 + "x";
else

var hh = Math.round(gg1*1000)/1000 + "+" + Math.round(ff1*1000)/1000 + "x";

}}}document.getElementById("r1").value = x;document.getElementById("r2").value = ff1;document.getElementById("r3").value = gg1;document.getElementById("r4").value = hh;}
}
function rst()
{
var text=x;for(var rr=1;rr<text;rr++){if(x>1){var len = x.length;var s = document.getElementById("a").getElementsByTagName("TBODY")[0];s.removeChild(s.lastChild);x=x-1;}}
}
</script>
<style>
h2 {border-bottom: 1px solid #ebebeb;color: #474747;font-size: 1.6em;font-weight: normal;line-height: 130%;
}
.frms
{
margin:0 auto;
padding:10px;
-moz-border-radius:.3em;
-webkit-border-radius:.3em;
-o-border-radius:.3em;
font-family:Tahoma, Geneva, sans-serif;
color:#333;
font-size:.9em;
line-height:1.2em;
}
.frms input[type="text"],[type="password"],[type="file"],textarea,select
{
width:99%;
background:#fff;
border:#ddd 1px solid;
border-radius:.35em;
-moz-border-radius:.35em;
-webkit-border-radius:.35em;
-o-border-radius:.35em;
padding:0 .5%;
margin-top:5px;
margin-bottom:15px;
height:35px;
}
.frms input:hover,textarea:hover,select:hover
{
box-shadow:#dae1e5 0px 0px 5px;
-moz-box-shadow:#dae1e5 0px 0px 5px;
-webkit-box-shadow:#dae1e5 0px 0px 5px;
-o-box-shadow:#dae1e5 0px 0px 5px;
}
.frms input:focus,textarea:focus,select:focus
{
-webkit-box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);
-moz-box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);
box-shadow: inset 7px 4px 7px -7px rgba(0,0,0,0.42);
border:#9d9983 1px solid;
}
.frms input[type="submit"],input[type="reset"],input[type="button"]
{
padding:7px 14px;
font-weight:bold;
color:#fff;
cursor:pointer;
border-radius:.3em;
-moz-border-radius:.2em;
-webkit-border-radius:.2em;
-o-border-radius:.2em;
margin:10px 0;
border:none;
}
.frms input[type="submit"]
{
background:#75ab22;
border-bottom:#629826 3px solid;
text-shadow:#396e12 1px 1px 0px;
}
.frms input[type="reset"]
{
background:#ee765d;
border-bottom:#d95e44 3px solid;
text-shadow:#8c3736 1px 1px 0px;
}
input[type="button"],button,.blue_button
{
background:#468cd2;
border-bottom:#3277bc 3px solid;
text-shadow:#214d73 1px 1px 0px;
}
.frms label
{
font-size:1.2em;
}
.resp_code
{
margin:5px 10px 10px 300px;
padding:10px 20px 10px 20px;
font:normal 1em/1.3em Tahoma, Geneva, sans-serif;
color:#333;
background:#f8f8f8;
border:#ddd 1px solid;
border-radius:.25em;
overflow:auto;width:50%;
}
@media screen and (max-width: 480px)
{
.resp_code
{width:auto !important;margin:0px !important;
}
}
</style>
</head><body>
<div class='resp_code' id='maindiv'><div align='center'><h2>Calculate Simple / Linear Regression Line</h2></div>
<form name=first class='frms noborders'>
<table width=100%><caption><b>To Calculate Regression:</b></caption><tr><td></td></tr><tr><td><table id=a width=100%><tbody><tr><td>X Value</td> <td>Y Value </td> </tr><tr><td><input id=xvalu0 onkeyup=checnum(this) type=text size=10></td> <td> <input id=yvalu0 onkeyup=checnum(this) type=text size=10></td></tr>
<tr><td><input id=xvalu1 onkeyup=checnum(this) type=text size=10></td> <td> <input id=yvalu1 onkeyup=checnum(this) type=text size=10></td></tr>
</tbody><tr align='right'> <td> <a style=text-decoration:none; href=javascript:add() id='addd'>Add</a> </td> <td> <a style=text-decoration:none; href=javascript:del() id='dell'>Remove</a></td></tr></table></td></tr></table>
<div align=center><input type=button value=Calculate onclick=calculate()>
<input type='reset' value=Reset><span
align='center' style="font-size: 10px;color: #dadada;" id="dumdiv"><a href="https://www.hscripts.com" id="dum" style="font-size: 10px;color: #dadada;text-decoration:none;color: #dadada;">&copy;h</a></span></div><div class='result'><div align='left'><label>Total Numbers : </label> <input id=r1 type=text readonly></div><div align='left'><label>Slope(b) : </label> <input id=r2 type=text readonly> </div><div align='left'><label>y-Intercept(a) : </label> <input id=r3 type=text readonly> </div><div align='left'><label>Regression Equation(y): </label> <input id=r4 type=text readonly></div></div> </form>
</div> </body>
</html>


  • Release Date - 16-04-2015
  • Get free version without ©copyright link for just $10/-
  • For customization of this script or any script development, mail to support@hscripts.com

Usage

  • Copy and paste the above code into your HTML page.
  • Run the file in your browser.
  • Enter minimum of two values for both the variables to calculate the linear regression.
  • Regression equation is calculated by using the function 'calculate()',it will get the values you have entered in the x and y textboxes and gives the output.
  • If you wish to use the functionality of the script in your web page, copy and paste the code from the <script> tag into head part of HTML page.

License

Related Scripts

Free Javascript Scripts


Ask Questions

Ask Question