Python တွင် likelihood ratio test ကို မည်သို့လုပ်ဆောင်ရမည်နည်း။
ဖြစ်နိုင်ခြေအချိုးစမ်းသပ်မှုသည် nested regression မော်ဒယ် နှစ်ခု၏ အံဝင်ခွင်ကျရှိမှုကို နှိုင်းယှဉ်သည်။
nested model သည် ယေဘုယျအားဖြင့် regression model တွင် ကြိုတင်ခန့်မှန်းနိုင်သော variable အစုအဝေးတစ်ခုပါရှိသော မော်ဒယ်တစ်ခုဖြစ်သည်။
ဥပမာအားဖြင့်၊ ကျွန်ုပ်တို့တွင် ကြိုတင်ခန့်မှန်းကိန်းရှင် လေးခုပါရှိသော အောက်ဖော်ပြပါ ဆုတ်ယုတ်မှုပုံစံ ရှိသည်ဆိုပါစို့။
Y = β 0 + β 1 x 1 + β 2 x 2 + β 3 x 3 + β 4 x 4 + ε
nested model ၏နမူနာတစ်ခုသည် မူရင်းကြိုတင်ခန့်မှန်းကိန်းရှင်နှစ်ခုသာပါရှိသော အောက်ပါမော်ဒယ်ဖြစ်လိမ့်မည်-
Y = β 0 + β 1 x 1 + β 2 x 2 + ε
ဤမော်ဒယ်နှစ်ခုသည် သိသိသာသာကွဲပြားခြင်းရှိမရှိ ဆုံးဖြတ်ရန်၊ အောက်ပါ null နှင့် အခြားအခြားသော ယူဆချက်များကို အသုံးပြုသည့် ဖြစ်နိုင်ခြေအချိုးစမ်းသပ်မှုတစ်ခုကို လုပ်ဆောင်နိုင်သည်-
H 0 : မော်ဒယ်အပြည့်အစုံနှင့် nested မော်ဒယ်သည် ဒေတာကို အညီအမျှ ကောင်းစွာ လိုက်ဖက်ပါသည်။ ဒါကြောင့် nested model ကိုသုံး သင့်ပါတယ်။
H A : မော်ဒယ်အပြည့်အစုံသည် nested မော်ဒယ်ထက် ဒေတာကို သိသိသာသာ ကိုက်ညီပါသည်။ ဒါကြောင့် template အပြည့်အစုံကို သုံး ရပါမယ်။
အကယ်၍ စမ်းသပ်မှု၏ p-တန်ဖိုးသည် အချို့သော အရေးပါမှုအဆင့် (ဥပမာ 0.05) အောက်တွင် ရှိနေပါက၊ ကျွန်ုပ်တို့သည် null hypothesis ကို ငြင်းပယ်နိုင်ပြီး မော်ဒယ်အပြည့်အစုံသည် သိသိသာသာ ပိုမိုကောင်းမွန်သော ကိုက်ညီမှုရှိကြောင်း ကောက်ချက်ချနိုင်ပါသည်။
အောက်ဖော်ပြပါ အဆင့်ဆင့် ဥပမာသည် Python တွင် ဖြစ်နိုင်ခြေအချိုးစမ်းသပ်မှုကို မည်သို့လုပ်ဆောင်ရမည်ကို ပြသထားသည်။
အဆင့် 1: ဒေတာကို တင်ပါ။
ဤဥပမာတွင်၊ ကျွန်ုပ်တို့သည် mtcars dataset မှဒေတာကိုအသုံးပြု၍ Python ရှိ အောက်ပါဆုတ်ယုတ်မှုပုံစံနှစ်ခုကို အံဝင်ခွင်ကျဖြစ်အောင် မည်သို့ပြုလုပ်ရမည်ကို ပြသပါမည်။
မော်ဒယ်အပြည့်အစုံ- mpg = β 0 + β 1 ရနိုင်သည် + β 2 carb + β 3 hp + β 4 cyl
မော်ဒယ်- စိုင်းစိုင်းခမ်းလှိုင် = β 0 + β 1 ရရှိနိုင်သော + β 2 ကာဗိုဟိုက်ဒရိတ်
ဦးစွာ၊ ကျွန်ုပ်တို့သည် ဒေတာအတွဲကို တင်ပါမည်-
from sklearn. linear_model import LinearRegression import statsmodels. api as sm import pandas as pd import scipy #define URL where dataset is located url = "https://raw.githubusercontent.com/Statorials/Python-Guides/main/mtcars.csv" #read in data data = pd. read_csv (url)
ဆက်စပ်- Pandas ဖြင့် CSV ဖိုင်များကို ဘယ်လိုဖတ်မလဲ။
အဆင့် 2- ဆုတ်ယုတ်မှုပုံစံများကို အံကိုက်လုပ်ပါ။
ဦးစွာ၊ ကျွန်ုပ်တို့သည် မော်ဒယ်အပြည့်အစုံနှင့် ကိုက်ညီပြီး မော်ဒယ်၏ မှတ်တမ်းဖြစ်နိုင်ခြေကို တွက်ချက်ပါမည်-
#define response variable y1 = data['mpg'] #define predictor variables x1 = data[['disp', 'carb', 'hp', 'cyl']] #add constant to predictor variables x1 = sm. add_constant (x1) #fit regression model full_model = sm. OLS (y1,x1). fit () #calculate log-likelihood of model full_ll = full_model. llf print (full_ll) -77.55789711787898
ထို့နောက်၊ ကျွန်ုပ်တို့သည် လျှော့ချထားသော မော်ဒယ်နှင့် ကိုက်ညီပြီး မော်ဒယ်၏ မှတ်တမ်းဖြစ်နိုင်ခြေကို တွက်ချက်ပါမည်-
#define response variable y2 = data['mpg'] #define predictor variables x2 = data[['disp', 'carb']] #add constant to predictor variables x2 = sm. add_constant (x2) #fit regression model reduced_model = sm. OLS (y2, x2). fit () #calculate log-likelihood of model reduced_ll = reduced_model. llf print (reduced_ll) -78.60301334355185
အဆင့် 3- မှတ်တမ်းဖြစ်နိုင်ခြေစမ်းသပ်မှုကို လုပ်ဆောင်ပါ။
ထို့နောက်၊ ကျွန်ုပ်တို့သည် ယုံကြည်နိုင်မှုစမ်းသပ်မှုကို လုပ်ဆောင်ရန် အောက်ပါကုဒ်ကို အသုံးပြုပါမည်။
#calculate likelihood ratio Chi-Squared test statistic
LR_statistic = -2 * (reduced_ll-full_ll)
print (LR_statistic)
2.0902324513457415
#calculate p-value of test statistic using 2 degrees of freedom
p_val = scipy. stats . chi2 . sf (LR_statistic, 2)
print (p_val)
0.35165094613502257
ရလဒ်မှ၊ chi-square စမ်းသပ်မှုကိန်းဂဏန်းသည် 2.0902 ဖြစ်ပြီး ဆက်စပ် p-value သည် 0.3517 ဖြစ်ကြောင်း ကျွန်ုပ်တို့ တွေ့နိုင်ပါသည်။
ဤ p-value သည် 0.05 ထက်မနည်းသောကြောင့်၊ null hypothesis ကို ငြင်းပယ်မည်မဟုတ်ပါ။
ဆိုလိုသည်မှာ full model နှင့် nested model သည် data ကို အညီအမျှ ကောင်းစွာ လိုက်ဖက်ပါသည်။ ထို့ကြောင့် မော်ဒယ်အပြည့်အစုံရှိ ထပ်လောင်းကြိုတင်ခန့်မှန်းကိန်းရှင်များသည် အံဝင်ခွင်ကျရှိ သိသာထင်ရှားသောတိုးတက်မှုကို မပေးနိုင်သောကြောင့် ကျွန်ုပ်တို့သည် nested မော်ဒယ်ကို အသုံးပြုရပါမည်။
ထို့ကြောင့် ကျွန်ုပ်တို့၏ နောက်ဆုံးပုံစံသည်-
mpg = β 0 + β 1 ရရှိနိုင်သော + β 2 ကာဗိုဟိုက်ဒရိတ်
မှတ်ချက် – p-value ကို တွက်ချက်ရာတွင် လွတ်လပ်မှု 2 ဒီဂရီကို အသုံးပြုထားသောကြောင့် ၎င်းသည် မော်ဒယ်နှစ်ခုကြားတွင် အသုံးပြုသည့် စုစုပေါင်း ကြိုတင်ခန့်မှန်းကိန်းရှင်များ၏ ကွာခြားချက်ကို ကိုယ်စားပြုသောကြောင့် ဖြစ်သည်။
ထပ်လောင်းအရင်းအမြစ်များ
အောက်ဖော်ပြပါ သင်ခန်းစာများသည် Python တွင် ဆုတ်ယုတ်မှုပုံစံများကို အသုံးပြုခြင်းဆိုင်ရာ နောက်ထပ်အချက်အလက်များကို ပေးဆောင်သည်-
Python ရှိ Linear Regression အတွက် ပြီးပြည့်စုံသော လမ်းညွှန်
Python တွင် polynomial regression လုပ်နည်း
Python တွင် Logistic Regression ကို မည်သို့လုပ်ဆောင်မည်နည်း။