Xin chào mọi người,
Tôi muốn chạy phân tích hồi quy trong VBA. Tôi có một bộ dữ liệu mà tôi muốn khớp với bản phân phối LogNormal. Tôi đã gọi các tham số của phân phối Lognormal là "sigma" và "mju".
Để thực hiện điều này, tôi đang cố gắng tạo một hàm trong VBA, được gọi là "regreslognormalsigma". Hàm này yêu cầu các phạm vi sau làm đầu vào.
- probabilitetet --> phạm vi này chứa mật độ tích lũy của đối số;
- abshisat --> phạm vi này chứa các đối số, trong trường hợp của tôi, đây là các chuyển vị .
Tôi không thể hiểu tại sao đầu ra luôn là giá trị ban đầu mà tôi đặt cho "sigma", tôi. e. đầu ra luôn là 0. 0001.
Ai đó có thể giúp tôi không? . Trong thời gian chờ đợi, tôi nghĩ rằng thuật toán bên dưới cũng có thể được sử dụng để tìm giá trị trung bình [với một chút sửa đổi, điều mà tôi dự định thực hiện sau khi giải quyết vấn đề].
Mã bên dưới.
Hàm regreslognormalsigma[probabilitetet As Range, abshisat As Range] As Double
Dim nrqeliza As Double 'cái này tôi dùng để đếm phạm vi
Dim Yi As Double
Dim xi As Double
Dim Li As Double
Dim i As Double
Dim j As Double
Dim sigma, mju As Double
Dim sigmafinal, mjufinal As Double
Dim shgk, shgkmin, shgkvar As Double ' these are respectively: the sum of errors squared, the minimum sum of errors squared, and an auxiliary value
Kết quả mờ tăng gấp đôi
nrqeliza = probabilitetet. Đếm
sigma = 0. 0001 'giá trị ban đầu
mju = 0. 0001 'giá trị ban đầu
shgkmin = 1E+300 'Tôi đặt giá trị lớn cho giá trị nhỏ nhất
shgk = 0 'giá trị ban đầu
đây là cách tôi lấy các giá trị của phân phối Log Normal
'Li = WorksheetFunction.LogNorm_Dist[xi, sigma, mju, True] --> this is how I take values of the Log Normal distribution
While sigma < 10
While mju < 10 . LogNorm_Dist[abshisat[i], sigma, mju, True]
For i = 2 To nrqeliza
Li = WorksheetFunction.LogNorm_Dist[abshisat[i], sigma, mju, True]
shgk = [[[xác suất[i] - Li]] ^ 2] + shgk
Tiếp theo . shgkmin = shgkmin
If shgk < shgkmin Then
shgkvar = shgk
result = sigma
shgkmin = shgkvar
Else: shgkmin = shgkmin
Kết thúc nếu
mju = mju + 0. 0001
Wend
sigma = sigma + 0. 0001
Wend
regreslognormalsigma = result
Chức năng kết thúc
Đây là phần sửa đổi mà tôi đã thực hiện để phù hợp với tệp do shg cung cấp. Vẫn không hoạt động
Mã.
Function regreslognormalmju[probabilitetet As Range, abshisat As Range] As Double
Dim nrqeliza As Double
Dim Yi As Double
Dim Li As Double
Dim i As Double
Dim j As Double
Dim sigma As Double
Dim mju As Double
Dim shgk As Double
Dim shgkmin As Double
Dim result As Double
Dim nrresult As Double
Dim error As Double
nrqeliza = probabilitetet.Count
nrresult = 291 'kete rregulloje
sigma = 0.0001
mju = 0.0001
errormin = 1E+30
shgk = 0.01
result = 0.01
While sigma < 10
While mju < 10
For i = 1 To nrqeliza
Yi = [WorksheetFunction.Ln[abshisat[i, 1]] - mju] / sigma
Li = nrresult * WorksheetFunction.NormSDist[Yi]
shgk = shgk + [Li - probabilitetet[i, 1]] ^ 2
Next i
error = Sqr[shgk / nrqeliza]
If error < errormin Then
errormin = error
result = mju
End If
mju = mju + 0.01
Wend
sigma = sigma + 0.01
Wend
regreslognormalmju = result
End Function
Tôi đã thực hiện một số sửa đổi để làm cho mã dễ hiểu, tôi hy vọng bạn có thể xem rõ hơn bây giờ.
Tôi đã tạo một hàm tính toán sai số cho một giá trị trung bình và độ lệch chuẩn cho trước [tôi không trình bày hàm này ở đây]. Tôi đã thử nghiệm chức năng lỗi và nó hoạt động. Tuy nhiên, tôi vẫn không thể tìm thấy lỗi tối thiểu. bất cứ ai có thể xin vui lòng cho tôi biết những gì là sai? .
Mã.
Sub regresisub[]
Dim freq1 As Range 'this is an input for the function I created to calculate the error
Dim xet1 As Range 'this is an input for the function I created to calculate the error
Dim nrsample1 As Double 'this is an input for the function I created to calculate the error
Dim mean As Double
Dim stdev As Double
Dim errormin As Double
Set freq1 = Range["D41:D49"]
Set xet1 = Range["B41:B49"]
nrsample1 = 40 'I will change this
errormin = 1000000000
mean = 0.00001
stdev = 0.00001
Do
mean = mean + 0.001
Do
stdev = stdev + 0.001
If error1[freq1, xet1, nrsample1, mean, stdev] < errormin Then
errormin = error1[freq1, xet1, nrsample1, mean, stdev] 'here seems to be the problem
Range["F38"].Value = stdev
Else
End If
Loop While stdev < 5
Loop While mean < 10
End Sub