我目前正在尝试运行逻辑回归模型。我的数据有两个变量,一个响应变量和一个预测变量。问题是我有 2 亿个观察值。我正在尝试运行逻辑回归模型,但即使在亚马逊上的 EC2 实例的帮助下,我也很难在 R/Stata/MATLAB 中这样做。我认为问题在于逻辑回归函数是如何在语言本身中定义的。还有另一种快速运行逻辑回归的方法吗?目前我遇到的问题是我的数据很快就会填满它正在使用的任何空间。我什至尝试使用高达 30 GB 的 RAM,但无济于事。任何解决方案都将非常受欢迎。
最佳答案
如果您的主要问题是在给定计算机内存限制的情况下估计 Logit 模型的能力,而不是估计的速度,您可以利用最大似然估计的可加性并为 ml 编写自定义程序. Logit 模型只是使用逻辑分布的最大似然估计。只有一个自变量这一事实简化了这个问题。我已经模拟了下面的问题。您应该使用以下代码块创建两个 do 文件。
如果您在加载整个数据集时没有问题——您不应该这样,我的模拟只使用了约 2 gigs 的 ram,使用 2 亿个 obs 和 2 个变量,尽管里程可能会有所不同——第一步是分解将数据集分成可管理的部分。例如:
depvar = 你的因变量(0 或 1s) indepvar = 你的自变量(一些数字数据类型)
cd "/path/to/largelogit"
clear all
set more off
set obs 200000000
// We have two variables, and independent variable and a dependent variable.
gen indepvar = 10*runiform()
gen depvar = .
// As indpevar increases, the probability of depvar being 1 also increases.
replace depvar = 1 if indepvar > ( 5 + rnormal(0,2) )
replace depvar = 0 if depvar == .
save full, replace
clear all
// Need to split the dataset into managable pieces
local max_opp = 20000000 // maximum observations per piece
local obs_num = `max_opp'
local i = 1
while `obs_num' == `max_opp' {
clear
local h = `i' - 1
local obs_beg = (`h' * `max_opp') + 1
local obs_end = (`i' * `max_opp')
capture noisily use in `obs_beg'/`obs_end' using full
if _rc == 198 {
capture noisily use in `obs_beg'/l using full
}
if _rc == 198 {
continue,break
}
save piece_`i', replace
sum
local obs_num = `r(N)'
local i = `i' + 1
}
从这里开始,为了最大限度地减少内存使用,请关闭 Stata 并重新打开它。当您创建如此大的数据集时,即使您清除了数据集,Stata 也会为开销等分配一些内存。您可以在 save full
和 clear all
之后键入 memory
以了解我的意思。
接下来,您必须定义自己的自定义 ml 程序,该程序将在程序中一次输入其中的每一个片段,计算并计算每个片段的每个观察值的对数似然值,然后将它们加在一起。您需要使用 d0
ml 方法
而不是 lf
方法,因为使用 lf
的优化例程需要所有过去常常将数据加载到 Stata 中。
clear all
set more off
cd "/path/to/largelogit"
// This local stores the names of all the pieces
local p : dir "/path/to/largelogit" files "piece*.dta"
local i = 1
foreach j of local p { // Loop through all the names to count the pieces
global pieces = `i' // This is important for the program
local i = `i' + 1
}
// Generate our custom MLE logit progam. This is using the d0 ml method
program define llogit_d0
args todo b lnf
tempvar y xb llike tot_llike it_llike
quietly {
forvalues i=1/$pieces {
capture drop _merge
capture drop depvar indepvar
capture drop `y'
capture drop `xb'
capture drop `llike'
capture scalar drop `it_llike'
merge 1:1 _n using piece_`i'
generate int `y' = depvar
generate double `xb' = (indepvar * `b'[1,1]) + `b'[1,2] // The linear combination of the coefficients and independent variable and the constant
generate double `llike' = .
replace `llike' = ln(invlogit( `xb')) if `y'==1 // the log of the probability should the dependent variable be 1
replace `llike' = ln(1-invlogit(`xb')) if `y'==0 // the log of the probability should the dependent variable be 0
sum `llike'
scalar `it_llike' = `r(sum)' // The sum of the logged probabilities for this iteration
if `i' == 1 scalar `tot_llike' = `it_llike' // Total log likelihood for first iteration
else scalar `tot_llike' = `tot_llike' + `it_llike' // Total log likelihood is the sum of all the iterated log likelihoods `it_llike'
}
scalar `lnf' = `tot_llike' // The total log likelihood which must be returned to ml
}
end
//This should work
use piece_1, clear
ml model d0 llogit_d0 (beta : depvar = indepvar )
ml search
ml maximize
我刚刚运行了上面的两段代码并收到了以下输出:
这种方法的优点和缺点:
亲:
-
- ‘max_opp’越小,内存使用量越低。如上所述,我从未在模拟器中使用超过 ~1 gig。
-
- 您最终会得到无偏估计量、整个数据集估计量的完整对数似然、正确的标准误差 - 基本上所有对进行推理都很重要的东西。
缺点:
-
- 你在内存中保存的东西你必须牺牲 CPU 时间。我在装有 Stata SE(单核)和 i5 处理器的个人笔记本电脑上运行了这个程序,这花了我一整夜的时间。
-
- Wald Chi2 统计是错误的,但我相信你可以根据上面提到的正确数据计算出来
-
- 您不会像使用 logit 那样获得 Psudo R2。
要测试系数是否真的与标准 logit 相同,将 obs
设置为相对较小的值,如 100000,并将 max_opp
设置为 1000 之类的值。运行我的代码,查看输出,运行 logit depvar indepvar
,查看输出,除了我在上面的“缺点”中提到的以外,它们是相同的。将 obs
设置为与 max_opp
相同将更正 Wald Chi2 统计数据。
关于python - 对大型数据集(2 亿个变量)运行逻辑回归的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25046395/