问题
需要用 js 来实现 线性度 CORREL.
解决
一组是固定值的线性度计算
比如: {0,10,30,60,80} 是一组固定的数据集。
<template>
<div>
<h1>Linearity (Correlation Coefficient) Calculator</h1>
<textarea v-model="inputData" placeholder="Enter data for set B (comma-separated, e.g., 1,2,3,4,5)"></textarea>
<button @click="calculateLinearity">Calculate Linearity</button>
<p>Correlation Coefficient: {{ correlationCoefficient.toFixed(4) }}</p>
</div>
</template>
<script>
export default {
data() {
return {
fixedData: [0, 10, 30, 60, 80], // 固定数据集
inputData: '', // 用户输入的数据集
correlationCoefficient: null, // 相关系数
};
},
methods: {
parseArray(dataString) {
// 将逗号分隔的字符串转换为数字数组
return dataString.split(',').map(Number).filter(num => !isNaN(num));
},
calculateMean(array) {
// 计算数组的平均值
return array.reduce((acc, val) => acc + val, 0) / array.length;
},
calculateCovariance(array1, array2, mean1, mean2) {
// 计算两个数组的协方差
const n = array1.length;
let covariance = 0;
for (let i = 0; i < n; i++) {
covariance += (array1[i] - mean1) * (array2[i] - mean2);
}
return covariance / (n - 1); // 使用 n-1 作为分母(样本协方差)
},
calculateStandardDeviation(array, mean) {
// 计算数组的标准差
const n = array.length;
let variance = 0;
for (let i = 0; i < n; i++) {
variance += Math.pow(array[i] - mean, 2);
}
return Math.sqrt(variance / (n - 1)); // 使用 n-1 作为分母(样本标准差)
},
calculateCorrelationCoefficient(array1, array2) {
// 计算两个数组的相关系数
const mean1 = this.calculateMean(array1);
const mean2 = this.calculateMean(array2);
const stdDev1 = this.calculateStandardDeviation(array1, mean1);
const stdDev2 = this.calculateStandardDeviation(array2, mean2);
const covariance = this.calculateCovariance(array1, array2, mean1, mean2);
return covariance / (stdDev1 * stdDev2);
},
calculateLinearity() {
// 解析用户输入的数据字符串为数组
const parsedInput = this.parseArray(this.inputData);
// 检查数组长度是否相等
if (parsedInput.length !== this.fixedData.length) {
alert('The number of elements in both data sets must be equal.');
return;
}
// 计算相关系数
this.correlationCoefficient = this.calculateCorrelationCoefficient(this.fixedData, parsedInput);
},
},
};
</script>
<style scoped>
textarea {
width: 100%;
height: 50px;
margin-bottom: 10px;
}
button {
margin-top: 10px;
}
</style>
两个变量集
<template>
<div>
<h1>Correlation Coefficient Calculator</h1>
<textarea v-model="dataA" placeholder="Enter data for set A (comma-separated)"></textarea>
<textarea v-model="dataB" placeholder="Enter data for set B (comma-separated)"></textarea>
<button @click="calculateCorrelation">Calculate Correlation</button>
<p>Correlation Coefficient: {{ correlationCoefficient }}</p>
</div>
</template>
<script>
export default {
data() {
return {
dataA: '',
dataB: '',
correlationCoefficient: null,
};
},
methods: {
parseArray(dataString) {
// 将逗号分隔的字符串转换为数字数组
return dataString.split(',').map(Number).filter(num => !isNaN(num));
},
calculateMean(array) {
// 计算数组的平均值
const sum = array.reduce((acc, val) => acc + val, 0);
return sum / array.length;
},
calculateStandardDeviation(array, mean) {
// 计算数组的标准差
const variance = array.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / array.length;
return Math.sqrt(variance);
},
calculateCorrelation(arrayA, arrayB, meanA, meanB, stdDevA, stdDevB) {
// 计算相关系数
const covariance = arrayA.reduce((acc, valA, index) => acc + (valA - meanA) * (arrayB[index] - meanB), 0);
return covariance / (stdDevA * stdDevB);
},
calculateCorrelationCoefficient() {
// 解析数据字符串为数组
const parsedA = this.parseArray(this.dataA);
const parsedB = this.parseArray(this.dataB);
// 检查数组长度是否相等
if (parsedA.length !== parsedB.length) {
alert('Data sets must have the same number of elements.');
return;
}
// 计算平均值和标准差
const meanA = this.calculateMean(parsedA);
const meanB = this.calculateMean(parsedB);
const stdDevA = this.calculateStandardDeviation(parsedA, meanA);
const stdDevB = this.calculateStandardDeviation(parsedB, meanB);
// 计算相关系数
this.correlationCoefficient = this.calculateCorrelation(parsedA, parsedB, meanA, meanB, stdDevA, stdDevB);
},
},
};
</script>
<style scoped>
textarea {
width: 100%;
height: 100px;
margin-bottom: 10px;
}
button {
margin-top: 10px;
}
</style>