python中調用r(Python中調用_________來計算數的絕對值)

  • 1、Python調用R編程——rpy2
  • 2、求助,R語言在python中調用問題
  • 3、如何通過PypeR來實現在Python中調用R

在Python調用R,最常見的方式是使用rpy2模塊。

The package is made of several sub-packages or modules:

Importing R packages is often the first step when running R code, and rpy2 is providing a function rpy2.robjects.packages.importr() that makes that step very similar to importing Python packages.

We mentioned earlier that rpy2 is running an embedded R. This is may be a little abstract, so there is an object rpy2.robjects.r to make it tangible.

The __getitem__() method of rpy2.robjects.r, gets the R object associated with a given symbol

The object r is also callable , and the string passed in a call is evaluated as R code.

An R object has a string representation that can be used directly into R code to be evaluated.

In R, data are mostly represented by vectors, even when looking like scalars. When looking closely at the R object pi used previously, we can observe that this is in fact a vector of length 1.

Creating R vectors can be achieved simply.

The easiest way to create such objects is to do it through R functions.

Calling R functions is disappointingly similar to calling Python functions.

By default, calling R functions return R objects.

Linear models

Creating an R vector or matrix, and filling its cells using Python code

This module should be the right pick for casual and general use. Its aim is to abstract some of the details and provide an intuitive interface to both Python and R programmers.

The instance can be seen as the entry point to an embedded R process. The elements that would be accessible from an equivalent R environment are accessible as attributes of the instance.

When safety matters most, we recommend using __getitem__() to get a given R object.

Storing the object in a python variable will protect it from garbage collection, even if deleted from the objects visible to an R user.

Just like it is the case with RPy-1.x, on-the-fly evaluation of R code contained in a string can be performed by calling the r instance.

The astute reader will quickly realize that R objects named by python variables can be plugged into code through their R representation.

R environments can be described to the Python user as an hybrid of a dictionary and a scope.

The first of all environments is called the Global Environment, that can also be referred to as the R workspace.

Assigning a value to a symbol in an environment has been made as simple as assigning a value to a key in a Python dictionary.

An environment is also iter-able, returning all the symbols (keys) it contains.

R functions exposed by rpy2’s high-level interface can be used:

This is all looking fine and simple until R arguments with names such as na.rm are encountered. By default, this is addressed by having a translation of ‘.’ (dot) in the R argument name into a ‘_’ in the Python argument name.

In Python one can write:

R is capable of introspection, and can return the arguments accepted by a function through the function formals().

The method Function.rcall() is an alternative way to call an underlying R function.

For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.

The class robjects.Formula is representing an R formula.

Other options are:

This is achieved by the R functions library() and require() (attaching the namespace of the package to the R search path).

Beside functions and environments, most of the objects an R user is interacting with are vector-like. For example, this means that any scalar is in fact a vector of length one.

The class Vector has a constructor:

Creating vectors can be achieved either from R or from Python.

When the vectors are created from R, one should not worry much as they will be exposed as they should by rpy2.robjects.

When one wants to create a vector from Python, either the class Vector or the convenience classes IntVector, FloatVector, BoolVector, StrVector can be used.

Extracting, Python-style

The python __getitem__() method behaves like a Python user would expect it for a vector (and indexing starts at zero).

Extracting, R-style

Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.

Assigning, Python-style

Since vectors are exposed as Python mutable sequences, the assignment works as for regular Python lists.

In R vectors can be named, that is elements of the vector have a name.

Assigning, R-style

The attributes rx and rx2 used previously can again be used:

For the sake of complete compatibility with R, arguments can be named (and passed as a dict or rpy2.rlike.container.TaggedList).

In S/Splus/R special NA values can be used in a data vector to indicate that fact, and rpy2.robjects makes aliases for those available as data objects NA_Logical, NA_Real, NA_Integer, NA_Character, NA_Complex .

To expose that to Python, a delegating attribute ro is provided for vector-like objects.

R vectors can have a name given to all or some of the elements. The property names can be used to get, or set, those names.

Array

In R, arrays are simply vectors with a dimension attribute. That fact was reflected in the class hierarchy with robjects.Array inheriting from robjects.Vector.

Matrix

A Matrix is a special case of Array. As with arrays, one must remember that this is just a vector with dimension attributes (number of rows, number of columns).

DataFrame

In rpy2.robjects, DataFrame represents the R class data.frame.

Creating a DataFrame can be done by:

The DataFrame constructor accepts either an rinterface.SexpVector (with typeof equal to VECSXP, that is, an R list) or any Python object implementing the method items() (for example dict or rpy2.rlike.container.OrdDict).

To create a DataFrame and be certain of the clumn order order, an ordered dictionary can be used:

Here again, Python’s __getitem__() will work as a Python programmer will expect it to:

The DataFrame is composed of columns, with each column being possibly of a different type:

The approach followed in rpy2 has 2 levels (rinterface and robjects), and conversion functions help moving between them.

R vectors are mapped to Python objects implementing the methods __getitem__() / __setitem__() in the sequence protocol so elements can be accessed easily.

R functions are mapped to Python objects implementing the __call__() so they can be called just as if they were functions.

R environments are mapped to Python objects implementing __getitem__() / __setitem__() in the mapping protocol so elements can be accessed similarly to in a Python dict.

In its high-level interface rpy2 is using a conversion system that has the task of convertion objects between the following 3 representations: – lower-level interface to R (rpy2.rinterface level), – higher-level interface to R (rpy2.robjects level) – other (no rpy2) representations

R vectors or arrays can be converted to numpy arrays using numpy.array() or numpy.asarray().

