R 模型拟合可以有常数吗?

Can R Model Fits Have A Constant?

我正在尝试向我的模型添加一个常量

 exponential <- lm((log(fallpercent$n_activities)~ 
 ((fallpercent$percentabovebelow1 )+3)) 

这会产生错误

Error in terms.formula(formula, data = data) : 
  invalid model formula in ExtractVars. 

我正在尝试创建一个等价于 y = ex+3.

的函数

关于如何做的想法?

I am trying to create a function with the equivalent of y= e^(x+3) Ideas on how to do so?

您只是想创建这个函数吗?

myFunc <- function(x) {
  exp(x + 3)
}

myFunc(2)
[1] 148.4132

如果您愿意,可以在 lm 中修复拦截。我相信以下内容可能对您有所帮助。我使用虹膜作为示例数据。

首先拦截。

fit <- lm(log(Sepal.Length) ~ Sepal.Width, data = iris)
fit

#Call:
#lm(formula = log(Sepal.Length) ~ Sepal.Width, data = iris)

#Coefficients:
#(Intercept)  Sepal.Width  
#    1.88199     -0.04141  

现在没有

fit <- lm(log(Sepal.Length) - 1.88199 ~  0 + Sepal.Width, data = iris)
fit

#Call:
#lm(formula = log(Sepal.Length) - 1.88199 ~ 0 + Sepal.Width, data = iris)

#Coefficients:
#Sepal.Width  
   -0.04141 

这里1.88199是你的3等