0% found this document useful (0 votes)
31 views6 pages

Reg Lineal

The document contains code for a linear regression analysis program with the following classes and functionality: - The main class contains the main method that initializes a RegresionLineal object and calls its methods to print the regression equation, fill prediction arrays, and create a graph. - The RegresionLineal class contains methods to calculate the mean and sums needed for the linear regression equation, compute the slope and intercept, fill prediction arrays, and create a line chart graph comparing predictions to data values. - The code performs a linear regression on sample y-value data, outputs the regression equation, generates predictions over a range of x-values, and plots the regression line against the original data points.

Uploaded by

Divan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views6 pages

Reg Lineal

The document contains code for a linear regression analysis program with the following classes and functionality: - The main class contains the main method that initializes a RegresionLineal object and calls its methods to print the regression equation, fill prediction arrays, and create a graph. - The RegresionLineal class contains methods to calculate the mean and sums needed for the linear regression equation, compute the slope and intercept, fill prediction arrays, and create a line chart graph comparing predictions to data values. - The code performs a linear regression on sample y-value data, outputs the regression equation, generates predictions over a range of x-values, and plots the regression line against the original data points.

Uploaded by

Divan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MAIN

public class main {

public static void main(String[] args) {

RegresionLineal rl = new RegresionLineal();

rl.imprimirEcuacion();

//System.out.println(m);

rl.llenarArregloPronostico();

rl.grafico();

CLASS RegLineal

public class RegLineal {

public double Y[] = {600, 1.550, 1.500, 1.500, 2.400, 3.100, 2.600, 2.900, 3.800, 4.500, 4.000,
4.900};

int n = Y.length;

public double X[] = new double[12];

public double mediaX() {

double mx;

double suma = 0;

for (int i = 0; i < n; i++) {

suma = suma + i;

mx = suma / n;

return mx;

public double mediaY() {


double my;

double suma = 0;

for (int i = 0; i < n; i++) {

suma = suma + i;

my = suma / n;

return my;

public double sumaXY() {

double xy;

double suma = 0;

for (int i = 0; i < n; i++) {

xy = (i + 1) * Y[i];

suma = suma + xy;

return suma;

public double sumaX2() {

double x2;

double suma = 0;

for (int i = 0; i < n; i++) {

x2 = (i + 1) * (i + 1);

suma = suma + x2;

return suma;

public double nmXmY() {

double mx = mediaX();
double my = mediaY();

double media;

media = n * mx * my;

return media;

public double nx2() {

double mx = mediaX();

double media;

media = n * mx * mx;

return media;

public double b1() {

double xy = sumaXY();

double x2 = sumaX2();

double mediaXY = nmXmY();

double mediaX2 = nx2();

double b1 = (xy - mediaXY) / (x2 - mediaX2);

return b1;

public double b0() {

double my = mediaY();

double b1 = b1();

double mx = mediaX();

double b0 = my - b1 * mx;
return b1;

public void imprimirEcuacion() {

double b1 = b1(), b0 = b0();

System.out.println("Ecuación de la recta: " + b1 + " X + " + b0);

public void llenarArregloPronostico() {

double b1 = b1(), b0 = b0();

System.out.println("Y Pronosticado");

for (int i = 0; i < X.length; i++) {

X[i] = b1 * (i + 1) + b0;

System.out.println(X[i]);

public void PronosticarNumeroEspecifico(int i) {

double b1 = b1(), b0 = b0();

System.out.println("Y Pronosticado cuando X vale: " + i);

X[i] = b1 * (i + 1) + b0;

//System.out.println(pronostico[i]);

public void grafico() {

llenarArregloPronostico();

DefaultCategoryDataset dataset = new DefaultCategoryDataset();


dataset.addValue(pronostico[0], "Pronostico", "1");

dataset.addValue(pronostico[1], "Pronostico", "2");

dataset.addValue(pronostico[2], "Pronostico", "3");

dataset.addValue(pronostico[3], "Pronostico", "4");

dataset.addValue(pronostico[4], "Pronostico", "5");

dataset.addValue(pronostico[5], "Pronostico", "6");

dataset.addValue(pronostico[6], "Pronostico", "7");

dataset.addValue(pronostico[7], "Pronostico", "8");

dataset.addValue(pronostico[8], "Pronostico", "9");

dataset.addValue(pronostico[9], "Pronostico", "10");

dataset.addValue(pronostico[10], "Pronostico", "11");

dataset.addValue(pronostico[11], "Pronostico", "12");

JFreeChart chart = ChartFactory.createLineChart(

"Grafica Lineal", // Titulo

"Periodo", // Etiqueta de datos

"Soles", // Etiqueta de valores

dataset, // Datos

PlotOrientation.VERTICAL, // orientacion

false, // Incluye leyenda

true, // Incluye tooltips

false // urls

);

ChartFrame frame = new ChartFrame("Graficador", chart);

frame.pack();

frame.setVisible(true);

CAPTURAS

You might also like