小编Roy*_*lTS的帖子

从r中多项式拟合中分离系数

我已经将一个简单的二阶多项式拟合成时间序列数据,形式如下:

polyfit <- lm(y ~ poly(x,2))
Run Code Online (Sandbox Code Playgroud)

我希望从形式为y = Ax ^ 2 + Bx + C的拟合多项式中提取各自的系数(A,B和C).我自然认为答案可以在polyfit对象中的polyfit $系数中找到,但是这些系数不正确.我已经尝试了一些非常简单的数据集并与excel进行了比较,虽然poly曲线拟合在R和excel中是相同的,但是从excel获得的A,B和C系数是正确的,但是从polyfit对象获得的那些不是吗?我是否从polyfit对象中提取了不正确的信息?为了我的目的,直接从R中提取系数会更方便吗?有人可以帮忙吗?

r polynomial-math

1
推荐指数
1
解决办法
1051
查看次数

组内成员之间的差异

我测量了几次试验的不同处理方法,如下所示:

set.seed(1)
df <- data.frame(treatment = rep(c('baseline', 'treatment 1', 'treatment 2'), 
                                 times=5),
                 round = rep(1:5, each=3),
                 measurement1 = rep(1:5, each=3) + rnorm(15),
                 measurement2 = rep(1:5, each=3) + rnorm(15))

df

#      treatment round measurement1 measurement2
# 1     baseline     1    0.3735462    0.9550664
# 2  treatment 1     1    1.1836433    0.9838097
# 3  treatment 2     1    0.1643714    1.9438362
# 4     baseline     2    3.5952808    2.8212212
# 5  treatment 1     2    2.3295078    2.5939013
# 6  treatment 2     2    1.1795316    2.9189774
# 7     baseline     3    3.4874291    3.7821363 …
Run Code Online (Sandbox Code Playgroud)

r dplyr

1
推荐指数
1
解决办法
402
查看次数

在图中定制离散色标

我想自定义plotly绘图中的颜色.这适用于连续变量和按照文档进行缩放:

library(plotly)

plot_ly(iris, x = Petal.Length, y = Petal.Width,
             color = Sepal.Length, colors = c("#132B43", "#56B1F7"),
             mode = "markers")
Run Code Online (Sandbox Code Playgroud)

但是,如果我将参数设置为离散颜色(字符或因子),这仍然有效,但会发出警告:

> plot_ly(iris, x = Petal.Length, y = Petal.Width,
          color = Sepal.Length>6, colors = c("#132B43", "#56B1F7"),
          mode = "markers")


Warning message:
In RColorBrewer::brewer.pal(N, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

r plotly

1
推荐指数
1
解决办法
5615
查看次数

通过预提交挂钩强制文件大小写

我想在整个 git 存储库中强制使用小写 \xe2\x80\x93 和可能的 Snake_case \xe2\x80\x93 文件名,最好是通过预提交挂钩。唉,谷歌对于这个特定的用例却一无所获。check-case-conflict不完全是我正在寻找的。有这样的事情存在吗?

\n

git pre-commit.com

1
推荐指数
1
解决办法
669
查看次数

ggplot facet轴标签中的智能小数位数

我有一个ggplot像这样的刻面:

library(ggplot2)
grid <- seq(-10,10,length.out=1000)
df <- data.frame(x=rep(grid,2),y=c(grid^2,100+grid^2/100000000),group=rep(c(1,2),each=length(grid)))
ggplot(df,aes(x,y)) + geom_line() + facet_wrap(~group,scales='free')
Run Code Online (Sandbox Code Playgroud)

问题是第2组的y轴值都是相同的,因为它们仅在第6-7位小数中不同.所以我试过了

fmt <- function(){
  function(x) format(x,nsmall = 7,scientific = FALSE)
}
ggplot(df,aes(x,y)) + geom_line() + facet_wrap(~group,scales='free') + scale_y_continuous(labels = fmt())
Run Code Online (Sandbox Code Playgroud)

但是这为第一组增加了许多不必要的小数(呃!).

是否有任何方法可以ggplot显示,但是y轴上的值需要多个小数位才能对所有方面都有所不同?或者这是gridExtra我最好的选择吗?

r ggplot2

0
推荐指数
1
解决办法
2571
查看次数

事件数据开始 - 停止

我有一个包含日期时间和值的数据框,如下所示:

             datetime value
1 2016-05-03 08:51:41     0
2 2016-05-03 10:36:24     0
3 2016-05-03 10:36:32     9
4 2016-05-03 10:45:01     5
5 2016-05-03 10:45:24     0
6 2016-05-03 19:37:02     0
7 2016-05-03 19:37:06     7
8 2016-05-03 19:48:38     0
Run Code Online (Sandbox Code Playgroud)

我想要的是一个表,其中包含值恒定的句点的开始和停止时间.对于上表,预期输出如下:

  value               start                stop
1     0                <NA> 2016-05-03 10:36:32
2     9 2016-05-03 10:36:32 2016-05-03 10:45:01
3     5 2016-05-03 10:45:01 2016-05-03 10:45:24
4     0 2016-05-03 10:45:24 2016-05-03 19:37:06
5     7 2016-05-03 19:37:06 2016-05-03 19:48:38
6     0 2016-05-03 19:48:38                <NA>
Run Code Online (Sandbox Code Playgroud)

原始表的输入

structure(list(datetime = structure(c(1462258301, 1462264584, 
1462264592, 1462265101, …
Run Code Online (Sandbox Code Playgroud)

r time-series

0
推荐指数
1
解决办法
63
查看次数

Redshift varchar太窄了

我有一个表,我填充了来自文件的制表符分隔数据,这些数据的编码似乎不是utf-8,如下所示:

CREATE TABLE tab (
    url varchar(2000),
    ...
);

COPY tab
FROM 's3://input.tsv'
Run Code Online (Sandbox Code Playgroud)

复制完成后,我运行

SELECT
MAX(LEN(url))
FROM tab
Run Code Online (Sandbox Code Playgroud)

返回1525.我想,因为我在浪费空间,所以我可以通过使用varchar(2000)而不是将列重新调整大约四分之一varchar(1525).但是既不重做COPY也不重新设置新表并插入已导入的数据.在这两种情况下,我得到

error:  Value too long for character type
Run Code Online (Sandbox Code Playgroud)

为什么列不能保存这些值?

varchar amazon-web-services amazon-redshift

0
推荐指数
1
解决办法
278
查看次数

情节中的刻面标签

我想更改plotly(_express)情节中的方面标签。这是情节:

import plotly.express as px
tips = px.data.tips()
fig = px.scatter(tips, x="total_bill", y="tip", color="smoker", facet_col="sex")
fig.show()
Run Code Online (Sandbox Code Playgroud)

我想要的是sex=从标签中删除。

plotly plotly-python

0
推荐指数
1
解决办法
2698
查看次数

分组数据框列表

我有一个包含分组名称的数据框,如下所示:

df <- data.frame(group = rep(letters[1:2], each=2),
                 name = LETTERS[1:4])
> df
  group name
1     a    A
2     a    B
3     b    C
4     b    D
Run Code Online (Sandbox Code Playgroud)

我想将其转换为一个列表,该列表键入组名并包含名称.示例输出:

df_out <- list(a=c('A', 'B'),
               b=c('C', 'D'))

> df_out
$a
[1] "A" "B"

$b
[1] "C" "D"
Run Code Online (Sandbox Code Playgroud)

不是一个新问题,但我想在tidyverse中完全做到这一点.

r tidyverse

-3
推荐指数
1
解决办法
6175
查看次数