【发布时间】:2021-11-26 11:33:32
【问题描述】:
我可以从线性回归中找到系数和截距,但找不到合适的方法来获取相应变量趋势的 p 值和 z 值。此外,无法找到将输出结果保存为 excel 格式的方法。数据为here。时间有 24 个变量。我没有得到 z 统计量和 p 值,此外,第一种方法的估计也是不正确的。我哪里错了?
library("trend")
# read ozone data (I converted to a text file first)
otm <- read.table("D:/data.txt",header=T)
# make a data frame version
otm_df <- data.frame(otm)
markers <- sample(0:1, replace = T, size = 11)
# calculate OLS slope for all columns
# the -1 at end removes the intercepts
ols <- sapply(otm_df, function(x) coef(lm(markers ~ x))[-1])
我试过这个方法。我没有得到 z 统计数据,也无法将其保存为 excel 格式。
library(reshape2)
DF <- reshape2::melt(otm, id.var = "Year")
library(broom); library(tidyverse)
ols <- DF %>% nest(data = -variable) %>%
mutate(model = map(data, ~lm(value ~ Year, data = .)),
tidied = map(model, tidy)) %>%
unnest(tidied)
#to save the results in excel format (not working here for me)
capture.output(summary(ols), file = "ols.csv" )
write.csv(ols, file.path('E:/',filename = "ols2.csv"), row.names = TRUE)
# A tibble: 48 x 8
variable data model term estimate std.error statistic p.value
<fct> <list> <list> <chr> <dbl> <dbl> <dbl> <dbl>
1 BanTES <tibble [11 x 2]> <lm> (Intercept) -236. 488. -0.483 0.641
2 BanTES <tibble [11 x 2]> <lm> Year 0.139 0.242 0.572 0.582
3 SriTES <tibble [11 x 2]> <lm> (Intercept) 220. 351. 0.627 0.546
4 SriTES <tibble [11 x 2]> <lm> Year -0.0935 0.174 -0.536 0.605
5 AfgTES <tibble [11 x 2]> <lm> (Intercept) 364. 444. 0.820 0.434
6 AfgTES <tibble [11 x 2]> <lm> Year -0.161 0.221 -0.730 0.484
7 BhuTES <tibble [11 x 2]> <lm> (Intercept) 373. 831. 0.449 0.664
8 BhuTES <tibble [11 x 2]> <lm> Year -0.170 0.413 -0.412 0.690
9 IndTES <tibble [11 x 2]> <lm> (Intercept) -342. 213. -1.60 0.143
10 IndTES <tibble [11 x 2]> <lm> Year 0.190 0.106 1.80 0.106
summary(ols)
variable data.Length data.Class data.Mode model.Length model.Class model.Mode term
BanTES : 2 2 tbl_df list 12 lm list Length:48
SriTES : 2 2 tbl_df list 12 lm list Class :character
AfgTES : 2 2 tbl_df list 12 lm list Mode :character
BhuTES : 2 2 tbl_df list 12 lm list
IndTES : 2 2 tbl_df list 12 lm list
NepTES : 2 2 tbl_df list 12 lm list
(Other):36 2 tbl_df list 12 lm list
任何帮助都会很有用。提前谢谢你!
【问题讨论】:
-
help("summary.lm") -
没有关于 p 值和 z 统计的内容
-
有(当然它是 t 统计量而不是 z 统计量,因为它应该是)。
-
我用 excel 检查了估算值。 excel和R软件的OLS斜率值不匹配。为什么会这样?
标签: r regression linear-regression p-value trend