简体   繁体   English

lm公式的可变长度不同

[英]lm formula variable lengths differ

I would like to calculate an r square coefficient for each ggplot in facetwrap. 我想为facetwrap中的每个ggplot计算一个r平方系数。 I thought of a solution using a for loop and save the coeficients in a list and than assign the items of the list to each facet. 我想到了一个使用for循环的解决方案,并将系数保存在列表中,然后将列表的项目分配给每个方面。 However, I cannot calculate the coefficients in the for loop. 但是,我无法计算for循环中的系数。 I get an error, that says: 我收到一个错误,说:

Error in model.frame.default(formula = H60percent ~ choice, data = DATA50, : variable lengths differ (found for 'choice') model.frame.default中的错误(公式= H60%〜选择,数据= DATA50,:可变长度不同(找到'选择')

Here is my code: 这是我的代码:

xvalue <- c("Jnr3250","Jnr6450","Jnr12850","Jnr25650")
Rcoef_list <- list()
for (i in 1:length(xvalue)) {
  #print i as a number
  print(i)
  #choose elemnt from from xvalue according to the i (number)
  choice <-  noquote(xvalue[i], right = FALSE)
  print(choice)
  # counts R2
  LM1 =  lm(formula = H60percent ~ choice, data = DATA50)
  Rvalue <-round(as.numeric(summary(LM1)$r.squared[1]),2) 
  R2 <- paste("r^2 == ", Rvalue)
  print(R2)
  #put each R2 in a list
  Rcoef_list[[i]] <- R2
}

if I write the actual name of a column (for example Jnr3250) instead of choice in lm function it works (but only for one value obviously). 如果我写一列的实际名称(例如Jnr3250)而不是在lm函数中进行选择,则它可以工作(但显然仅适用于一个值)。 I tried paste0(choice) and the error is the same. 我尝试了paste0(choice)并且错误是相同的。 I will be glad for any tips or if someone could just point me in the right direction. 我会为任何提示而高兴,或者有人能为我指出正确的方向。

Are your variables separate predictor values, or values of a grouping variable? 您的变量是单独的预测变量值还是分组变量的值? The answer below assumes the former; 下面的答案假定为前者; if it's the latter, use lme4::lmList() or nlme::lmList() ... 如果是后者,请使用lme4::lmList()nlme::lmList() ...

This is pretty close to a duplicate ... I haven't dissected your example to see exactly where you got in trouble, but it's usually best to do these kinds of problems with reformulate() . 这几乎是重复的……我还没有剖析您的示例来确切地了解您遇到了什么问题,但是通常最好使用reformulate()此类问题。 Stripped down, your list would look like this: 剥离下来,您的列表将如下所示:

xvalue <- c("Jnr3250","Jnr6450","Jnr12850","Jnr25650")
Rcoef_list <- list()
for (x in xvalue) {
  form <- reformulate(x, response="H60percent")
  LM1 <- lm(form, data=DATA50)
  Rcoef_list[[x]] <- summary(LM1)$r.squared[1]
}

If you want the r^2 == stuff, you can use sprintf("r^2 == %1.1f", unlist(Rcoef_list)) after running the loop. 如果需要r^2 ==东西,则可以在运行循环后使用sprintf("r^2 == %1.1f", unlist(Rcoef_list))

Your question isn't reproducible, but I think you'll have better luck creating a formula() object to pass into lm() . 您的问题无法重现,但我认为您最好创建一个formula()对象传递给lm() Here's an example with mtcars data, regressing mpg against three of the other variables in the dataset: 这是mtcars数据的示例,针对数据集中的其他三个变量对mpg进行回归:

xcols <- c("disp", "hp", "qsec")
Rcoef_list <- list()

for (i in 1:length(xcols)){
  f <- as.formula(paste0("mpg ~", xcols[i]))
  LM1 <- lm(formula = f, data = mtcars)
  Rvalue <-round(as.numeric(summary(LM1)$r.squared[1]),2) 
  R2 <- paste("r^2 == ", Rvalue) 
  Rcoef_list[[i]] <- R2  
}
Rcoef_list
#> [[1]]
#> [1] "r^2 ==  0.72"
#> 
#> [[2]]
#> [1] "r^2 ==  0.6"
#> 
#> [[3]]
#> [1] "r^2 ==  0.18"

Created on 2019-01-27 by the reprex package (v0.2.1) reprex软件包 (v0.2.1)创建于2019-01-27

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM