问题描述
我有一个147列的数据集,每列7行.我想使用每个列运行单独的回归分析.
I have a data set with 147 columns, each with 7 rows. I want to run a separate regression analysis using each column.
fit = nls(DD$X1, ~ (1/(1+k*xValues)),start=list(k=k))
说明:
DD
是数据集:它是一个数据帧)级别为"X1,","X2"等.
DD
is the dataset: it's a data frame) The levels are "X1," "X2", etc.
xValues = c(1,2,7,14,30,180,720)
k
是我要确定的参数)
但是我想循环执行,例如接下来以DD $ 2的价格购买,一直到DD $ 147.一路上,我想存储拟合值.
But I want to do it in a loop, e.g. next to do it for DD$2, ... all the way to DD$147.Along the way I'd like to store the values of fit.
我是r的新手:有人可以建议如何做吗?非常感谢.
I'm new to r: could someone suggest how to do this? Many thanks.
推荐答案
我在这里使用BodyFat数据集.您必须替换数据集文件名,并为响应和解释变量分配适当的列.
I am using BodyFat dataset here. You have to replace dataset file name and also assign proper column to response and explanatory variables.
formu <-function(a,lis)
{
k=""
k= paste(a,'~',sep="")
h=do.call("paste", c(lis, sep = "+"))
formula1= paste(k,h,sep="")
return(formula1)
}
results=list()
dat = read.table("BodyFatPercentage.txt",header=TRUE)
g=names(dat)
target=names(dat)[2:2]
print(target)
expla=names(dat)[3:16]
for(i in 1:length(expla))
{
formula1=formu(target,list(expla[i]))
model1=lm(formula1,dat)
print(model1)
results[i] = model1
}
print results
这篇关于使用循环一次运行许多回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!