标签: non-linear-regression

R警告:newdata'有15行,但找到的变量有22行

我在这里读到的答案很少,但我恐怕无法找到答案.

我的R代码是:

colors <- bmw[bmw$Channel=="Colors" & bmw$Hour=20,]
colors_test <- tail(colors, 89)
colors_train <- head(colors, 810)

colors_train_agg <- aggregate(colors_train$Impressions, list(colors_train$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_train_agg) <- c("ad_position", "avg_impressions")
lm_colors <- lm(colors_train_agg$avg_impressions ~ poly(colors_train_agg$ad_position, 12))
 summary(lm_colors)

colors_test_agg <- aggregate(colors_test$Impressions, list(colors_test$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_test_agg) <- c("ad_position", "avg_impressions")
new.df <- data.frame(colors_test_agg$ad_position)
colnames(new.df) <- c("ad_position")
colors_test_test <- predict(lm_colors, newdata=new.df)
Run Code Online (Sandbox Code Playgroud)

所以我对训练和测试数据都有完全相同的列名.我仍然收到警告:

Warning message: 'newdata' had 15 rows but variables found have 22 rows

有人可以提出什么是错的吗?另外,我想知道我是否以正确的方式做到了.

此外,将非常感谢关于如何计算模型精度的一些指示.谢谢!

r prediction non-linear-regression

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

R:Optim() 拟合参数限制

我希望限制 optim() 的输出参数。对于我的正弦函数(其中“designL”是我的自变量,“ratio”是我的因变量数据,dfm 是我的数据帧),它不必要地收敛了数千个异相相位:

lo_0 = 2e-6
kc_0 = 80000
min.RSS <- function(data, par) {
  with(data, sum( (sin(par[2] *(par[1] + designL))^2 - ratio)^2) )
}
resultt <- optim(par = c(lo_0, kc_0), min.RSS, data = dfm)
Run Code Online (Sandbox Code Playgroud)

我想从 0:2e-5 限制 lo_0(相移)。我找到了一些关于此的文档,但它没有详细说明如何实现:https : //ubuntuforums.org/showthread.php?t=1420061

optimization r curve-fitting non-linear-regression

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

为什么 R 中的 nls(非线性模型)方程与 Excel 不同?

我想使用 nls 包检查非线性模型。

power<- nls(formula= agw~a*area^b, data=calibration_6, start=list(a=1, b=1))
summary(power)
Run Code Online (Sandbox Code Playgroud)

这是关于模型的参数。

在此处输入图片说明

它说 y= 0.85844 x^1.37629

但是,在 Excel 中(下图)。它说 y= 0.7553 x^1.419

在此处输入图片说明

如果我在 R 中绘制图形,则图形是相同的。为什么同一个模型产生不同的参数?

我需要更信任哪个方程式?你能回答我吗?

非常感谢。

ggplot(data=calibration_6, aes(x=area, y=agw)) + 
  geom_point (shape=19, color="cadetblue" ,size=2.5) +
  stat_smooth(method = 'nls', formula= y~a*x^b, start = list(a = 0, b=0), se=FALSE, color="Dark Red",level=0.95) +
  scale_x_continuous(breaks = seq(0,25,5),limits = c(0,25)) +
  scale_y_continuous(breaks = seq(0,80,10), limits = c(0,80)) +
  theme_bw() + 
  theme(panel.grid = element_blank())
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

parameters r nls non-linear-regression

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

为什么我的非线性回归曲线有直线段?

我尝试绘制一条非线性曲线,但我不知道为什么它有直线段。

原始数据如下:

ISIDOR <- structure(list(Pos_heliaphen = c("W30", "X41", "Y27", "Z24", 
                                       "Y27", "W30", "W30", "X41", "Y27", "W30", "X41", "Z40", "Z99"
), traitement = c("WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
                  "WW", "WW", "WW", "WW", "WW"), Variete = c("Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Cali"), FTSW_apres_arros = c(0.462837958498518, 
                                                                                                                         0.400045032939416, 0.352560790392534, 0.377856799586057, 0.170933345859364, 
                                                                                                                         0.315689846065931, 0.116825600914318, 0.0332444780173884, 0.00966070114456602, 
                                                                                                                         0.0871102539376406, 0.0107280083093036, 0.195548432729584, 1), 
NLE = c(0.903498791068124, 0.954670066942938, 0.970762905436272, 
        0.873838605282389, 0.647875257025359, 0.53056603773585, 0.0384548155916796, 
        0.0470924009989314, 0.00403163281128882, 0.193696514297641, 
        0.0718450645564359, 0.295346695941639, 1)), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 non-linear-regression

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

R中具有许多自变量(固定效应)的非线性模型

我正在尝试拟合具有近50 个变量的非线性模型(因为存在年份固定效应)。问题是我有太多的变量,我无法像这样写出完整的公式

nl_exp = as.formula(y ~ t1*year.matrix[,1] + t2*year.matrix[,2] 
                        +... +t45*year.matirx[,45] + g*(x^d))
nl_model =  gnls(nl_exp, start=list(t=0.5, g=0.01, d=0.1))
Run Code Online (Sandbox Code Playgroud)

其中y是二元响应变量,year.matirx是 45 列的矩阵(表示 45 个不同的年份),x是自变量。需要估计的参数是t1, t2, ..., t45, g, d

我有很好的起始值t1, ..., t45, g, d。但是我不想为这个非线性回归写一个很长的公式。

我知道如果模型是线性的,则表达式可以简化为

l_model = lm(y ~ factor(year) + ...)
Run Code Online (Sandbox Code Playgroud)
  1. factor(year)gnls函数中尝试过,但它不起作用。
  2. 此外,我也尝试过

    nl_exp2 = as.formula(y ~ t*year.matrix + g*(x^d))

    nl_model2 = gnls(nl_exp2, start=list(t=rep(0.2, 45), g=0.01, d=0.1))

它还会返回我的错误消息。

那么,有没有简单的方法来写下非线性公式和 中的起始值R

r random-effects non-linear-regression

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

为什么 nls() 在 R 中返回负标准差?

我正在尝试将数据建模为 R 中的正态曲线。我使用方程的形式:

在此输入图像描述

下面是方程参数计算的代码:

filtered_sp14 <- c(549.778714, 259.835892, 992.874178, 75.267324, 53.014376, 128.281700, 10.471976, 58.904862,
                   88.357293, 102.756260, 201.585529, 29.452431, 1130.973355, 198.967535, 233.001455, 106.028752,
                   962.112750, 75.921822, 858.047494, 82.466807, 198.967535, 70.685835, 68.722339, 130.899694,
                   52.359878, 41.233404, 53.014376, 187.186562, 10.471976, 26.179939, 39.269908, 7.853982,
                   26.179939, 47.123890, 311.541271, 157.734131, 111.919238, 53.014376, 392.699082, 58.904862,
                   294.524311, 141.371669, 52.359878, 114.537232, 15.707963, 47.123890, 147.262156, 23.561945,
                   16.755161, 26.179939)

filtered_osf <- c(22.885584, 24.905062, 8.816039, 6.877041, 8.782836, 5.853161, 4.088499, 11.792189, 17.533123, 7.043904,
                  9.189750, 11.216450, 22.821028, 27.308823, 10.652498, 18.590091, 5.973716, 4.387657, 5.091982, 5.973609,
                  5.012901, 22.547434, …
Run Code Online (Sandbox Code Playgroud)

r non-linear-regression

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