变量名称的 R 循环到 运行 线性回归模型
R Loop for Variable Names to run linear regression model
首先,我对此很陌生,所以我的 method/thinking 可能是错误的,我已经使用 R 和 R studio 将 xlsx 数据集导入到数据框中。我希望能够遍历列名以获取其中完全包含“10”的所有变量,以便 运行 进行简单的线性回归。所以这是我的代码:
indx <- grepl('_10_', colnames(data)) #list returns all of the true values in the data set
col10 <- names(data[indx]) #this gives me the names of the columns I want
这是我的 for 循环,returns 出错:
temp <- c()
for(i in 1:length(col10)){
temp = col10[[i]]
lm.test <- lm(Total_Transactions ~ temp[[i]], data = data)
print(temp) #actually prints out the right column names
i + 1
}
甚至可以 运行 循环将这些变量放入线性回归模型中吗?我收到的错误是:"Error in model.frame.default(formula = Total_Transactions ~ temp[[i]], : variable lengths differ (found for 'temp[[i]]')"。如果有人能指出我正确的方向,我将不胜感激。谢谢
您可以创建一个临时子集,其中 select 只有回归中使用的列。这样,您就不需要在公式中插入临时名称。
遵守您的代码,这应该可以解决问题。
for(i in 1:length(col10)){
tempSubset <- data[,c("Total_Transactions", col10[i]]
lm.test <- lm(Total_Transactions ~ ., data = tempSubset)
i + 1
}
好的,我会post回答。我将使用数据集 mtcars
作为示例。我相信它将适用于您的数据集。
首先,我创建了一个商店 lm.test
,class list
的一个对象。在你的代码中,你每次通过循环分配 lm(.)
的输出,最后你只会有最后一个,所有其他的都会被新的重写。
然后,在循环内,我使用函数 reformulate
来组合回归公式。还有其他方法可以做到这一点,但这个很简单。
# Use just some columns
data <- mtcars[, c("mpg", "cyl", "disp", "hp", "drat", "wt")]
col10 <- names(data)[-1]
lm.test <- vector("list", length(col10))
for(i in seq_along(col10)){
lm.test[[i]] <- lm(reformulate(col10[i], "mpg"), data = data)
}
lm.test
现在您可以将结果列表用于各种事情。我建议你开始使用 lapply
和朋友。
例如,要提取系数:
cfs <- lapply(lm.test, coef)
为了获得摘要:
smry <- lapply(lm.test, summary)
熟悉 *apply
函数后就会变得非常简单。
首先,我对此很陌生,所以我的 method/thinking 可能是错误的,我已经使用 R 和 R studio 将 xlsx 数据集导入到数据框中。我希望能够遍历列名以获取其中完全包含“10”的所有变量,以便 运行 进行简单的线性回归。所以这是我的代码:
indx <- grepl('_10_', colnames(data)) #list returns all of the true values in the data set
col10 <- names(data[indx]) #this gives me the names of the columns I want
这是我的 for 循环,returns 出错:
temp <- c()
for(i in 1:length(col10)){
temp = col10[[i]]
lm.test <- lm(Total_Transactions ~ temp[[i]], data = data)
print(temp) #actually prints out the right column names
i + 1
}
甚至可以 运行 循环将这些变量放入线性回归模型中吗?我收到的错误是:"Error in model.frame.default(formula = Total_Transactions ~ temp[[i]], : variable lengths differ (found for 'temp[[i]]')"。如果有人能指出我正确的方向,我将不胜感激。谢谢
您可以创建一个临时子集,其中 select 只有回归中使用的列。这样,您就不需要在公式中插入临时名称。
遵守您的代码,这应该可以解决问题。
for(i in 1:length(col10)){
tempSubset <- data[,c("Total_Transactions", col10[i]]
lm.test <- lm(Total_Transactions ~ ., data = tempSubset)
i + 1
}
好的,我会post回答。我将使用数据集 mtcars
作为示例。我相信它将适用于您的数据集。
首先,我创建了一个商店 lm.test
,class list
的一个对象。在你的代码中,你每次通过循环分配 lm(.)
的输出,最后你只会有最后一个,所有其他的都会被新的重写。
然后,在循环内,我使用函数 reformulate
来组合回归公式。还有其他方法可以做到这一点,但这个很简单。
# Use just some columns
data <- mtcars[, c("mpg", "cyl", "disp", "hp", "drat", "wt")]
col10 <- names(data)[-1]
lm.test <- vector("list", length(col10))
for(i in seq_along(col10)){
lm.test[[i]] <- lm(reformulate(col10[i], "mpg"), data = data)
}
lm.test
现在您可以将结果列表用于各种事情。我建议你开始使用 lapply
和朋友。
例如,要提取系数:
cfs <- lapply(lm.test, coef)
为了获得摘要:
smry <- lapply(lm.test, summary)
熟悉 *apply
函数后就会变得非常简单。