下面我将尝试将评论合并为一个-希望有用的答案。
不幸的是,rcall
它似乎不能很好地处理您需要的大型矩阵。我认为最好调用 R 来使用shell
命令运行脚本并将字符串作为变量保存在dta
文件中。这需要更多的工作,但它肯定是可编程的。
然后您可以将这些变量读入 Stata 并使用内置函数轻松操作它们。例如,您可以将字符串保存在单独的变量中或保存在一个变量中,然后levelsof
按照@Dimitriy 的建议使用。
考虑以下玩具示例:
clear
set obs 5
input str50 string
"this is a string"
"A longer string is this"
"A string that is even longer is this one"
"How many strings do you have?"
end
levelsof string, local(newstr)
`"A longer string is this"' `"A string that is even longer is this one"' `"How many strings do you have?"' `"this is a string"'
tokenize `"`newstr'"'
forvalues i = 1 / `: word count `newstr'' {
display "``i''"
}
A longer string is this
A string that is even longer is this one
How many strings do you have?
this is a string
根据我的经验,程序喜欢rcall
并且rsource
对于简单的任务很有用。但是,对于更复杂的工作,它们可能会成为真正的麻烦,在这种情况下,我个人只是求助于真实的东西,即直接使用其他软件。
正如@Dimitriy 还指出的那样,现在有一些社区贡献的命令可用于lasso
,ehich 可以满足您的需求,因此您不必摆弄 R:
search lasso
5 packages found (Stata Journal and STB listed first)
-----------------------------------------------------
elasticregress from http://fmwww.bc.edu/RePEc/bocode/e
'ELASTICREGRESS': module to perform elastic net regression, lasso
regression, ridge regression / elasticregress calculates an elastic
net-regularized / regression: an estimator of a linear model in which
larger / parameters are discouraged. This estimator nests the LASSO / and
lars from http://fmwww.bc.edu/RePEc/bocode/l
'LARS': module to perform least angle regression / Least Angle Regression
is a model-building algorithm that / considers parsimony as well as
prediction accuracy. This / method is covered in detail by the paper
Efron, Hastie, Johnstone / and Tibshirani (2004), published in The Annals
lassopack from http://fmwww.bc.edu/RePEc/bocode/l
'LASSOPACK': module for lasso, square-root lasso, elastic net, ridge,
adaptive lasso estimation and cross-validation / lassopack is a suite of
programs for penalized regression / methods suitable for the
high-dimensional setting where the / number of predictors p may be large
pdslasso from http://fmwww.bc.edu/RePEc/bocode/p
'PDSLASSO': module for post-selection and post-regularization OLS or IV
estimation and inference / pdslasso and ivlasso are routines for
estimating structural / parameters in linear models with many controls
and/or / instruments. The routines use methods for estimating sparse /
sivreg from http://fmwww.bc.edu/RePEc/bocode/s
'SIVREG': module to perform adaptive Lasso with some invalid instruments /
sivreg estimates a linear instrumental variables regression / where some
of the instruments fail the exclusion restriction / and are thus invalid.
The LARS algorithm (Efron et al., 2004) is / applied as long as the Hansen