R 面板数据的回归分析

R 面板数据的回归分析,r,dataframe,lm,R,Dataframe,Lm,我想对面板数据进行线性回归。下面是我的代码,但我不明白为什么不返回fit和rsq。有什么建议吗 示例代码: for(i in names(df)) { if(is.numeric(df[3,i])) ##if row 3 is numeric, the entire column is { fit <- lm(df[3,i] ~ Gender, data=df) #does a regression for each column in my csv f

我想对面板数据进行线性回归。下面是我的代码,但我不明白为什么不返回fit和rsq。有什么建议吗

示例代码:

for(i in names(df))
{ 
  if(is.numeric(df[3,i]))  ##if row 3 is numeric, the entire column is 
  {       
    fit <- lm(df[3,i] ~ Gender, data=df) #does a regression for each column in my csv file against my independent variable 'etch'
    rsq <- summary(fit)$r.squared
  }
}
    df<-structure(list(id = c(1, 1, 2, 2, 2), id1 = c(1, 2, 1, 2, 3), 
    a1 = c(5, 8, 7, 6, 3), a2 = c(1, 4, 3, 10, 5), a3 = c(2, 
    34, 3, 12, 6), a4 = c(9, 2, 3, 12, 7), a5 = c(0, 0, 0, 7, 
    8), a6 = c(7, 7, 0, 0, 9), a7 = c(5, 8, 7, 6, 0), a8 = c(1, 
    4, 3, 10, 3), a9 = c(2, 34, 3, 12, 3), a10 = c(9, 2, 3, 12, 
    3), Gender = c(1, 2, 1, 1, 2)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L), spec = structure(list(
    cols = list(id = structure(list(), class = c("collector_double", 
    "collector")), id1 = structure(list(), class = c("collector_double", 
    "collector")), a1 = structure(list(), class = c("collector_double", 
    "collector")), a2 = structure(list(), class = c("collector_double", 
    "collector")), a3 = structure(list(), class = c("collector_double", 
    "collector")), a4 = structure(list(), class = c("collector_double", 
    "collector")), a5 = structure(list(), class = c("collector_double", 
    "collector")), a6 = structure(list(), class = c("collector_double", 
    "collector")), a7 = structure(list(), class = c("collector_double", 
    "collector")), a8 = structure(list(), class = c("collector_double", 
    "collector")), a9 = structure(list(), class = c("collector_double", 
    "collector")), a10 = structure(list(), class = c("collector_double", 
    "collector")), Gender = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))
for(名称中的i(df))
{ 
如果(是.numeric(df[3,i])##如果第3行是数值的,则整个列为
{       

拟合要在每列上拟合线性回归,可以使用
lappy
。我们使用
重新格式化
从列名创建公式对象,并在
lappy
中使用它。可以从每个模型的
摘要
中提取R平方值

cols <- grep('a\\d+', names(df), value = TRUE)
cols
#[1] "a1"  "a2"  "a3"  "a4"  "a5"  "a6"  "a7"  "a8"  "a9"  "a10"

lapply(cols, function(x) {
  lm(reformulate('Gender', x), df)
}) -> fit

r.squared <- sapply(fit, function(x) summary(x)$r.squared)
cols-fit

r、 squared“return”的确切含义是什么?您是否希望for循环返回某种类型的值?或者您是否正在尝试使用
writelines
将数据写入某个未命名的文件?这是一个输入错误吗?因为没有名为
writelines
的基r函数(有一个
writeline
,这是不同的,因为R名称区分大小写)@MrFlick谢谢我删除了writeline,但仍然没有计算摘要统计信息