Running regression issues

rstudio

#1

Hi,

Here is my code:

fit <- lm(Total ~ AMT, data = df)

Could you advise why I am getting the following error?

Error: unexpected symbol in "fit <- lm(Total ~ AMT"

Thank you,


#2

Could you show the head of your df?


#3

Hi,

I figured out the issue which was NA's in my data set.

Also, a second issue I find is the fact that some of my variables have spaces in them. Is there a way to refer to those variables within the lm function?


#4

See the Google's R style guide:

https://google.github.io/styleguide/Rguide.xml

It is encouraged to use variable names without any space. Unless you can wrap the variable names in quote, spaces within variable names could cause issues. So my personal advice is to replace space with underscore.


#5

I agree with @Peter_Griffin that it's best to avoid spaces in your variable names to begin with! If for some reason you really need to use a variable with a space in lm, however, you can enclose the variable name in backticks:

mtcars_badnames <- mtcars 
names(mtcars_badnames)[3] <- "Engine Displacement"
lm(`Engine Displacement` ~ factor(cyl), data = mtcars_badnames)
#> 
#> Call:
#> lm(formula = `Engine Displacement` ~ factor(cyl), data = mtcars_badnames)
#> 
#> Coefficients:
#>  (Intercept)  factor(cyl)6  factor(cyl)8  
#>       105.14         78.18        247.96

Created on 2018-05-29 by the reprex package (v0.2.0).


#6

Got it! Thanks a lot!