0
$\begingroup$

I am trying to run a LASSO Regression via the enet function (from the elasticnet library) in R on each and every one of a large number of individual csv file formatted datasets all within the same file folder for a research project where each dataset has 1 column with observations on the dependent variable called Y, and 30 columns with obs on the independent variables, called X1:X30 respectively.

I have absolutely no idea how to do this or even what search terms to use to look it up, I have already tried in both Google and Bing several times. I believe that the only packages my code as it stands requires are: leaps lars stats plyr dplyr readr elasticnet This is my code to run the LASSO Regression itself once on of you nice people help me either load the data beforehand or adjust this function in order to do that part in the function itself (obviously, I made up the dataframe names for the x & y arguments in the enet() function for this post/question):

## Attempt 2: Run a LASSO regression using 
## the enet function from the elasticnet library
set.seed(11)   
library(elasticnet)
enet_LASSO <- enet(x = as.matrix(df_all_obs_on_all_of_the_IVs), 
                                  y = df_all_obs_on_the_DV, 
                                  lambda = 0, normalize = FALSE)
print(enet_LASSO)
# In order to ascertain which predictors/regressors are still
# included in the version of the model after running a 
# LASSO regression on it for the purpose of variable selection, 
# I am going to use the 'predict' method from the stats package.
LASSO_coeffs <- predict(enet_LASSO, 
                         x = as.matrix(df_all_obs_on_all_of_the_IVs),
                         s = 0.1, mode = "fraction", type = "coefficients")
print(LASSO_coeffs)

Again, I am still a newbie/novice at coding in general. My background is much stronger on the statistics, probability, and econometrics end of data science than the coding side to be honest. But I am trying to learn.

$\endgroup$
3
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Commented Aug 12, 2022 at 13:04
  • $\begingroup$ You have to build a loop which reads the files in the directory, this question might help. Btw this is a programming question, so it's better to ask on stackoverflow.com and it will usually give you an answer faster. But I'd suggest that you learn the basics of programming in R, it will be useful in the long term. $\endgroup$
    – Erwan
    Commented Aug 13, 2022 at 14:55
  • $\begingroup$ Thank you for replying Erwan, I did ask this question on stackoverflow before I asked it here, however, this was the first time I had ever asked a public question on stackoverflow, so I had no idea how fast I would get a response, so I asked it on here as well to hedge my bets! p.s. I thought my master's of science program in Data Analytics Engineering at GMU this research project is for would teach me R programming to a competent, intermediate level, but they have not. I'll be finished in March & still suck at R, very disappointed. $\endgroup$
    – Marlen
    Commented Aug 14, 2022 at 20:51

1 Answer 1

0
$\begingroup$

This question is not worded very well, it needs a lot more detail and probably ought to be broken up into multiple questions honestly, but since you said this is one of the first questions you have asked on here, I'll give it a shot.

First, you'll have to assign the filepath of the file folder with the datasets in it to an object like so:

folderpath <- "/file-folder_filepath"

Then, create a list of the paths for each dataset in that folder with the following line of R code:

csvpaths_list <- list.files(path = folderpath, full.names = TRUE, recursive = TRUE)

Thence, you may read of the datasets in this folder into R with:

datasets_list <- lapply(csvpaths_list, read.csv)

And now we are finally getting somewhere! Ready to run your LASSO Regressions on each dataset in the list we just created by running:

LASSO.fits <- lapply(datasets_list, function(i) 
               enet(x = as.matrix(select(i, starts_with("X"))), 
                    y = i$Y, lambda = 0, normalize = FALSE))

Let me know if this all runs and gets you more or less what you are looking for.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.