简体   繁体   English

dplyr (R) 中的 stderr:我做错了什么?

[英]stderr in dplyr (R): What am I doing wrong?

I'm trying to calculate year-wise standard error for the variable AcrePrice.我正在尝试计算变量 AcrePrice 的年度标准误差。 I'm running the function stderr (also tried with sd(acrePrice)/count(n)).我正在运行函数 stderr (也尝试使用 sd(acrePrice)/count(n))。 Both of these return an error.这两个都返回错误。

Here's the relevant code:这是相关的代码:

library(alr4)
library(tidyverse)

MinnLand %>% group_by(year) %>% summarize(sd(acrePrice)/count(n))
MinnLand %>% group_by(year) %>% summarize(stderr(acrePrice))

Why is there a problem?为什么会出现问题? The mean and SDs are easily calculated.平均值和标准差很容易计算。

The issue with the first function is count , which requires a data.frame, instead it would be n()第一个函数的问题是count ,它需要一个 data.frame,而不是n()

library(dplyr)
MinnLand %>%
     group_by(year) %>%
     summarize(SE = sd(acrePrice)/n(), .groups = 'drop')

-output -输出

# A tibble: 10 x 2
#    year    SE
#   <dbl> <dbl>
# 1  2002 2.25 
# 2  2003 0.840
# 3  2004 0.742
# 4  2005 0.862
# 5  2006 0.849
# 6  2007 0.765
# 7  2008 0.708
# 8  2009 1.23 
# 9  2010 0.986
#10  2011 1.95 

According to ?stderr根据?stderr

stdin(), stdout() and stderr() are standard connections corresponding to input, output and error on the console respectively (and not necessarily to file streams). stdin()、stdout() 和 stderr() 是分别对应于控制台上的输入、输出和错误的标准连接(不一定是文件流)。

We can use std.error from plotrix我们可以使用std.errorplotrix

library(plotrix)
MinnLand %>%
       group_by(year) %>%
       summarize(SE = std.error(acrePrice))

-output -输出

# A tibble: 10 x 2
#    year    SE
#   <dbl> <dbl>
# 1  2002  53.4
# 2  2003  38.6
# 3  2004  37.0
# 4  2005  41.5
# 5  2006  39.7
# 6  2007  36.3
# 7  2008  34.9
# 8  2009  47.1
# 9  2010  42.1
#10  2011  63.6

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

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