Graphing Linear Regression With JFreeChart

Hi everybody I'm making a school project where I need to graph linear regression and scatter diagram in the same graphic, I am using JFreeChart and is a little confusing, I have already made a basic graph but I want to know if someone can tell me how to draw the line for the regression, i have de the values of a, b [bx + a], and also how to make the thickness of the line more thin...Here is my code any ideas are welcome :D
        JFrame frm_grafica = new JFrame("Grafica - B-Rabbit");       
        frm_grafica.setVisible(true);
        frm_grafica.setSize(250, 250);
        frm_grafica.setLocation(this.getLocation().x+this.getWidth()+5, this.getLocation().y);               
        XYSeries serie1 = new XYSeries("Diagrama de Dispersion");
        XYSeries serie2 = new XYSeries("Regresion Lineal");
        for(int i = 0; i < limitRows; i++){
            serie1.add(matriz[0], matriz[i][1]);
for(double i = 0; i <= limitRows; i+=0.1){
serie2.add(i, b*i+a);
XYSeriesCollection datasetCollection = new XYSeriesCollection();
datasetCollection.addSeries(serie1);
XYSeriesCollection datasetCollection2 = new XYSeriesCollection();
datasetCollection.addSeries(serie2);
XYDataset dataset = datasetCollection;
XYDataset dataset2 = datasetCollection2;
JFreeChart chart = ChartFactory.createScatterPlot(
"Diagrama de Dispersion",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
false,
true,
false
XYPlot plot = (XYPlot)chart.getXYPlot();
plot.setDataset(1, dataset2);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesFilled(0, true);
NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
ChartPanel chartPanel = new ChartPanel(chart);
frm_grafica.add(chartPanel);
chartPanel.setVisible(true);

I think I have an idea as to what to do.  I still need to retain the integrity of the original data, therefore I will be creating new channels. 
In a script you could do a loop looking for a value that is within the range then copy that data set to the new channel.  Are there any builtin commands that would make this process quicker or more streamlined?  All I know to do is to look at each value with CHD and make the decision from there. 
Thanks,
Wayne

Similar Messages

  • Calculating linear regression with DAX in a Tabular model

    Hi,
    I'm interesting to know if anyone has faced the linear regression in a Tabular model by DAX formulas.
    Thanks

    It's been a while, but... recently Rob Collie did a blog post about this
    here.

  • Linear regression calculation

    Need to know whether an algorithm to calculate linear regressions exist, given a determined number of points.

    Dear Ingo,
    I am not sure about your answer. Well, I need an script as a solution. Imagine, the time data is collected in a channel, DatCha_1. There are 1000 points corresponding to 10 seconds. I have the corresponding aceleration data, in Dat_Cha_2, that is, 1000 acceleration points. So, I need to calculate the linear regression with this acceleration data each second, that is, I must obtain the a, b data of a y=a+bx  corresponding to each 100 values of the acceleration data, so 10 a values and 10 b values. I must divide the original acceleration data into 10 interval and obtain the linear regresion of each of this intervals.
    What should be the script for this action? You mention to adjust the channel length of the time and data channel, but I do not know what you mean. In fact the function is:
    Call CHNREGRYCALC("Time","Acceleration","Time","Regress_Y","linear") '... X,Y,XCHNNO,E,REGRTYPE
    Could I use a new time channel only with 10 values and the original aceleration with 1000 values?
    So, please could you send me a little example of the script I would need to solve the problem?
    Regards,
    Koniker

  • Linear regression

    Hi,
    I would like to know how I could do the following linear regression calculation.  I have two data channels, the first one corresponding to time information and the second one about aceleration. I must do linear regression with this acceleration data each second, but taking into account that the time channel has 10585 points for example for 44.1 seconds, corresponding to a 240msec sampling frecuency. I suppose that I must use the ChnRegrXYCalc function but I would like to know what the parameters should be.
    REgards
    Koniker.

    Dear Ingo,
    I am not sure about your answer. Well, I need an script as a solution. Imagine, the time data is collected in a channel, DatCha_1. There are 1000 points corresponding to 10 seconds. I have the corresponding aceleration data, in Dat_Cha_2, that is, 1000 acceleration points. So, I need to calculate the linear regression with this acceleration data each second, that is, I must obtain the a, b data of a y=a+bx  corresponding to each 100 values of the acceleration data, so 10 a values and 10 b values. I must divide the original acceleration data into 10 interval and obtain the linear regresion of each of this intervals.
    What should be the script for this action? You mention to adjust the channel length of the time and data channel, but I do not know what you mean. In fact the function is:
    Call CHNREGRYCALC("Time","Acceleration","Time","Regress_Y","linear") '... X,Y,XCHNNO,E,REGRTYPE
    Could I use a new time channel only with 10 values and the original aceleration with 1000 values?
    So, please could you send me a little example of the script I would need to solve the problem?
    Regards,
    Koniker

  • How can I make a graphic (scatter chart) with 1 "y", but multiple "x" (to make a multiple linear regression)?

    how can I make a graphic (scatter chart) with 1 "y", but multiple "x" (to make a multiple linear regression)?

    Émilie,
    The default for X-Y Charts is for X values not to be shared, so it's entirely possible for you to create X-Y pair sets that have common Y-values and independent X-values.
    This answer assumes, as always, that I properly interpreted your problem statement. A linear fit line for each pair set should satisfy.
    Jerry

  • 50G / simultaneous multiple scatter plots + intersection of linear regression lines

    I have a real specific problem with scatter plots and linear regression lines.
    First, I've been looking over the manual and the internet trying to figure out how to graph multiple scatter plots at the same time, but it seems like you can only do Columns 1 and 2 or Columns 1 and 3, etc., from Edat (greek letter), you can't do all three columns at once IF Column 1 represents X and 2 and 3 represent Y-values.  Or columns 1 & 2 as one set, and 3 & 4 as a second set. 
    Further, because it doesn't seem to be able to do this, I can't get it to find the intersection of these two lines. I can get the x-intercept and y-intercept just fine but I don't need that. What I can do is do a linear regression on both sets of data to get an equation for a line, put it into an array, and use the solver on it, that does indeed work. But that's a whole lot of extra keypresses just to get an ordered pair. I have experience on TI and Casio calculators and they don't have this seeming limitation. 
    Any suggestions?

    I have a real specific problem with scatter plots and linear regression lines.
    First, I've been looking over the manual and the internet trying to figure out how to graph multiple scatter plots at the same time, but it seems like you can only do Columns 1 and 2 or Columns 1 and 3, etc., from Edat (greek letter), you can't do all three columns at once IF Column 1 represents X and 2 and 3 represent Y-values.  Or columns 1 & 2 as one set, and 3 & 4 as a second set. 
    Further, because it doesn't seem to be able to do this, I can't get it to find the intersection of these two lines. I can get the x-intercept and y-intercept just fine but I don't need that. What I can do is do a linear regression on both sets of data to get an equation for a line, put it into an array, and use the solver on it, that does indeed work. But that's a whole lot of extra keypresses just to get an ordered pair. I have experience on TI and Casio calculators and they don't have this seeming limitation. 
    Any suggestions?

  • Date/Time on X-Axis with JFreeChart

    Hi guys,
    I've asked this question on the JFreeChart forums, but got no response.
    If anyone has any experience with JFreeChart, help would be most appreaciated.
    I am drawing a XY Line Chart to display CPU usage retrieved from a database. The user can search with specific dates/times (eg: from: 24/2/2005 to: 16/4/2005) - and then these results would be graphed.
    My question is, how do I use dates/times on the X-axis using JFreeChart?
    The results I am getting from the DB would be a time-stamp and CPU usage.
    Here is my current code, but it wont work with a time-stamp:
    public static class ChartTest   {
        public ChartTest()
            // create a dataset...FOR LOOP
            XYSeries dataSet = new XYSeries("CPU Usage");
            TimeSeries closing = new TimeSeries( "Closing Value", Day.class );
         for(int h=0; h < newArray[0].length; h++)
            for(int i=0; i < newArray.length; i++)
               dataSet.add(newArray[i][h], newArray[i][h++]);
            XYDataset xyDataset = new XYSeriesCollection(dataSet);
            // create a chart...
            JFreeChart lineGraph = ChartFactory.createXYLineChart
                        ("Mitch's CPU Usage Test",  // Title
                          "Time",           // X-Axis label
                          "CPU Load",           // Y-Axis label
                          xyDataset,          // Dataset
                          PlotOrientation.VERTICAL,        //Plot orientation
                          true,                //show legend
                          true,                // Show tooltips
                          false               //url show
            // create and display a frame...
            ChartFrame frame = new ChartFrame("CPU Usage Test", lineGraph);
            frame.pack();
            frame.setVisible(true);
        }

    The JFreeChard demo should give you the idea
    package demo;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.text.SimpleDateFormat;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
    import org.jfree.data.time.*;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.*;
    public class TimeSeriesDemo1 extends ApplicationFrame
        public TimeSeriesDemo1(String s)
            super(s);
            XYDataset xydataset = createDataset();
            JFreeChart jfreechart = createChart(xydataset);
            ChartPanel chartpanel = new ChartPanel(jfreechart, false);
            chartpanel.setPreferredSize(new Dimension(500, 270));
            chartpanel.setMouseZoomable(true, false);
            setContentPane(chartpanel);
        private static JFreeChart createChart(XYDataset xydataset)
            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", xydataset, true, true, false);
            jfreechart.setBackgroundPaint(Color.white);
            XYPlot xyplot = (XYPlot)jfreechart.getPlot();
            xyplot.setBackgroundPaint(Color.lightGray);
            xyplot.setDomainGridlinePaint(Color.white);
            xyplot.setRangeGridlinePaint(Color.white);
            xyplot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
            xyplot.setDomainCrosshairVisible(true);
            xyplot.setRangeCrosshairVisible(true);
            org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
            if(xyitemrenderer instanceof XYLineAndShapeRenderer)
                XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
                xylineandshaperenderer.setDefaultShapesVisible(true);
                xylineandshaperenderer.setDefaultShapesFilled(true);
            DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
            dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
            return jfreechart;
        private static XYDataset createDataset()
            TimeSeries timeseries = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Month.class);
            timeseries.add(new Month(2, 2001), 181.8);
            timeseries.add(new Month(3, 2001), 167.3);
            timeseries.add(new Month(4, 2001), 153.8);
            timeseries.add(new Month(5, 2001), 167.6);
            timeseries.add(new Month(6, 2001), 158.8);
            timeseries.add(new Month(7, 2001), 148.3);
            timeseries.add(new Month(8, 2001), 153.9);
            timeseries.add(new Month(9, 2001), 142.7);
            timeseries.add(new Month(10, 2001), 123.2);
            timeseries.add(new Month(11, 2001), 131.8);
            timeseries.add(new Month(12, 2001), 139.6);
            timeseries.add(new Month(1, 2002), 142.9);
            timeseries.add(new Month(2, 2002), 138.7);
            timeseries.add(new Month(3, 2002), 137.3);
            timeseries.add(new Month(4, 2002), 143.9);
            timeseries.add(new Month(5, 2002), 139.8);
            timeseries.add(new Month(6, 2002), 137.0);
            timeseries.add(new Month(7, 2002), 132.8);
            TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust", org.jfree.data.time.Month.class);
            timeseries1.add(new Month(2, 2001), 129.6);
            timeseries1.add(new Month(3, 2001), 123.2);
            timeseries1.add(new Month(4, 2001), 117.2);
            timeseries1.add(new Month(5, 2001), 124.1);
            timeseries1.add(new Month(6, 2001), 122.6);
            timeseries1.add(new Month(7, 2001), 119.2);
            timeseries1.add(new Month(8, 2001), 116.5);
            timeseries1.add(new Month(9, 2001), 112.7);
            timeseries1.add(new Month(10, 2001), 101.5);
            timeseries1.add(new Month(11, 2001), 106.1);
            timeseries1.add(new Month(12, 2001), 110.3);
            timeseries1.add(new Month(1, 2002), 111.7);
            timeseries1.add(new Month(2, 2002), 111.0);
            timeseries1.add(new Month(3, 2002), 109.6);
            timeseries1.add(new Month(4, 2002), 113.2D);
            timeseries1.add(new Month(5, 2002), 111.6);
            timeseries1.add(new Month(6, 2002), 108.8);
            timeseries1.add(new Month(7, 2002), 101.6);
            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
            timeseriescollection.addSeries(timeseries);
            timeseriescollection.addSeries(timeseries1);
            timeseriescollection.setDomainIsPointsInTime(true);
            return timeseriescollection;
        public static JPanel createDemoPanel()
            JFreeChart jfreechart = createChart(createDataset());
            return new ChartPanel(jfreechart);
        public static void main(String args[])
            TimeSeriesDemo1 timeseriesdemo1 = new TimeSeriesDemo1("Time Series Demo 1");
            timeseriesdemo1.pack();
            RefineryUtilities.centerFrameOnScreen(timeseriesdemo1);
            timeseriesdemo1.setVisible(true);
    }

  • Seasonal linear regression

    Hi there, APO Guru's
    We have created a forecast profile that uses strategy 35.
    However when we run the forecast with this model for say the last 36months of history. The system proposes an almost straight line forecast. If we run it with 18 months history then the system proposes an acceptable forecast. I can now clearly see the sales history pattern in my forecast.
    We do have enough data and seasonality in our history for the 36 months to work.
    Can anyone maybe shed some light on this.
    We also recently upgraded to SCM 5.0
    Thanks for your assistance
    Robert

    Before the system applies seasonal linear regression, it carries out a seasonal test. The system uses this test to check if the historical data contains any seasonal patterns. For this, the system determines the autocorrelation coefficient for all periods.
    If the value determined is at least 0.3, the system applies seasonal linear regression. If the value is less than 0.3, the system does not recognise a seasonal pattern and applies linear regression.
    So I think the test against your 36 months of data returns a value of less than 0.3 and hence it uses linear only, and the value for your 18 months seems to be greater than 0.3.
    (P.S. You can change this limit in the /SAPAPO/SCM_FCSTPARA BAdI – Method PARAMETER_SET)

  • How to create a linear regression line series

    I am relatively new to Flex and I am currently working with charts in the data visualization components. It has been requested that I add a linear trendline (ie, linear regression trend line) to a column chart. From what I can tell, there is no linear regression form for the mx:LineSeries. Has anyone created a linear trendline using the data visualization components in Flex, or does anyone have any suggestions on how to accomplish this?

    Nicholas @ Bime posted a solution to this sort of problem earlier this year, apparently. He created a custom component that extends the CartesianDataCanvas, and can be called as an annotation element of the chart. See the following url: http://bimehq.com/data-visualization/introducing-flex-charting-trend-line-component/
    His solution works very well, with the only caveat being if you have mutiple series on your chart, all the trendlines will be the same color, and there is no label on the lines to show which line corresponds to which series.

  • Linear Regression - How to view t-statistic and p-value

    How can we view the t-statistic and p-value for the predictors in a linear regression or multiple linear regression model?

    The R code sample for LM would be as follows: model <- lm(dependent_var ~ independent_var, data=data.set)
    In the Azure ML gallery there are a few samples which can help you get set up with the experiment. (Sample name: Exploring the Usage of R in AML)

  • Linear regression correlation coefficient

    Does anyone have a vi that computes the linear regression correlation coefficient (R^2)?

    gbaby wrote:
    I know this thread is ten years old but I'm still going to post the subVI i made, it will work even for those with the base development package. Hope this proves useful for somebody out there.
    Thanks! Actually, looks very similar to the one posted by greg over 10 years ago.
    You can eliminate the FOR loops (might perform better by taking better advantage of SSE). After the FOR loops are removed, there are a few common sub-expressions that you can merge for even better performance. No big deal, though.
    LabVIEW Champion . Do more with less code and in less time .

  • Crystal Reports Linear Regression Trendline Slope

    I need to know the slope of my linear regression trendline from my chart to determine whether the trend is positive or negative. I can't find any information on how to display and/or use that value in a formula.

    It appears that the steps I was taking was incorrect.  To actually get the trendline, you need to click the line in the line chart that you want the trendline to be against.  Follow the steps above BUT do not start it from the legend.

  • Using Multiple Linear Regression from SAP Predictive Analysis within S&OP

    How could I go about using a model exported from SAP Predictive Analysis, say a multiple linear regression model, as a stored procedure in HANA from within S&OP?

    Hi Kevin,
    You cannot use a model exported from SAP Predictive Analysis in S&OP.  S&OP uses the same PAL libraries from HANA for Statistical Forecasting. We enable a few statistical forecasting methods from PAL like exponential smoothing from S&OP.
    Thanks,
    Raghav

  • I am looking for a lab view schematic for hooking up a high-power motor driver to a linear actuator with feedback

    I need some help coding in labview 2012. I am hooking up a Pololu High-Power Motor Driver 18v25 CS to a Concentric LACT12P-12V-5 Linear Actuator with Feedback: 12" Stroke, 12V, 1.7"/s.  Thanks!

    Hi James,
    How are you planning to hook up your motor driver with PC? Do you have some DAQ device or hardware target to send TTL/PWM signals to driver?
    Regards.

  • DCN Case Study 3: ONS 15454 Linear Topology with DCN Connections at Both Ends Using OSPF

    Hi, I need advise regarding design of Network Connectivity - DCN Case Study 3: ONS 15454 Linear Topology with DCN Connections at Both Ends Using OSPF.
    Am I need area 100 and 200 on Router 1 and Router 2 or I can don't do that?
    Am 

Maybe you are looking for