R 中数据帧的回归循环

标签 r loops statistics dataframe regression

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

最佳答案

这里是对您的代码的快速重写,这应该可以满足您的需求。为每一列分配一个值是不必要的,因为 myData 应该是一个 data.frame,这样你就可以使用它的列名访问每一列。

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"))
    }
}

希望这对您有所帮助。

关于R 中数据帧的回归循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583917/

相关文章:

r - 无法通过 RStudio 的任务调度程序启动浏览器

r - 逻辑回归双环 R

json - Elasticsearch查询中单引号 “json”对象中的R变量

r - RStudio 控制台中的非英语(希伯来语)输出

linux - 遍历一个表并将该表的信息附加到另一个文件

R 代码迭代谷歌地图距离查询的数据帧行

string - Bash 将数据集拆分为每对 1 行

Python Pandas : dataframe. loc 返回 "KeyError: label not in [index]",但 dataframe.index 显示它是

c - 使用 C 中的置换 (nPr, nCr) 函数避免整数溢出

perl - 如何在 Perl 中将 z 分数转换为百分比