Java 有没有办法在ImageJ中绘制线性回归?

Java 有没有办法在ImageJ中绘制线性回归?,java,plot,linear-regression,imagej,Java,Plot,Linear Regression,Imagej,我有一个编码问题,我想绘制值(x和y)并计算线性回归,然后在imageJ中绘制它。有人能帮忙吗 我试过这个 public class Plot implements PlugIn { public void run(String arg) { if (IJ.versionLessThan("1.27t")) return; float[] x = {0.375f, 0.75f, 1.5f, 2.25f, 3f,3.75f,4.5f

我有一个编码问题,我想绘制值(x和y)并计算线性回归,然后在imageJ中绘制它。有人能帮忙吗

我试过这个

public class Plot implements PlugIn {
    public void run(String arg) {
        if (IJ.versionLessThan("1.27t"))
            return;

        float[] x = {0.375f, 0.75f, 1.5f, 2.25f, 3f,3.75f,4.5f,4.75f,5f};
        // x-coordinates
        float[] y = {123f,456f,5678f,4567f,4567f,5678f,2345f,4563f,2345f};
        // y-coordinates
        float[] e = {.8f,.6f,.5f,.4f,.3f,.5f,.6f,.7f,.8f};
        // error bars

        PlotWindow plot = new PlotWindow("Example Plot","x-axis","y-axis",x,y);
        plot.setLimits(0, 5.5, 0, 6000);
        plot.addErrorBars(e);

        // add a second curve
        float x2[] = {.4f,.5f,.6f,.7f,.8f};
        float y2[] = {4,3,3,4,5};

        int N=9;
        double sumx = 0.0, sumy = 0.0, sumx2 = 0.0;
        for (int i = 0; i < N; i++)
            sumx  += x[i];
        for (int i = 0; i < N; i++)
            sumx2 += x[i]*x[i];
        for (int i = 0; i < N; i++)
            sumy  += y[i];
        double xbar = sumx / N;
        double ybar = sumy / N;

        // second pass: compute summary statistics
        double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
        for (int i = 0; i < N; i++) {
            xxbar += (x[i] - xbar) * (x[i] - xbar);
            yybar += (y[i] - ybar) * (y[i] - ybar);
            xybar += (x[i] - xbar) * (y[i] - ybar);
        }
        slope  = xybar / xxbar;
        intercept = ybar - slope * xbar;

        // more statistical analysis
        double rss = 0.0;      // residual sum of squares
        double ssr = 0.0;      // regression sum of squares
        for (int i = 0; i < N; i++) {
            double fit = slope*x[i] + intercept; //calculating fit
            rss += (fit - y[i]) * (fit - y[i]);
            ssr += (fit - ybar) * (fit - ybar); //calculating ssr
        }
    }
}
public类Plot实现插件{
公共无效运行(字符串参数){
如果(IJ.versionlesson(“1.27t”))
返回;
float[]x={0.375f,0.75f,1.5f,2.25f,3f,3.75f,4.5f,4.75f,5f};
//x坐标
float[]y={123f,456f,5678f,4567f,4567f,5678f,2345f,4563f,2345f};
//y坐标
float[]e={.8f、.6f、.5f、.4f、.3f、.5f、.6f、.7f、.8f};
//误差条
PlotWindow plot=新PlotWindow(“示例图”、“x轴”、“y轴”、x、y轴);
绘图设定限值(0,5.5,0,6000);
图.附录B(e);
//添加第二条曲线
浮点x2[]={.4f、.5f、.6f、.7f、.8f};
浮点y2[]={4,3,3,4,5};
int N=9;
双sumx=0.0,sumy=0.0,sumx2=0.0;
对于(int i=0;i
以下(与Java非常类似,可以直接从ImageJ中的运行)说明了如何使用和类生成带有回归线的绘图:

import ij.gui.Plot
import ij.measure.CurveFitter

x = [0.375d, 0.75d, 1.5d, 2.25d, 3d,3.75d,4.5d,4.75d,5d];
// x-coordinates
y = [123d,456d,5678d,4567d,4567d,5678d,2345d,4563d,2345d];
// y-coordinates

plot = new Plot("Example Plot","x-axis","y-axis");
plot.addPoints(x,y,Plot.LINE)
plot.setLimits(0, 5.5, 0, 6000);

cf = new CurveFitter((double[]) x, (double[]) y)
cf.doFit(CurveFitter.STRAIGHT_LINE)

yfit = []
for (cx in x) {
    yfit.add(cf.f(cx))
}

plot.addPoints(x, yfit, Plot.LINE)
plotwindow = plot.show()
一些建议:

  • 不要调用自己的类
    Plot
    ,因为代码的读者可能会将其与
    ij.gui.Plot
    混淆
  • 使用
    ij.gui.Plot
    而不是
    ij.gui.PlotWindow
    ;前者提供更多功能
  • 当有可用的工具时,不要进行手动回归
  • 对于特定于的问题,最好的提问地点是

什么不起作用?这段代码怎么没有达到您期望的效果?