楼主: Lisrelchen
4795 13

[專題大討論]R or Python, Who Is Better for Statistical Analysis? [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2014-7-1 02:22:06 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

R or Python, Who Is Better for Statistical Analysis?


  1. class NLS:
  2.     ''' This provides a wrapper for scipy.optimize.leastsq to get the relevant output for nonlinear least squares. Although scipy provides curve_fit for that reason, curve_fit only returns parameter estimates and covariances. This wrapper returns numerous statistics and diagnostics'''

  3.     import numpy as np
  4.     import scipy.stats as spst
  5.     from scipy.optimize import leastsq

  6.     def __init__(self, func, p0, xdata, ydata):
  7.         # Check the data
  8.         if len(xdata) != len(ydata):
  9.             msg = 'The number of observations does not match the number of rows for the predictors'
  10.             raise ValueError(msg)

  11.         # Check parameter estimates
  12.         if type(p0) != dict:
  13.             msg = "Initial parameter estimates (p0) must be a dictionry of form p0={'a':1, 'b':2, etc}"
  14.             raise ValueError(msg)

  15.         self.func = func
  16.         self.inits = p0.values()
  17.         self.xdata = xdata
  18.         self.ydata = ydata
  19.         self.nobs = len( ydata )
  20.         self.nparm= len( self.inits )

  21.         self.parmNames = p0.keys()

  22.         for i in range( len(self.parmNames) ):
  23.             if len(self.parmNames[i]) > 5:
  24.                 self.parmNames[i] = self.parmNames[i][0:4]

  25.         # Run the model
  26.         self.mod1 = leastsq(self.func, self.inits, args = (self.xdata, self.ydata), full_output=1)

  27.         # Get the parameters
  28.         self.parmEsts = np.round( self.mod1[0], 4 )

  29.         # Get the Error variance and standard deviation
  30.         self.RSS = np.sum( self.mod1[2]['fvec']**2 )
  31.         self.df = self.nobs - self.nparm
  32.         self.MSE = self.RSS / self.df
  33.         self.RMSE = np.sqrt( self.MSE )

  34.         # Get the covariance matrix
  35.         self.cov = self.MSE * self.mod1[1]

  36.         # Get parameter standard errors
  37.         self.parmSE = np.diag( np.sqrt( self.cov ) )

  38.         # Calculate the t-values
  39.         self.tvals = self.parmEsts/self.parmSE

  40.         # Get p-values
  41.         self.pvals = (1 - spst.t.cdf( np.abs(self.tvals), self.df))*2

  42.         # Get biased variance (MLE) and calculate log-likehood
  43.         self.s2b = self.RSS / self.nobs
  44.         self.logLik = -self.nobs/2 * np.log(2*np.pi) - self.nobs/2 * np.log(self.s2b) - 1/(2*self.s2b) * self.RSS

  45.         del(self.mod1)
  46.         del(self.s2b)
  47.         del(self.inits)

  48.     # Get AIC. Add 1 to the df to account for estimation of standard error
  49.     def AIC(self, k=2):
  50.     return -2*self.logLik + k*(self.nparm + 1)

  51.     del(np)
  52.     del(leastsq)

  53.     # Print the summary
  54.     def summary(self):
  55.         print
  56.         print 'Non-linear least squares'
  57.         print 'Model: ' + self.func.func_name
  58.         print 'Parameters:'
  59.         print " Estimate Std. Error t-value P(>|t|)"
  60.         for i in range( len(self.parmNames) ):
  61.                 print "% -5s % 5.4f % 5.4f % 5.4f % 5.4f" % tuple( [self.parmNames[i], self.parmEsts[i], self.parmSE[i], self.tvals[i], self.pvals[i]] )
  62.         print
  63.         print 'Residual Standard Error: % 5.4f' % self.RMSE
  64.         print 'Df: %i' % self.df
复制代码
  1. import pandas as pd

  2. respData = pd.read_csv(my datafile here)
  3. respData['respDaily'] = respData['C_Resp_Mass'] * 24

  4. # Create the Arrhenius temperature

  5. respData['Ar'] = -1 / (8.617 * 10**-5 * (respData['Temp']+273))

  6. # First, define the likelihood null model
  7. def nullMod(params, mass, yObs):
  8.     a = params[0]
  9.     c = params[1]

  10.     yHat = a*mass**c
  11.     err = yObs - yHat
  12.     return(err)

  13. p0 = {'a':1, 'b':1}

  14. tMod = NLS(nullMod, p0, respData['UrchinMass'], respData['respDaily'] )

  15. tMod.summary()

  16. tMod.AIC()

  17. tMod.logLik
复制代码
  1. The above NLS code is the first that I know if in Python to do statistical NLS (not just curve fitting) and get the output I needed. But, it wasn’t easy, it took me about a week of my off (and on) hours. The following R code to do the same takes me maybe 15 minutes:

  2. # Import the data
  3. respiration <- read.csv( my file)
  4. # Standardize to 24 h
  5. respiration$C_Resp_Day <- respiration$C_Resp_Mass*24
  6. # Create the Arrhenius temperature
  7. respiration$Ar<--1/(8.617*10^-5*(respiration$Temp+273))

  8. Null.mod <- nls(C_Resp_Day~a*UrchinMass^c, data=respiration, start=list(a=exp(6),c=0.25))

  9. summary(Null.mod)
  10. AIC(Null.mod)
  11. logLik(Null.mod)
复制代码

R is just so much easier for real data analysis and has more functions. Python can do these things, but the modules are scattered (there’s at least three separate modules to fit curves that people have written to do different things) and don’t always give the needed output. The NLS example using Python above is, more or less, a replica of R’s NLS output and the missing pieces can be easily added.


R is currently head-and-shoulders above Python for data analysis, but we remain convinced that Python CAN catch up, easily and quickly. It is entirely possible to do statistical analysis in Python if you want to spend the time coding the analyses yourself. By and large, R is the way to go (for now). We would not be surprised that, if 10 years or so, Python’s data analyses libraries coalesce and evolve into something that can rival R.


For those of you interested in trying this out, the data can be downloaded here.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Statistical statistica statistic Analysis Analysi provides relevant returns import reason

沙发
songlinjl 发表于 2014-7-1 02:31:39 来自手机
Lisrelchen 发表于 2014-7-1 02:22
Nonlinear Regression using Python

R is just so much easier for real data analysis and has more fu ...
齐头并进
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

藤椅
大家开心 发表于 2014-7-1 02:47:23
研究研究
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

板凳
charlieq 发表于 2014-7-1 04:02:48
如果只是STAT的话那当然是R了,我不知道如果算像logistic regression这样的模型Python有什么有效地办法。如果是Data Mining,两个差不多吧。但是如果是Numerical Analysis 我觉得还是Python方便一些
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

报纸
complicated 在职认证  发表于 2014-7-1 09:32:20
charlieq 发表于 2014-7-1 04:02
如果只是STAT的话那当然是R了,我不知道如果算像logistic regression这样的模型Python有什么有效地办法。如 ...
那个,logistic regression不是data mining的分类算法之一么。。。。一直搞不懂
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

地板
zgy_Russell 发表于 2014-7-1 11:21:36
不太了解Python,但是觉得R软件不错。貌似也是现在用户增长量增加最快的。R功能较齐全,还能根据自己研究添加程序包,网上也能下载资源,感觉比较强大吧。

7
zgy_Russell 发表于 2014-7-1 11:21:41
不太了解Python,但是觉得R软件不错。貌似也是现在用户增长量增加最快的。R功能较齐全,还能根据自己研究添加程序包,网上也能下载资源,感觉比较强大吧。
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

8
charlieq 发表于 2014-7-1 22:21:14
complicated 发表于 2014-7-1 09:32
那个,logistic regression不是data mining的分类算法之一么。。。。一直搞不懂
logit regression  是regression的,属于统计方法,Data Mining又是另一回事,两者有思想交叉的地方,但是严格来讲是两个领域。Data Mining主要是大数据处理
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

9
complicated 在职认证  发表于 2014-7-2 09:09:54
charlieq 发表于 2014-7-1 22:21
logit regression  是regression的,属于统计方法,Data Mining又是另一回事,两者有思想交叉的地方,但是 ...
OK~
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

10
410234198 发表于 2014-7-2 13:07:46
数据一多,总感觉python好用些。
已有 1 人评分论坛币 收起 理由
Lisrelchen + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-3 20:37