我有2个不同的数据框,我想执行线性回归
我为它编写了以下代码
mydir<- "/media/dev/Daten/Task1/subject1/t1"
#multiple subject paths should be given here
# read full paths
myfiles<- list.files(mydir,pattern = "regional_vol*",full.names=T)
# initialise the dataframe from first file
df<- read.table( myfiles[1], header = F,row.names = NULL, skip = 3, nrows = 1,sep = "\t")
# [-c(1:3),]
df
#read all the other files and update dataframe
#we read 4 lines to read the header correctly, then remove 3
ans<- lapply(myfiles[-1], function(x){ read.table( x, header = F, skip = 3, nrows = 1,sep = "\t") })
ans
#update dataframe
#[-c(1:3),]
lapply(ans, function(x){df<<-rbind(df,x)} )
#this should be the required dataframe
uncorrect<- array(df)
# Linear regression of ICV extracted from global size FSL
# Location where your icv is located
ICVdir <- "/media/dev/Daten/Task1/T1_Images"
#loding csv file from ICV
mycsv <- list.files(ICVdir,pattern = "*.csv",full.names = T )
af<- read.csv(file = mycsv,header = TRUE)
ICV<- as.data.frame(af[,2],drop=FALSE)
#af[1,]
#we take into consideration second column of csv
#finalcsv <-lapply(mycsv[-1],fudnction(x){read.csv(file="global_size_FSL")})
subj1<- as.data.frame(rep(0.824,each=304))
plot(df ~ subj1, data = df,
xlab = "ICV value of each subject",
ylab = "Original uncorrected volume",
main="intercept calculation"
)
fit <- lm(subj1 ~ df )
数据帧df具有以下格式的304值
6433 6433
1430 1430
1941 1941
3059 3059
3932 3932
6851 6851
和另一个数据帧Subj1具有以下格式的304值
0.824
0.824
0.824
0.824
0.824
当我运行我的代码时,我会发生以下错误
Error in model.frame.default(formula = subj1 ~ df, drop.unused.levels = TRUE) :
invalid type (list) for variable 'subj1'
为什么来自变量subj1的data.frame值无效的任何建议
答案 0 :(得分:1)
如前所述,您试图将data.frame作为一个独立变量。尝试:
fit <- lm(subj1 ~ ., data=df )
这将使用数据框中的所有变量,只要subj1
是因变量的名称,而不是数据框本身。
如果df有两列作为预测变量,而subj1是预测(依赖)变量,将两者结合起来,给它们正确的列名,并以上面的格式创建模型。
类似的东西:
data <- cbind(df, subj1)
names(data) <- c("var1", "var2", "subj1")
fit <- lm(subj1 ~ var1 + var2, data=df )
编辑:一些指示: