Non-Linear Regression Analysis- New R User

I am getting the following error:

Error in eval(predvars, data, env) : object 'crimes' not found

here is my code:
model <- lm(crimes ~ treatment + runningvariable + runningvariable_sq + runningvariable_cube,

        temperature = subset(temperature, abs(runningvariable) <= 10))

"crimes" is a column in my dataset

If you're after nonlinear regression, you should use the nls(...) procedure. The lm(...) procedure is for linear regression only.

It was hard to tell what the structure of you data set looks like, but in the nls call, make sure it includes 'data = nameofdataframe). Also you'll need a function like 'crimes~treatment + runningvariable + ...' and a 'start; list for the parameters you're trying to estimate.

All this said it looks like your model is actually a linear regression problem that is in the form

y ~ b0 + b1x + b2x^2 + b3*x^3

where you have inputs of x, x^2, and x^3 in your data frame. From the error message, it's possible your data subset excludes 'crime'. A more complete listing of the function and outputs, including the first few lines of the dataset would clarify these issues.

FYI, this might not be the right forum for this issue. This is a forum for POSIT/Quarto/RMarkdown and is mainly about publishing hybrid documents that include r code, text, tables, figures in either html or pdf format. I'd suggest [r-help@r-project.org] for r programming questions.

leave out the temperature = and it should work

lm(mpg ~ wt + disp, subset(mtcars, cyl == 4))
#> 
#> Call:
#> lm(formula = mpg ~ wt + disp, data = subset(mtcars, cyl == 4))
#> 
#> Coefficients:
#> (Intercept)           wt         disp  
#>     41.1350      -0.6978      -0.1225
lm(mpg ~ wt + disp, mtcars = subset(mtcars, cyl == 4))
#> Error in eval(predvars, data, env): object 'mpg' not found

Created on 2023-12-04 with reprex v2.0.2

I tried the nls and received this error: Error in eval(formula[[2L]], data, env) : object 'crimes' not found

model <- nls(crimes ~ treatment + runningvariable + runningvariable_sq + runningvariable_cube,

        temperature = subset(temperature, abs(runningvariable) <= 10,))

this is the data "temperature"

that did work, however I'm not sure it is non-linear now as per the comment above.

model <- lm(crimes ~ treatment + runningvariable + runningvariable_sq + runningvariable_cube,

        subset(temperature, abs(runningvariable) <= 10,))

"Linear" and "Nonlinear" regression get used in two different ways. Properly, a linear regression is one that is linear in its parameters, even if the functions the parameters multiply are nonlinear. So what you have is a linear regression that is nonlinear in the runningvariable.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.