我正在尝试在car库中使用deltaMethod ,但我得到了一个奇怪的错误.
library(car)
x=c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11)
y=c(-0.78636545259908996, -0.48499513735893701, -0.61035206318152102, -0.60156864486986295, -0.61323703005521701, -0.33091952573467498, -0.269895273193686, -0.32222378534205598, -0.53183084634683997, -0.96631869084439304, -0.77105781684519603, -0.524039870915605, -0.41181303531095498, -0.27581842299642001, -0.72085673574325404, -0.35874718580022702, -0.30752543764527501, -0.090745334342823197, -0.465889655296298, -0.20115970219526799, -0.0511742487116199, 0.0100170907454752, -0.176138595601495, 0.042138062483845398, 0.00081247733328697303, -0.0045220167465173499, 0.57326735553016905, 0.116862163616526, 0.0072264835163109399, 0.48714531471859701, 0.83738659120408598, …Run Code Online (Sandbox Code Playgroud) 有一个很好的解释这里的如何使用GGPLOT2创建散点图,利用NLS拟合数据,并绘制配合,都在一条线,像这样
myhist = data.frame(size = 10:27, counts = c(1L, 3L, 5L, 6L, 9L, 14L, 13L, 23L, 31L, 40L, 42L, 22L, 14L, 7L, 4L, 2L, 2L, 1L) )
ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() +
geom_smooth(method="nls", formula = y ~ N * dnorm(x, m, s), se=F,
start=list(m=20, s=5, N=300))
Run Code Online (Sandbox Code Playgroud)
我的问题是:使用这种结构,是否有可能从该调用中拉出实际的nls对象?我想知道我的系数等.现在我无法弄清楚如何在不进行单独的nls调用的情况下获取它们.
我想通过使用Stata的非线性最小二乘估计以下函数:

我正在测试另一个papper的结果,并且想要使用Stata,因为它与我复制的论文中使用的软件/解算器相同,例如,它比使用GAMS更容易.
我的问题是我找不到任何方法来写出上面等式的总和部分.在我的数据中,我所拥有的是一个单独的观察,其中j的值在单独的变量中.我可以用以下方式写出整个表达式(对于三个观察/我):
nl (ln_wage = {alpha0} + {alpha0}*log( ((S_over_H_1)^{alpha2})*exp({alpha3}*distance_1) + ((S_over_H_2)^{alpha2})*exp({alpha3}*distance_2) + ((S_over_H_1)^{alpha2})*exp({alpha3}*distance_1) ))
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以告诉Stata对给定数字集的表达式/变量求和,比如在GAMS中你可以写:
lnwage(i) = alpha0 + alpha1*ln(sum((j), power(S_over_H(i,j),alpha2) * exp(alpha3 * distance(i,j))))
Run Code Online (Sandbox Code Playgroud) 我有两个向量:
y <- c(0.044924, 0.00564, 0.003848, 0.002385, 0.001448, 0.001138,
0.001025, 0.000983, 0.00079, 0.000765, 0.000721, 0.00061, 0.000606,
0.000699, 0.000883, 0.001069, 0.001226, 0.001433, 0.00162, 0.001685,
0.001604, 0.001674, 0.001706, 0.001683, 0.001505, 0.001497, 0.001416,
0.001449, 0.001494, 0.001544, 0.00142, 0.001458, 0.001544, 0.001279,
0.00159, 0.001756, 0.001749, 0.001909, 0.001885, 0.002063, 0.002265,
0.002137, 0.002391, 0.002619, 0.002733, 0.002957, 0.003244, 0.003407,
0.003563, 0.003889, 0.004312, 0.004459, 0.004946, 0.005248, 0.005302,
0.00574, 0.006141, 0.006977, 0.007386, 0.007843, 0.008473, 0.008949,
0.010164, 0.010625, 0.011279, 0.01191, 0.012762, 0.014539, 0.01477)
x <- 0:68
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用非线性最小二乘函数来拟合数据,但我不断收到错误消息:
nlsModel(formula, mf, start, wts) …
借用这个问题的示例数据,如果我有以下数据并且我将以下非线性模型拟合到它,我如何计算我的曲线的95%预测 区间?
library(broom)
library(tidyverse)
x <- seq(0, 4, 0.1)
y1 <- (x * 2 / (0.2 + x))
y <- y1 + rnorm(length(y1), 0, 0.2)
d <- data.frame(x, y)
mymodel <- nls(y ~ v * x / (k + x),
start = list(v = 1.9, k = 0.19),
data = d)
mymodel_aug <- augment(mymodel)
ggplot(mymodel_aug, aes(x, y)) +
geom_point() +
geom_line(aes(y = .fitted), color = "red") +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)
举个例子,我可以轻松地从线性模型计算预测区间,如下所示:
## linear example
d2 <- d %>%
filter(x > …Run Code Online (Sandbox Code Playgroud) 在RI中使用nls进行非线性最小二乘拟合.那么我如何使用拟合提供的系数值绘制模型函数?
(是的,这是R相对新手的一个非常天真的问题.)
我目前被分配了一项工作,我需要将 SAS 代码翻译成 R。我已经成功完成了 80%,现在我被困在使用 PROC NLIN 的部分。从我读到的,PROC NLIN 用于拟合非线性模型,我不确定代码是否真的这样做,因此,坚持如何在 R 中做到这一点。代码如下 -
proc nlin data=ds1 outest=estout;
parms ET= 0 f= 10.68;
E= f- R*(1-ET*M);
L = E*E;
model.like = sqrt(E*E);
by Name ;
run;
Run Code Online (Sandbox Code Playgroud)
样本数据如下——
Name M R
Anna 0.5456231 4.118197
Anna 0.5359164 4.240243
Anna 0.541881 3.943975
Anna 0.5436047 3.822222
Anna 0.5522962 3.58813
Anna 0.5561487 3.513195
Anna 0.5423374 3.666507
Anna 0.525836 3.715371
Anna 0.5209941 3.805572
Anna 0.5304675 3.750689
Anna 0.5232541 3.788292
Run Code Online (Sandbox Code Playgroud)
当我浏览 SAS 帮助中 PROC NLIN 的页面时,参数“模型”用于指定方程,但此处的代码没有模型方程。Model.like 是指定似然函数(第 4316 …
因为我想分别使用nls模型,所以我对geom_smooth函数和ggplot外部的数据做了拟合:
library(ggplot2)
set.seed(1)
data <- data.frame(x=rnorm(100))
a <- 4
b <- -2
data$y <- with(data, exp(a + b * x) + rnorm(100) + 100)
mod <- nls(formula = y ~ (exp(a + b * x)), data = data, start = list(a = a, b = b))
data$fit <- predict(mod, newdata=data)
plot <- ggplot(data, aes(x=x, y=y)) +
geom_point() +
geom_smooth(method = "nls", colour = "red", formula=y ~ exp(a + b * x),
method.args = list(start = c(a = a, b = …Run Code Online (Sandbox Code Playgroud) 我nlsLM {minpack.lm}用来找到最适合数据集的参数a和b函数的值myfun,mydata.
mydata=data.frame(x=c(0,5,9,13,17,20),y = c(0,11,20,29,38,45))
myfun=function(a,b,r,t){
prd=a*b*(1-exp(-b*r*t))
return(prd)
}
Run Code Online (Sandbox Code Playgroud)
和使用 nlsLM
myfit=nlsLM(y~myfun(a,b,r=2,t=x),data=mydata,start=list(a=2000,b=0.05),
lower = c(1000,0), upper = c(3000,1))
Run Code Online (Sandbox Code Playgroud)
有用.但是现在我想引入一个约束条件a*b<1000.我查看了可nlsLM用于设置约束的选项nls.lm.control.但它没有多大帮助.有人可以帮助我或建议一个不同的方法吗?
我正在尝试绘制我的值并使用 nls 模型将它们与曲线拟合。但我收到一条错误消息,说我的变量没有起始值。
conc <- c(1.83, 3.66, 7.32, 14.65, 29.30, 58.59, 117.19, 468.75, 937.5, 1875, 3750)
avg <- c(0.02, 0.03, 0.05, 0.09, 0.23, 0.40, 0.60, 0.79, 0.98, 0.82, 1)
DataSet <- data.frame(conc, avg)
ggplot(DataSet, aes(x = conc, y = avg)) +
geom_point() +
scale_x_log10() +
stat_smooth(aes(x=conc, y = avg), method = "nls",
formula = "avg~Emax*(conc^Hill)/((EC50^Hill)+(conc^Hill))",
method.args=list(start=c(Emax = 1, EC50 = 100, Hill = 2)),
se = FALSE)
# Warning message:
# Computation failed in `stat_smooth()`:
# parameters without starting value …Run Code Online (Sandbox Code Playgroud)