R中的外推
Extrapolation in R
我有一个参考数据集,p
,
p=structure(list(v = 0:26, t = c(Inf, 1.016, 0.568, 0.418666666666667,
0.344, 0.2992, 0.269333333333333, 0.248, 0.232, 0.219555555555556,
0.2096, 0.201454545454545, 0.194666666666667, 0.188923076923077,
0.184, 0.179733333333333, 0.176, 0.172705882352941, 0.169777777777778,
0.167157894736842, 0.1648, 0.162666666666667, 0.160727272727273,
0.15895652173913, 0.157333333333333, 0.15584, 0.154461538461538
)), .Names = c("v", "t"), row.names = c(NA, -27L), class = "data.frame")
其中p$v
和p$t
有如下关系:
t=(0.16*(0.75*v + 5.6))/v
我的第二个数据集是测量数据,w
,包含相同的变量,如:
w=structure(list(v = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), t = c(0.235291176470588,
0.354020375722543, 0.310974343434343, 0.25272725498699, 0.20351968240702,
0.163155804025208, 0.132330740162655, 0.108593108108108, 0.0859813015873016,
0.0655131899302683, 0.0492580103144236, 0.0368029846567365, 0.030538003253355,
0.0300744415648525, 0.0347586421891237, 0.0451097744360902, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("v", "t"
), row.names = c(NA, -27L), class = "data.frame")
我的参考数据p
遵循幂律,我想根据数据拟合的幂律进行预测,以取代我测量数据的NA
。我怎么能在 R 中做到这一点?
这样的怎么样?
# Non-linear power law fit
fit.nls <- nls(
t ~ a * v^b,
data = w[-1, ],
start = list(a = 0.4, b = -0.7),
na.action = na.exclude);
# Linear fit with log-log transformation
fit.lm <- lm(
log(t) ~ log(v),
data = w[-1, ])
# Plot
w %>%
rename(t.src = t) %>%
mutate(
t.pred.nls = predict(fit.nls, data.frame(v = v)),
t.NA.pred.nls = ifelse(
is.na(t.src),
predict(fit.nls, data.frame(v = v)),
t.src),
t.pred.lm = exp(predict(fit.lm, data.frame(v = v)))) %>%
gather(source, t, 3:5) %>%
ggplot(aes(v, t, colour = source)) +
geom_line() +
geom_point(aes(v, t.src), colour = "black") +
scale_colour_manual(values = c(
"t.NA.pred.nls" = "black",
"t.pred.nls" = "blue",
"t.pred.lm" = "red"));
黑点为实际测量值。蓝色曲线为幂律非线性模型拟合,红色曲线为log-log变换后的线性拟合;黑色曲线对应于您的原始数据,其中 v = NA
值已替换为非线性模型拟合估计值(因此对于 v > 15
,黑色和蓝色曲线重叠)。
我有一个参考数据集,p
,
p=structure(list(v = 0:26, t = c(Inf, 1.016, 0.568, 0.418666666666667,
0.344, 0.2992, 0.269333333333333, 0.248, 0.232, 0.219555555555556,
0.2096, 0.201454545454545, 0.194666666666667, 0.188923076923077,
0.184, 0.179733333333333, 0.176, 0.172705882352941, 0.169777777777778,
0.167157894736842, 0.1648, 0.162666666666667, 0.160727272727273,
0.15895652173913, 0.157333333333333, 0.15584, 0.154461538461538
)), .Names = c("v", "t"), row.names = c(NA, -27L), class = "data.frame")
其中p$v
和p$t
有如下关系:
t=(0.16*(0.75*v + 5.6))/v
我的第二个数据集是测量数据,w
,包含相同的变量,如:
w=structure(list(v = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), t = c(0.235291176470588,
0.354020375722543, 0.310974343434343, 0.25272725498699, 0.20351968240702,
0.163155804025208, 0.132330740162655, 0.108593108108108, 0.0859813015873016,
0.0655131899302683, 0.0492580103144236, 0.0368029846567365, 0.030538003253355,
0.0300744415648525, 0.0347586421891237, 0.0451097744360902, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("v", "t"
), row.names = c(NA, -27L), class = "data.frame")
我的参考数据p
遵循幂律,我想根据数据拟合的幂律进行预测,以取代我测量数据的NA
。我怎么能在 R 中做到这一点?
这样的怎么样?
# Non-linear power law fit
fit.nls <- nls(
t ~ a * v^b,
data = w[-1, ],
start = list(a = 0.4, b = -0.7),
na.action = na.exclude);
# Linear fit with log-log transformation
fit.lm <- lm(
log(t) ~ log(v),
data = w[-1, ])
# Plot
w %>%
rename(t.src = t) %>%
mutate(
t.pred.nls = predict(fit.nls, data.frame(v = v)),
t.NA.pred.nls = ifelse(
is.na(t.src),
predict(fit.nls, data.frame(v = v)),
t.src),
t.pred.lm = exp(predict(fit.lm, data.frame(v = v)))) %>%
gather(source, t, 3:5) %>%
ggplot(aes(v, t, colour = source)) +
geom_line() +
geom_point(aes(v, t.src), colour = "black") +
scale_colour_manual(values = c(
"t.NA.pred.nls" = "black",
"t.pred.nls" = "blue",
"t.pred.lm" = "red"));
黑点为实际测量值。蓝色曲线为幂律非线性模型拟合,红色曲线为log-log变换后的线性拟合;黑色曲线对应于您的原始数据,其中 v = NA
值已替换为非线性模型拟合估计值(因此对于 v > 15
,黑色和蓝色曲线重叠)。