Published
Edited
Sep 21, 2020
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
unbiasedN = Ford.length - 1

Insert cell
FordVa = FordSS/unbiasedN - Ford.length/(Ford.length - 1)*Math.pow(Fordavg,2)
Insert cell
Insert cell
Insert cell
r = covariance(Merck,Ford)/(Mercksd*Fordsd)
Insert cell
coeffofDetermination = Math.pow(r,2)
Insert cell
Insert cell
FordVar = variance(Ford);
Insert cell
Fordsd= Math.sqrt(FordVar)
Insert cell
MerckVar = variance(Merck)
Insert cell
Mercksd = Math.sqrt(MerckVar)
Insert cell
//Merck = y in this case, and Ford = x
//So the regression coefficient b = cov (x,y)/ var(x) or //cov(MRK,F)/var(Ford)
b = covariance(Merck,Ford)/FordVar
Insert cell
md` Now we calculate a, using b, and the two averages`
Insert cell
a = Merckavg - b * Fordavg
Insert cell
md`
Next we want to create an array of y-hat values that use x values as input and the equation of y-hat = b x(i) + a`
Insert cell
y_hat = data => data.map((d,i,A)=>(Math.round((a + b * d),3)));
//percentChange = data => data.map((d,i,A)=>(Math.round(100*(d-A[0])/A[0])))
//change1 = percentChange(Ford)
Insert cell
Ypredicted = y_hat(Ford)
Insert cell
function differencesq(arr1, arr2){
let arr3 = new Array(arr1.length).fill(0);
let n = arr1.length
for (let i = 0; i < n; i++) {
arr3 [i] = Math.pow((arr1[i] - arr2[i]),2);
}
return arr3
}
Insert cell
yMinusYhatsq = differencesq(Merck,Ypredicted)
Insert cell
yavgvector = new Array(Merck.length).fill(Merckavg);
Insert cell
yMinusYavgsq = differencesq(Merck, yavgvector)
Insert cell
asum(yMinusYavgsq)
Insert cell
asum(yMinusYhatsq)
Insert cell
rsq_regr= 1 - ((asum(yMinusYhatsq)/asum(yMinusYavgsq)))
Insert cell
r_regr = (Math.sqrt(rsq_regr))
Insert cell
Predicted = y_hat(Ford)
Insert cell
{

let trace1 = {
type: "scatter",
mode: "markers+text",
name: 'Merck',
x: Ford,
y: Merck,
line: {color: '#17BECF'},
text: date0.map(d=>d3.timeFormat("%b %Y")(d)),
textfont : {
family:'Times New Roman'
},
textposition: 'bottom center',
marker: { size: 8 }
}
let trace2 = {
type: "scatter",
mode: "line",
name: "Regression Line",
x: Ford,
y: Predicted,
line: {color: '#7F7F7F'},
}

var data = [trace1,trace2];

var layout = {
title: 'Merck vs. Ford Quarterly Stock Price <br>Coeff of Determination = 0.638 vs r-sq = 0.640<br> Regression parameters: a= 116.2575, b = -4.3997 Merck = a + b(Ford)' ,
xaxis: {
title: "Ford"
},
yaxis: {
title: "Merck"
}
};
const div = DOM.element('div');
Plotly.newPlot(div, data, layout);
return div;
}
Insert cell
//percentChange = data => data.map((d,i,A)=>(Math.round(100*(d.close-A[0].close)/A[0].close)))
percentChange = data => data.map((d,i,A)=>(Math.round(100*(d-A[0])/A[0])))

Insert cell
change = percentChange(Merck)
Insert cell
change1 = percentChange(Ford)
Insert cell
//nMonths = 60;
Insert cell
range = '3mo'
Insert cell
data0=fetchYahooData('MRK',range, '1/1/'+years[0], '12/31/'+(years[0]+4))
Insert cell
data1=fetchYahooData('F',range, '1/1/'+years[1], '12/31/'+(years[1]+4))
Insert cell
years = [2015, 2015, 2015, 2015, 2015, 2015]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Plotly = require("https://cdn.plot.ly/plotly-latest.min.js")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more