I can make numpy ndarrays with rec2csv,
data = recfromcsv(dataset1, names=True)
xvars = ['exp','exp_sqr','wks','occ','ind','south','smsa','ms','union','ed','fem','blk']
y = data['lwage']
X = data[xvars]
c = ones_like(开发者_开发百科data['lwage'])
X = add_field(X, 'constant', c)
But, I have no idea how to take this into an R data frame usable by Rpy2,
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(X))
ValueError: Nothing can be done for the type <class 'numpy.core.records.recarray'> at the moment.
p = roptim(theta,robjects.r['ols'],method="BFGS",hessian=True ,y= robjects.FloatVector(y),X = base.matrix(array(X)))
ValueError: Nothing can be done for the type <type 'numpy.ndarray'> at the moment.
Just to get an RPY2 DataFrame from a csv file, in RPY2.3, you can just do:
df = robjects.DataFrame.from_csvfile('filename.csv')
Documentation here.
I'm not 100% sure I understand your issue, but a couple things:
1) if it's ok, you can read a csv into R directly, that is:
robjects.r('name <- read.csv(filename.csv)')
After which you can refer to the resulting data frame in later functions.
Or 2) you can convert a numpy array into a data frame - to do this you need to import the package 'rpy2.robjects.numpy2ri'
Then you could do something like:
array_ex = np.array([[4,3],[3,2], [1,5]])
rmatrix = robjects.r('matrix')
rdf = robjects.r('data.frame')
rlm = robjects.r('lm')
mat_ex = rmatrix(array_ex, ncol = 2)
df_ex = rdf(mat_ex)
fit_ex = rlm('X1 ~ X2', data = df_ex)
Or whatever other functions you wanted. There may be a more direct way - I get frustrated going between the two data types and so I am much more likely to use option 1) if possible.
Would either of these methods get you to where you need to be?
精彩评论