The activation (and deactivation) of the automatic conversion of numpy objects into rpy2 objects can be made with:

你使用的方法沒有問題。r.mydose()調用後返回的東西不是報錯,是因為mydose這個function返回值就是NULL,所以rpy就相應的返回了rpy2.rinterface.NULL,沒有問題。以上是我的測試nofunc是一個什麼也不做的functionhello是輸出Hello world的function

如何通過PypeR來實現在Python中調用R

In [1]: # LOAD PYTHON PACKAGES

In [2]: import pandas as pd

In [3]: import pyper as pr

In [4]: # READ DATA

In [5]: data = pd.read_table(“/home/liuwensui/Documents/data/csdata.txt”, header = 0)

In [6]: # CREATE A R INSTANCE WITH PYPER

In [7]: r = pr.R(use_pandas = True)

In [8]: # PASS DATA FROM PYTHON TO R

In [9]: r.assign(“rdata”, data)

In [10]: # SHOW DATA SUMMARY

In [11]: print r(“summary(rdata)”)

try({summary(rdata)})

LEV_LT3 TAX_NDEB COLLAT1 SIZE1

Min. :0.00000 Min. : 0.0000 Min. :0.0000 Min. : 7.738

1st Qu.:0.00000 1st Qu.: 0.3494 1st Qu.:0.1241 1st Qu.:12.317

Median :0.00000 Median : 0.5666 Median :0.2876 Median :13.540

Mean :0.09083 Mean : 0.8245 Mean :0.3174 Mean :13.511

3rd Qu.:0.01169 3rd Qu.: 0.7891 3rd Qu.:0.4724 3rd Qu.:14.751

Max. :0.99837 Max. :102.1495 Max. :0.9953 Max. :18.587

PROF2 GROWTH2 AGE LIQ

Min. :0.0000158 Min. :-81.248 Min. : 6.00 Min. :0.00000

1st Qu.:0.0721233 1st Qu.: -3.563 1st Qu.: 11.00 1st Qu.:0.03483

Median :0.1203435 Median : 6.164 Median : 17.00 Median :0.10854

Mean :0.1445929 Mean : 13.620 Mean : 20.37 Mean :0.20281

3rd Qu.:0.1875148 3rd Qu.: 21.952 3rd Qu.: 25.00 3rd Qu.:0.29137

Max. :1.5902009 Max. :681.354 Max. :210.00 Max. :1.00018

IND2A IND3A IND4A IND5A

Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000

1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000

Median :1.0000 Median :0.0000 Median :0.00000 Median :0.00000

Mean :0.6116 Mean :0.1902 Mean :0.02692 Mean :0.09907

3rd Qu.:1.0000 3rd Qu.:0.0000 3rd Qu.:0.00000 3rd Qu.:0.00000

Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000

In [12]: # LOAD R PACKAGE

In [13]: r(“library(betareg)”)

Out[13]: ‘try({library(betareg)})\nLoading required package: Formula\n’

In [14]: # ESTIMATE A BETA REGRESSION

In [15]: r(“m – betareg(LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata, subset = LEV_LT3 0)”)

Out[15]: ‘try({m – betareg(LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata, subset = LEV_LT3 0)})\n’

In [16]: # OUTPUT MODEL SUMMARY

In [17]: print r(“summary(m)”)

try({summary(m)})

Call:

betareg(formula = LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata,

subset = LEV_LT3 0)

Standardized weighted residuals 2:

Min 1Q Median 3Q Max

-7.2802 -0.5194 0.0777 0.6037 5.8777

Coefficients (mean model with logit link):

Estimate Std. Error z value Pr(|z|)

(Intercept) 1.229773 0.312990 3.929 8.53e-05 ***

SIZE1 -0.105009 0.021211 -4.951 7.39e-07 ***

PROF2 -2.414794 0.377271 -6.401 1.55e-10 ***

GROWTH2 0.003306 0.001043 3.169 0.00153 **

AGE -0.004999 0.001795 -2.786 0.00534 **

IND3A 0.688314 0.074069 9.293 2e-16 ***

Phi coefficients (precision model with identity link):

Estimate Std. Error z value Pr(|z|)

(phi) 3.9362 0.1528 25.77 2e-16 ***

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Type of estimator: ML (maximum likelihood)

Log-likelihood: 266.7 on 7 Df

Pseudo R-squared: 0.1468

Number of iterations: 25 (BFGS) + 2 (Fisher scoring)

In [18]: # CALCULATE MODEL PREDICTION

In [19]: r(“beta_fit – predict(m, link = ‘response’)”)

Out[19]: “try({beta_fit – predict(m, link = ‘response’)})\n”

In [20]: # SHOW PREDICTION SUMMARY IN R

In [21]: print r(“summary(beta_fit)”)

try({summary(beta_fit)})

Min. 1st Qu. Median Mean 3rd Qu. Max.

0.1634 0.3069 0.3465 0.3657 0.4007 0.6695

In [22]: # PASS DATA FROM R TO PYTHON

In [23]: pydata = pd.DataFrame(r.get(“beta_fit”), columns = [“y_hat”])

In [24]: # SHOW PREDICTION SUMMARY IN PYTHON

In [25]: pydata.y_hat.describe()

Out[25]:

count 1116.000000

mean 0.365675

std 0.089804

min 0.163388

25% 0.306897

50% 0.346483

75% 0.400656

max 0.669489

原創文章,作者:VAYEI,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126598.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VAYEI的頭像VAYEI
上一篇 2024-10-03 23:08
下一篇 2024-10-03 23:09

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論