2012-09-08 47 views
0

我所擁有的是在excel列A中給出的1000+數字數據。我必須取50(A1:A50)和另外50個數據(A51:A100),將它們粘貼到B和C列並從低到高排列。之後,我必須線性迴歸它們並找出Rsquare和迴歸公式。找到r平方和公式後,我必須將它們放在某個地方繼續進行下一步,這一步是相同的,但這次是A2:A51和A52:A101。作爲一個程序的迴歸

我必須在循環中做這件事,並存儲結果。

+1

什麼是您所遇到的麻煩呢?你的問題太廣泛了。 SO是一個社區,可以幫助您解決特定問題,而不是找到您的算法,除非您能證明它與SO社區相關。 – ApplePie

回答

2

這會讓你走。

Public Sub RegressionAnalysis() 
    Dim sample_count As Integer 
    sample_count = 50 

    Dim r As Range 
    Set r = Range("A2") ' First row of data 
    Dim i As Integer, N As Integer, j As Integer 
    N = Range(r, r.End(xlDown)).Rows.Count ' Count rows 

    Dim rx As Range, ry As Range 
    i = 0: j = 0 ' data counter, output counter 
    Dim slope As Double, yint As Double 
    Do While i + 2 * sample_count < N 
     ' Current block of data 
     Set rx = r.Offset(i, 0).Resize(sample_count, 1) 
     ' Next block of data 
     Set ry = rx.Offset(sample_count, 0).Resize(sample_count, 1) 
     ' Use built-in function for regression slope & intercept 
     slope = WorksheetFunction.slope(rx, ry) 
     yint = WorksheetFunction.Intercept(rx, ry) 
     ' Output values to adjacent columns 
     r.Offset(j, 1).Value = slope 
     r.Offset(j, 2).Value = yint 
     ' Increment the counters 
     i = i + sample_count 
     j = j + 1 
    Loop 
End Sub 

有了結果:

Excel Output

+0

感謝您的努力:)將需要一段時間來弄清楚邏輯和語言。給我一份新工作:) – Prophet