Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对大数据集(2亿个变量)运行逻辑回归的有效方法是什么?_Python_R_Matlab_Hadoop_Stata - Fatal编程技术网

Python 对大数据集(2亿个变量)运行逻辑回归的有效方法是什么?

Python 对大数据集(2亿个变量)运行逻辑回归的有效方法是什么?,python,r,matlab,hadoop,stata,Python,R,Matlab,Hadoop,Stata,我目前正在尝试运行逻辑回归模型。我的数据有两个变量,一个响应变量和一个预测变量。问题是我有2亿次观察。我正在尝试运行逻辑回归模型,但在R/Stata/MATLAB中,即使借助Amazon上的EC2实例,也很难做到这一点。我相信问题在于逻辑回归函数是如何在语言本身中定义的。是否有其他方法可以快速运行逻辑回归?目前的问题是,我的数据很快就会填满它所使用的任何空间。我甚至尝试使用高达30GB的RAM,但都没有用。任何解决方案都是非常受欢迎的。如果您的主要问题是在给定计算机内存限制的情况下估计logit

我目前正在尝试运行逻辑回归模型。我的数据有两个变量,一个响应变量和一个预测变量。问题是我有2亿次观察。我正在尝试运行逻辑回归模型,但在R/Stata/MATLAB中,即使借助Amazon上的EC2实例,也很难做到这一点。我相信问题在于逻辑回归函数是如何在语言本身中定义的。是否有其他方法可以快速运行逻辑回归?目前的问题是,我的数据很快就会填满它所使用的任何空间。我甚至尝试使用高达30GB的RAM,但都没有用。任何解决方案都是非常受欢迎的。

如果您的主要问题是在给定计算机内存限制的情况下估计logit模型的能力,而不是估计的快速性,那么您可以利用最大似然估计的可加性并为其编写自定义程序。logit模型只是使用logistic分布的最大似然估计。只有一个自变量这一事实简化了这个问题。我已经模拟了下面的问题。您应该使用以下代码块创建两个do文件

如果您在整个数据集中加载没有问题——您不应该这样做,我的模拟只使用了2亿OB和2个VAR的~2 Gig ram,尽管里程可能会有所不同——第一步是将数据集分解为可管理的部分。例如:

depvar=因变量(0或1s) indepvar=自变量(某些数字数据类型)

从这里开始,关闭Stata并重新打开它,以最小化内存使用。当您创建如此大的数据集时,即使您清除了数据集,Stata也会为开销等保留一些内存。您可以在
save full
之后键入
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时间。我用带有i5处理器的Stata SE(单核)在我的个人笔记本电脑上运行了这个程序,花了我一夜的时间。
    -沃尔德·奇尔的统计数字是错误的,但我相信你可以根据上面提到的正确数据进行计算
    -你不会像使用logit那样获得Psudo R2。

要测试系数是否与标准logit相同,请将obs设置为相对较小的100000,并将max_opp设置为1000。运行我的代码,查看输出,运行logit depvar indepvar,查看输出,它们与我在上面的“Cons”中提到的相同。将
obs
设置为与
max_opp
相同将纠正Wald Chi2统计数据。

仅使用一个预测值,您是否期望结果会有很大不同,这取决于您使用的是2亿行还是100万行?我的建议是使用100万行(甚至仅1e5行)的随机样本进行10(或100)回归,看看你的系数估计是如何变化的。我的猜测是,它们将几乎相同,添加更多的数据不会增加任何有趣的内容。您想要什么效果大小?你可能想做一些幂次计算,看看你需要多少样本来检验你的假设。你试过R的biglm软件包吗?
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