3
rm(list=ls())
myData <-read.csv(file="C:/Users/Documents/myfile.csv",header=TRUE, sep=",") 
for(i in names(myData))
{
    colNum <- grep(i,colnames(myData)) ##asigns a value to each column 
    if(is.numeric(myData[3,colNum]))  ##if row 3 is numeric, the entire column is 
   {
        ##print(nxeData[,i])        
        fit <- lm(myData[,i] ~ etch_source_Avg, data=myData) #does a regression for each column in my csv file against my independent variable 'etch'
        rsq <- summary(fit)$r.squared   
   }
}

I'm working on doing a regression loop for multiple columns and comparing them against one dependent variable column. I have the majority of the code written, but now I am unsure how to print out my R squared value for each column against the etch_source_Avg parameter while including the name of that column. Ideally it would something look like:

.765 "variable name 1"

.436 "variable name 2" ...and so on

2
  • 1
    Hi Jacob: Normally I would never do this, but I have an already-written answer to your just-deleted question about performing regressions on subsets of data that may be helpful for you, and I wanted to make sure it reached you just in case it was helpful. Here's the code for my answer. I'd also be happy to post it if you undeleted your question, or to stop bothering you if you prefer. Commented Jun 3, 2015 at 15:01
  • @DavidRobinson Thank you for the help!! I didn't want to break any of the forum rules so I took it down. Feel free to post it to the link and I would be happy to select it as the best answer. You are not bothering me at all :) Commented Jun 5, 2015 at 12:01

1 Answer 1

3

here is a quick rewrite of your code, this should give you what you are looking for. Assigning a value of each column is unnecessary since myData should be a data.frame, as such you can access each column with it's column name.

rm(list=ls())
myData <-read.csv(file="C:/Users/Documents/myfile.csv",header=TRUE, sep=",") 
for(i in names(myData))
{ 
    if(is.numeric(myData[3,i]))  ##if row 3 is numeric, the entire column is 
    {       
       fit <- lm(myData[,i] ~ etch_source_Avg, data=myData) #does a regression for each column in my csv file against my independent variable 'etch'
       rsq <- summary(fit)$r.squared
       writelines(paste(rsq,i,"\n"))
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.