为什么在for循环中从lm模型中提取系数时会得到NAs和意外的结果?

为什么在for循环中从lm模型中提取系数时会得到NAs和意外的结果?,r,R,为了确定每个位置的每个实验的唯一回归斜率,我尝试对28个实验的位置(纬度)值运行一个不同的模型。是指向我的数据的链接。在改变预测值时,类似的代码曾经用于循环回归,但现在这段代码正在做一些令人困惑的事情。P值列和slope列包含28行NAs和55行值,R平方列包含88行值。我运行了一个示例模型,但在表中的任何位置都找不到任何值 data$Grain<- as.numeric(as.character(data$Grain)) data2 <- subset(data, Age >

为了确定每个位置的每个实验的唯一回归斜率,我尝试对28个实验的位置(纬度)值运行一个不同的模型。是指向我的数据的链接。在改变预测值时,类似的代码曾经用于循环回归,但现在这段代码正在做一些令人困惑的事情。P值列和slope列包含28行NAs和55行值,R平方列包含88行值。我运行了一个示例模型,但在表中的任何位置都找不到任何值

data$Grain<- as.numeric(as.character(data$Grain))
data2 <- subset(data, Age >1)

ls1 <- list()
ls2 <- list()
ls3 <- list()
data2$Key3 <- paste(data2$LAT, data2$Experiment)

for (i in unique(data2$Key3)){ 
  model <- lm(as.formula(paste0("Grain", "~", "Age")), subset(data2, Key3 == i))
  pval <- summary(model)$coefficients[,4] #extracts P values for model
  rsq <- summary(model)$r.squared
  slope <- summary(model)$coefficients[,1]
  ls1 <- c(ls1, pval[2]) #extracts P values of second row, which is the predictor Age
  ls2 <- c(ls2, rsq)
  ls3 <- c(ls3, slope[2]) #extracts slope of second row, which is the predictor Age
}

Ps <- do.call(rbind, ls1)
Rs <- do.call(rbind, ls2)
slopes <- do.call(rbind, ls3)

table <- cbind(Ps, Rs, slopes)

## Sample model- sorry the Key3 values are so ugly
summary(lm(Grain~Age, subset(data2, Key3 == '44.06751 IREE- N Rate')))

data$Grain代码现在似乎可以工作了。如果您使用
paste0()
,请使用
Key3='44.06751ree-N Rate'
,因为默认情况下它不像
paste()
那样添加空格


data$Grain不要忘记
paste()
中的默认sep是一个
空格。检查这是否是导致错误的原因<代码>数据2$Key3
data$Grain <- readr::parse_number(data$Grain)
data2 <- subset(data, Age > 1)

ls1 <- list()
ls2 <- list()
ls3 <- list()
data2$Key3 <- paste0(data2$LAT, data2$Experiment)

for (i in unique(data2$Key3)){ 
  model <- lm(as.formula(paste0("Grain", "~", "Age")), subset(data2, Key3 == i))
  pval <- summary(model)$coefficients[,4] #extracts P values for model
  rsq <- summary(model)$r.squared
  slope <- summary(model)$coefficients[,1]
  ls1 <- c(ls1, pval[2]) #extracts P values of second row, which is the predictor Age
  ls2 <- c(ls2, rsq)
  ls3 <- c(ls3, slope[2]) #extracts slope of second row, which is the predictor Age
}

Ps <- do.call(rbind, ls1)
Rs <- do.call(rbind, ls2)
slopes <- do.call(rbind, ls3)

table <- cbind(Ps, Rs, slopes)

## Sample model- sorry the Key3 values are so ugly
summary(lm(Grain~Age, subset(data2, Key3 == '44.06751IREE- N Rate')))

summary(lm(Grain~Age, subset(data2, Key3 == '44.06751IREE- N Rate')))

Call:
lm(formula = Grain ~ Age, data = subset(data2, Key3 == "44.06751IREE- N Rate"))

Residuals:
   Min     1Q Median     3Q    Max 
  -245   -210     19    135    586 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   1175.3       68.1    17.2   <2e-16 ***
Age           -310.2       22.2   -14.0   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 211 on 132 degrees of freedom
  (10 observations deleted due to missingness)
Multiple R-squared:  0.596, Adjusted R-squared:  0.593 
F-statistic:  195 on 1 and 132 DF,  p-value: <2e-16