如何从R中的y变量估计x变量?

时间:2018-05-15 02:16:03

标签: r ggplot2 tidyverse

这是我的数据:

# A tibble: 8 x 3
    CFU strain diltn
  <dbl> <chr>  <dbl>
1 159   aM12    8748
2 124.  aM12    2916
3  76.5 aM12     972
4  22   aM12     324
5  16.5 aM12     108
6  17   aM12      36
7  22.5 aM12      12
8  17.5 aM12       4

这似乎是一个简单的问题,但我主要使用R来获取数据的基本摘要,并将它们绘制成图形(使用dplyr和ggplot)。

我可以绘制图表:

ggplot(data=data, aes(x=diltn, y=CFU))+
  geom_point()+
  geom_line()+
  scale_x_log10()

enter image description here

我想估计什么&#34; diltn&#34; (x变量),我会得到77&#34; CFU&#34; (y变量)。

我在excel中对此进行了管理,并将其绘制如下,以说明我想要实现的目标:

enter image description here

2 个答案:

答案 0 :(得分:3)

这实际上是一个比它看起来更棘手的问题(一般而言)。它并不能完成(有很多选项),但它在很大程度上取决于数据的行为方式。例如,假设感兴趣的y值是20而不是77.在4到324之间的任何稀释值现在都是合理的&#34;答案。

为了解决这个问题,我们使用统计模型。如果我正确地猜测并且您正在使用剂量反应模型(或类似的东西 - 例如我在测定中使用标准曲线),您可以查看insertNewRow(){ var newItem = this.createNewRowData(); var res = this.gridOptions.api.updateRowData({add: [newItem]}); var updatedColDefs = []; **//how can I update columnDefs here, so that all fields are editable for this record?** var col = this.gridOptions.api.setColumnDefs(updatedColDefs); } createNewRowData(){ var newData = []; this.tableColumns.forEach(item => { if (item.headerName == "isChanged") { newData["isChanged"] = "inserted"; } else { newData[item.headerName] = ""; } }); console.log(newData); return newData; } drm()包中,可以适当地拟合这些曲线。

类似的东西:

drc

dose response curve 然后使用mod <- drm(CFU ~ diltn, data = data, fct = LL.4()) plot(mod) 函数来提取相关数据。我使用标准曲线,我发现以下设置很有用,但根据数据的工作方式,您可能需要不同的设置。

ED

自从我读了它上面的小插曲以来已经有一段时间了,所以你可能需要做一些阅读以确保你得到正确的结果。

答案 1 :(得分:1)

根据散点图,我们可能会将非线性回归线拟合到数据集中。假设您的数据集名为dat。我们可以使用nls函数来拟合回归模型。请注意,需要一些努力并思考找到可能的等式和起始值。在这种情况下,等式为CFU ~ a * diltn/(b + diltn)ab的起始值分别为1001000

library(tidyverse)

fit <- nls(formula = CFU ~ a * diltn/(b + diltn), 
           start = list(a = 100, b = 1000), data = dat)

summary(fit)

# Formula: CFU ~ a * diltn/(b + diltn)
# 
# Parameters:
#   Estimate Std. Error t value Pr(>|t|)    
# a   187.32      21.25   8.814 0.000118 ***
# b  1514.27     517.50   2.926 0.026420 *  
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 13.17 on 6 degrees of freedom
# 
# Number of iterations to convergence: 4 
# Achieved convergence tolerance: 3.555e-06

为了直观地检查模型拟合,我们可以首先使用diltn从1到9000创建第二个数据框。然后我们可以使用predict函数根据{{1}预测CFU }和模型diltn

fit

enter image description here

模特对我来说很好看。

最后,我们可以过滤dat2 <- data_frame(diltn = 1:9000) %>% mutate(Pred = predict(fit, .)) ggplot(data = dat, aes(x = diltn, y = CFU))+ geom_point() + geom_line(data = dat2, aes(x = diltn, y = Pred), color = "red") 值,以查找Pred的可能值。在这种情况下,我认为diltn可能是一个可能的答案。

1057

或者由于我们已经拟合了非线性回归模型,并且我们知道拟合参数dat2 %>% filter(Pred > 76.9, Pred < 77.1) # # A tibble: 5 x 2 # diltn Pred # <int> <dbl> # 1 1055 76.9 # 2 1056 77.0 # 3 1057 77.0 # 4 1058 77.0 # 5 1059 77.1 a,我们可以设置b = CFU并计算{ {1}}。我的计算结果显示77diltn