473,498 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Would anyone be able to help me with the correlation code for VBA?

3 New Member
I have to find the correlation by coding in the actual formula in the visual basic editor and not by calling it using the excel correlation function. I am getting the proper mean and standard deviation, however the correlation is wrong. Is there somethin wrong with my formula or the way I am coding things?

The following code is Module 3 of the code for correlation: Module 1 finds the mean, variance, and std deviation of X-values, and Module 2 does the same for Y-values. Module 3 was where I called on the functions of the other 2 models and got the wrong correlation. All help would be greatly appreciated.


Expand|Select|Wrap|Line Numbers
  1.  
  2. Module 3:
  3.  
  4. Option Explicit
  5.  
  6. Public Sub CalcStats()
  7.  
  8.     'Declarations
  9.     Dim vCorrel As Double
  10.     Dim vArrVal() As Double
  11.     Dim vCount1 As Long
  12.     Dim vCount2 As Long
  13.     Dim vCount3 As Long
  14.  
  15.     'Allocate memory
  16.     ReDim vArrVal(25)
  17.  
  18.     'Read the data
  19.     For vCount1 = 1 To 25
  20.         Let vArrVal(vCount1) = ActiveSheet.Range("B" & vCount1 + 1).Value
  21.     Next vCount1
  22.  
  23.     For vCount2 = 1 To 25
  24.         Let vArrVal(vCount2) = ActiveSheet.Range("C" & vCount2 + 1).Value
  25.     Next vCount2
  26.  
  27.     For vCount3 = 1 To 25
  28.         Let vArrVal(vCount3) = ActiveSheet.Range("D" & vCount3 + 1).Value
  29.     Next vCount3
  30.  
  31.     'Call function to calculate Correlation
  32.     Let vCorrel = CalcCorrel(vArrVal(), 25)
  33.  
  34.     'Message Box
  35.     MsgBox (vCorrel)
  36.  
  37.  
  38. End Sub
  39.  
  40.  
  41. Public Function CalcCorrel(pArrVal() As Double, _
  42.                            pNumOfObs As Long) As Double
  43.  
  44.     'Declarations
  45.     Dim vSum As Double
  46.     Dim vSumX As Double
  47.     Dim vSumY As Double
  48.     Dim vSum3 As Double
  49.     Dim vMeanX As Double
  50.     Dim vMeanY As Double
  51.     Dim vStdDevX As Double
  52.     Dim vStdDevY As Double
  53.     Dim vSumSq As Double
  54.     Dim vCorrel As Double
  55.     Dim vCount1 As Long
  56.     Dim vCount2 As Long
  57.     Dim vCount3 As Long
  58.  
  59.     'Initialize sum to zero
  60.     vSum = 0
  61.  
  62.     'Return
  63.     Let vMeanX = CalcMeanX(pArrVal(), pNumOfObs)
  64.     Let vMeanY = CalcMeanY(pArrVal(), pNumOfObs)
  65.  
  66.     'Return
  67.     Let vStdDevX = CalcStdDevX(pArrVal(), pNumOfObs)
  68.     Let vStdDevY = CalcStdDevY(pArrVal(), pNumOfObs)
  69.  
  70.     'Calculating sums
  71.     For vCount1 = 1 To pNumOfObs
  72.         Let vSumX = vSumX + pArrVal(vCount1)
  73.     Next vCount1
  74.  
  75.     For vCount2 = 1 To pNumOfObs
  76.         Let vSumY = vSumY + pArrVal(vCount2)
  77.     Next vCount2
  78.  
  79.     For vCount3 = 1 To pNumOfObs
  80.         Let vSum3 = vSum3 + pArrVal(vCount3)
  81.     Next vCount3
  82.  
  83.     'Calculating Correlation
  84.     Let CalcCorrel = ((vSum3) - ((vSumX * vSumY) / pNumOfObs)) / (Sqr((vSumX ^ 2 - ((vSumX) ^ 2 / pNumOfObs)) * (vSumY ^ 2 - ((vSumY) ^ 2 / pNumOfObs))))
  85.  
  86. End Function
  87.  
  88.  
  89.  

- Achal
Jul 12 '10 #1
0 1021

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2835
by: csx | last post by:
Hi all, I'm trying to count the number of leafnodes for a particular node. What im trying to do is make a function, that taking the tree structure: key row desc parent 1 1 A ...
0
1595
by: arachno | last post by:
Hello ppl, Is anyone can help me to figure out how to add data to TextML server document base? I have attached snipped, which taking XML source from file and adding it to documentbase, but my...
10
2481
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
5
2039
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
0
2984
by: Hugh Haggerty | last post by:
I need help with VB6 code to direct a WAV file to the computer speaker. Anyone know how to do this?
1
1344
by: Rob Meade | last post by:
Hi all, I found an article on how to write a Windows service here: http://www.dotnetbips.com/displayarticle.aspx?id=178 It was pretty much what I wanted, I want the service to scan a...
6
2265
by: gdarian216 | last post by:
I need help with my homework....i have to write a program that inputs multiple lines of integer numbers and outputs them in an HTML table. The numbers should be printed 3 in a row, one number per...
0
1116
by: NatMU1 | last post by:
I am coding a lexer that reads the input from a text file and displays the token ID's of each of the words in the file (e.g. if the file contains "<HEAD>" it displays the Symbol ID 10). Below is...
1
1248
Chrisjc
by: Chrisjc | last post by:
I am hoping someone can help me out here. I am trying to make one drop down box pull from a database the DB name is “dealerlocater” the column it needs to pull in this drop down is called “state”...
4
1801
by: Steve | last post by:
Can someone help me with this code - I'm trying to retrieve updated product information by pulling 3 fields and inserting values into my MYSQL db. In my code below I'm getting the page but I can't...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.