How to calculate correlation between two arrays?

datalyst

New Member
Joined
Oct 12, 2010
Messages
5
Hello,
I am wiritng a macro that will calculate correlation based on user input which determines how many items should be used from the two arrays to calculate the correlation.

e.g.
data starts from row 2 in excel sheet
Column A Column B
100 cells with data 100 cells with data


Array 1 Array 2
data from Column A data from column B


User input: e.g. 13 (thorugh an input box prompt)

correlation between first 13 items from Array A and Array B--> stored in cell 14( as data starts from row 2 in excel sheet)

then correlation between next 13 and stored in cell 27
.
.
.

and so on until last set of data items and stored in last row with data which is 100th row of data or 101st row in excel sheet.

any idea on how to achieve this?
Any help will be greatly appreciated.

Sincere Thanks.


user enters
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Sub Correlation()

    Dim i As Long, r As Long
    
    r = Abs(Application.InputBox("Enter the number of rows to correlate.", _
                                 Title:="Number of Rows?", Type:=1))
    If r = 0 Then Exit Sub  ' User canceled
    If r > 100 Then r = 100 ' Max number of rows = 100
    
    Range("C2:C101").ClearContents  ' Clear old correlations
    
    Range("C" & r + 1).FormulaR1C1 = "=CORREL(R[-" & r - 1 & "]C[-2]:RC[-2],R[-" & r - 1 & "]C[-1]:RC[-1])"
    If r < 51 Then Range("C2:C" & r + 1).AutoFill Destination:=Range("C2:C101")
    
'    For i = r + 1 To 101 Step r
'        Range("C" & i).FormulaR1C1 = "=CORREL(R[-" & r - 1 & "]C[-2]:RC[-2],R[-" & r - 1 & "]C[-1]:RC[-1])"
'    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,574
Messages
6,173,144
Members
452,501
Latest member
musallam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top