Calculating the slope of a series of values

前端 未结 4 1754
名媛妹妹
名媛妹妹 2021-02-15 11:32

I have 2 arrays of equal length. The following function attempts to calculate the slope using these arrays. It returns the average of the slope between each points. For the foll

4条回答
  •  花落未央
    2021-02-15 12:14

    I bet the other two methods are computing the least-squares fit, whereas you are not.

    When I verify this conjecture using R, I too get the slope of about 0.755:

    > summary(lm(y~x))
    
    Call:
    lm(formula = y ~ x)
    
    Coefficients:
                  Estimate Std. Error t value Pr(>|t|)    
    (Intercept) -1.265e+03  1.793e+02  -7.053 5.97e-05 ***
    x            7.551e-01  9.155e-02   8.247 1.73e-05 ***
    

    The relevant number is the 7.551e-01. It is also worth noting that the line has an intercept of about -1265.

    Here is a picture of the least-squares fit:

    lm fit

    As to implementing this in your code, see Compute least squares using java

提交回复
热议问题