Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Gini coefficient

All

I want to calculate the Gini coefficient of a series with the following
macro. Excel reports a type mismatch for sum2 which I don't understand.


Dirk

Sub gini()

Dim ran As Range
Dim sum1, sum2, sum3, sum4 As Variant
Dim i, num As Integer


'ask for the range of the variable
Set ran = Application.InputBox("type in the range of the variable",
Type:=8)
num = ran.Rows.Count

'copy and paste into column to the right, sort
Selection.Copy
ran.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Sort Key1:=ran.Offset(0, 1), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

'cumulative income
'=SUM($B$2:B2)/SUM($B$2:$B$49)
sum1 = 0
For i = 0 To num - 1
sum1 = sum1 + ran.Cells(i, 1)
Next i
sum2 = 0
For i = 0 To num - 1
sum2 = sum2 + ran.Offset(i, 1)
ran.Offset(i, 2) = sum2 / sum1
Next i

'cumulative population
'=ROWS($C$2:C2)/ROWS($C$2:$C$49)
sum3 = 0
For i = 0 To num - 1
sum3 = sum3 + ran.Offset(i, 1)
ran.Offset(i, 3) = sum2 / num
Next i

'the product in the gini equation
'=(D3-D2)*(C3+C2)
sum4 = 0
For i = 1 To num
ran.Offset(i, 4) = (ran.Offset(i, 3) - ran.Offset(i - 1, 3)) *
(ran.Offset(i, 2) _
+ ran.Offset(i - 1, 2))
sum4 = sum4 + ran.Offset(i, 4)
Next i

'calculate coefficient
With ran.Offset(-1, 4)
..Value = "Gini"
..Font.Bold = True
End With
With ran.Offset(0, 4)
..Value = Abs(1 - sum4)
..Font.Color = 1
End With

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 137
Default Gini coefficient

Hi Dirk,

Take a look at
http://www.wessa.net/co.wasp

HTH
Carim

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Gini coefficient

Your problem with the line

sum2 = sum2 + ran.Offset(i, 1)
ran.Offset(i, 2) = sum2 / sum1

is that ran is a multi-cell range, so the offset is also a multi-cell range.

Instead, you should use (perhaps! not sure of your logic)

sum2 = sum2 + ran(1,1).Offset(i, 1).Value
ran(1,1).Offset(i, 2) = sum2 / sum1

Note that you will need to change your code blocks for sum3 and sum4 as well.

HTH,
Bernie
MS Excel MVP


"Dirk" wrote in message
ups.com...
All

I want to calculate the Gini coefficient of a series with the following
macro. Excel reports a type mismatch for sum2 which I don't understand.


Dirk

Sub gini()

Dim ran As Range
Dim sum1, sum2, sum3, sum4 As Variant
Dim i, num As Integer


'ask for the range of the variable
Set ran = Application.InputBox("type in the range of the variable",
Type:=8)
num = ran.Rows.Count

'copy and paste into column to the right, sort
Selection.Copy
ran.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Selection.Sort Key1:=ran.Offset(0, 1), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

'cumulative income
'=SUM($B$2:B2)/SUM($B$2:$B$49)
sum1 = 0
For i = 0 To num - 1
sum1 = sum1 + ran.Cells(i, 1)
Next i
sum2 = 0
For i = 0 To num - 1
sum2 = sum2 + ran.Offset(i, 1)
ran.Offset(i, 2) = sum2 / sum1
Next i

'cumulative population
'=ROWS($C$2:C2)/ROWS($C$2:$C$49)
sum3 = 0
For i = 0 To num - 1
sum3 = sum3 + ran.Offset(i, 1)
ran.Offset(i, 3) = sum2 / num
Next i

'the product in the gini equation
'=(D3-D2)*(C3+C2)
sum4 = 0
For i = 1 To num
ran.Offset(i, 4) = (ran.Offset(i, 3) - ran.Offset(i - 1, 3)) *
(ran.Offset(i, 2) _
+ ran.Offset(i - 1, 2))
sum4 = sum4 + ran.Offset(i, 4)
Next i

'calculate coefficient
With ran.Offset(-1, 4)
.Value = "Gini"
.Font.Bold = True
End With
With ran.Offset(0, 4)
.Value = Abs(1 - sum4)
.Font.Color = 1
End With

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Curve fit coefficient Kipi Excel Discussion (Misc queries) 0 April 12th 10 01:58 PM
coefficient of determination Jene Excel Worksheet Functions 2 August 27th 09 02:39 AM
gini index tomeck Excel Discussion (Misc queries) 1 May 6th 08 12:17 AM
Coefficient of Repeatability thewildleo Excel Worksheet Functions 0 July 28th 06 09:21 AM
Creating a Custom Excel Function to Calculate Gini Coefficients [email protected] Excel Worksheet Functions 3 February 21st 06 10:15 PM


All times are GMT +1. The time now is 02:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"