Ниже я попытаюсь объединить комментарии в -хорошо-полезный ответ.
К сожалению, 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
доступны некоторые команды , предоставленные сообществом, что может удовлетворить ваши потребности, поэтому вам не придется возиться с 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