簡體   English   中英

在 y 軸上以對數正態尺度應用線性回歸,在 x 軸上應用概率尺度

[英]Apply linear regression in a Log-Normal scale in the y axis and a Prob scale in the x axis

我正在嘗試計算線性回歸系數,但我不斷收到與tuple相關的錯誤。

我想用 Python plot 對數正態線性回歸分布,並用以下數據計算截距b0和斜率b1 ,然后計算x=50x=84.1y值。

x axis應為概率比例, y axis應為對數正常比例。 我不確定我寫的方法對於在對數正態概率尺度上實現線性回歸和計算系數是否正確。

我正在使用的代碼是:

from matplotlib import pyplot as plt
import seaborn
import probscale
from pylab import *
import numpy as np

# Permeability values (mD)
y = [283, 650, 565, 407, 714, 500, 730, 900, 420, 591, 381, 430, 324, 440, 1212, 315, 450]

# Permeability values in descending order (y, mD)
y.sort(reverse = True)
print('Permeability values in Descending Order :', y)

# Percentage of samples with larger permeability (x, %)
x = tuple([round(n/len(y)*100, 1) for n in range(len(y))])
print('Percentage of samples with larger permeability :', x)

# Plot
fig, ax = plt.subplots(figsize=(10, 8))
ax.set_xlim(0.01, 99)
ax.set_xscale('prob')
ax.set_ylim(1e0, 1e4)
ax.set_yscale('log')
seaborn.despine(fig=fig)
plt.plot(x, y, 'go')
plt.title('Permeability Variation')
plt.ylabel('Permebility, md')
plt.xlabel('Percent of Samples with Larger Permeability, %')
plt.grid(True)
plt.show()

# Mean for x and y
mean_x = np.mean(x)
mean_y = np.mean(y)

# Total number of values
m = len(x)

# Calculate b1 and b0
numer = 0
denom = 0
for i in range(m):
    numer += (x[i] - mean_x) * (y[i] - mean_y)
    denom += (x[i] - mean_x) ** 2
b1 = numer / denom
b0 = mean_y - (b1 * mean_x)

# Print coefficients
print('b1 = ', b1, 'b0 = ', b0)

# Calculate permeability at 84.1% and 50% probability (Percentiles)
# Calculate variance for permeability distribution (VDP)
k1 = b0 + b1 * 50
k2 = b0 + b1 * 84.1

# Dykstra Parsons Formula 'VDP' (k1=@50% Percentile and k2=@84.1% Percentile)
vdp = (k1 - k2) / k1
print('vdp = ', vdp)

# Calculate r^2 score (Coefficient of Correlation)
sumofsquares = 0
sumofresiduals = 0
for i in range(m):
    y_pred = b0 + b1 * x[i]
    sumofsquares += (y[i] - mean_y) ** 2
    sumofresiduals += (y[i] - y_pred) ** 2
score = 1 - (sumofresiduals / sumofsquares)
print('R^2 score = ', score)[![enter image description here][1]][1]

理想情況下,它看起來像這樣,具有最佳擬合的線性回歸直線。 (這只是一個例子)[1]: https://i.stack.imgur.com/nZG7W.png

您正在有效地計算方差的協方差,從而為您提供回歸系數。 您可以使用列表推導或 numpy 計算所有值,但是是的,它是正確的。

我不確定,只有你能回答的一件事是,是 x 和 y 之間的線性關系,還是 x 和 log(y) 之間的線性關系?

下面我使用scipy.stats.linregress()來回歸它和 plot,你可以看到 b1 和 b0 是相同的,希望這能回答你的問題:

from matplotlib import pyplot as plt
import seaborn
import probscale
import numpy as np
from scipy import stats

y = [283, 650, 565, 407, 714, 500, 730, 900, 420, 591, 381, 430, 324, 440, 1212, 315, 450]
y.sort(reverse = True)

x = tuple([round(n/len(y)*100, 1) for n in range(len(y))])

slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)

fig, ax = plt.subplots(figsize=(10, 8))
ax.set_xlim(0.01, 99)
ax.set_xscale('prob')
ax.set_ylim(1e0, 1e4)
ax.set_yscale('log')
seaborn.despine(fig=fig)
plt.plot(x, y, 'go')
plt.plot(x,list(map(lambda i:intercept+slope*i,x)), '--k')

在此處輸入圖像描述

print(slope,intercept)
-7.2679081487585195 889.7839128827538

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM