微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

鉴于强制引入的 NA,Sweave/R/Latex 中没有 pdflatex

如何解决鉴于强制引入的 NA,Sweave/R/Latex 中没有 pdflatex

我在使用 RStudioSweave 生成 pdf 文档时遇到问题。运行代码后,没有错误信息,也没有警告信息。然而,当我在控制台中输入 warnings() 时,我得到了一个列表,这里是其中的一个摘录,其余的警告看起来完全一样:

 `Warning messages:
  1: In normality_test(df,i,j) : NAs introduced by coercion
  2: In normality_test(df,j) : NAs introduced by coercion
  3: In normality_test(df,j) : NAs introduced by coercion
  4: In normality_test(df,j) : NAs introduced by coercion
  5: In if (shapiro.test(df[,i]) > 0.05 & shapiro.test(df[,... :
  the condition has length > 1 and only the first element will be used
  6: In normality_test(df,j) : NAs introduced by coercion`

在我不得不意识到警告之前,代码中相应地丢弃了缺失值 (NA)。为了解决这个问题,我使用了命令 df[is.na(df)] <- 0。它没有改变任何东西。同样的警告仍然存在。相反,我观察到数字的生成就像人们预期的那样。上面显示了一些警告的所有代码,在 RStudio 中运行但未通过 sweave 链接时完美运行。这似乎是矛盾和奇怪的。我拼命尝试了几个小时没有成功。 您对如何解决此问题有任何想法吗?

我正在使用 penguins 数据集。这是使用的代码

df <- read.csv("penguins.csv")
str(df)
#We transform the character variables type into factor ones
i <- sapply(df,is.character)
df[,i] <- lapply(df[,i],as.factor)
df[,8] <- as.factor(df[,8])
str(df)

normality_test <- function(df,j) {
df <- df[!is.na(df[,i])&!is.na(df[,j]),]
plot(c(0,1),c(0,ann = F,bty = 'n',type = 'n',xaxt = 'n',yaxt  = 'n')
if (shapiro.test(df[,j]) > 0.05){
res1 <- cor.test(df[,df[,j],method = "pearson")
text(.5,.5,paste("p.value:",round(res1$p.value,2),"\n r:",round(res1$estimate,2)))
}
else {
res2 <- cor.test(df[,method = "spearman")
text(.5,round(res2$p.value,"rho:",round(res2$estimate,2)))
  }
}
#We define the density function to include diagonal elements
hist_density <- function (df,i) {
tmp <- na.omit(df[,i])
hist(tmp,col = "light blue",probability = TRUE,main=NULL)
lines(density(tmp),col = "red",lwd = 1.5)
}

new_pairs <- function(df,x){
par(mar=c(1,1,1))
n_col<-sum(sapply(df,is.numeric))
par(mfrow=c(n_col,n_col))
n<-ncol(df)
for (i in 1:n){

 for (j in 1:n){
  
  if ((class(df[,i])!="factor" ) & (class(df[,j])!="factor") & i<j) {
    plot(df[,col = df[,x])
   } 
   else if ((class(df[,i])!="factor") & (class(df[,j])!="factor") &  i==j)  {
    hist_density(df,i)
   } 
   else if ((class(df[,j])!="factor") &   i>j){
    normality_test(df,j)
   }
   else {NA}
     }
    }
   }


  new_pairs(df,2)

解决方法

根据代码,如果我们正在检查 p.value 中的 shapiro.test,则使用 $[[ 作为 {{1} 的输出提取该组件} 是一个 shapiro.test

list

-创建使用上述函数的 new_pairs 函数

normality_test <- function(df,i,j) {
    df <- df[!is.na(df[,i])&!is.na(df[,j]),]
    plot(c(0,1),c(0,ann = F,bty = 'n',type = 'n',xaxt = 'n',yaxt  = 'n')
    if (shapiro.test(df[,i])$p.value > 0.05 & 
        shapiro.test(df[,j])$p.value > 0.05){
                res1 <- cor.test(df[,i],df[,j],method = "pearson")
           text(.5,.5,paste("p.value:",round(res1$p.value,2),"\n r:",round(res1$estimate,2)))
            } else {
            res2 <- cor.test(df[,method = "spearman",exact = FALSE)
            text(.5,round(res2$p.value,"rho:",round(res2$estimate,2)))
            }
    }

# // We define the density function to include diagonal elements
hist_density <- function (df,i) {
    tmp <- na.omit(df[,i])
    hist(tmp,col = "light blue",probability = TRUE,main=NULL)
    lines(density(tmp),col = "red",lwd = 1.5)
    }

-测试

new_pairs <- function(df,x){
    par(mar=c(1,1,1))
    n_col<-sum(sapply(df,is.numeric))
    par(mfrow=c(n_col,n_col))
    n <- ncol(df)
    for (i in 1:n){

        for (j in 1:n){
  
      if ((class(df[,i])!="factor" ) & (class(df[,j])!="factor") & i<j) {
        plot(df[,col = df[,x])
        } 
        else if ((class(df[,i])!="factor") & 
           (class(df[,j])!="factor") &  i==j)  {
            hist_density(df,i)
            } 
         else if ((class(df[,i])!="factor" ) & 
                  (class(df[,j])!="factor") &   i>j){
         normality_test(df,j)
       }
    else {NA}
     }
    }
   }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。