lm() syntax with multiple variables and "spaces in variable names"

I am using lm() for a multivariate regression where it works when my variables are all names that are single words with _, but when I renamed my variables for prettier output, the code now gets an error that says

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

Here is my code

mod1 <- lm(new_cluster_connection ~ ("Latent Innovation, county" + 
    "Process Innovation, county" +
    "Product Innovation, county" +
    "Higher Education, county" +
    "High School Graduation, county" +
    "Safe Drinking Water Index, county" +
    "Perc Households with Broadband, county" +
    "StatsAmerica Connectivity Index, county" +
    "USDA Natural Amenities Scale, county" +
    "Perc Land Forest Service, county" + 
    "Perc Land National Parks, county" +
    "Perc Land Tribal, county" +
    "Perc Residents Worked In County, county" +
    "Average Commute in Minutes, county" +
  "Perc Hospital Stays Preventable, county" +
    "Obesity Level, county" +
    "Low Birth Weight, county" +
    "USDA Food Environment Index, county" +
    "Deaths Related to Drugs Rate, state" +
    "Labor Force Productivity, county" +
    "Manufacturing Intensity, county" +
    "Agriculture/GDP, county" +
    "Perc Strong Employment(CMP), county" +
    "Migration In / population, county" +
    "Migration Out / population, county" +
    "Perc of Inmigration from within EA, county" +
    "Student Teacher Ratio, state" +
    "Per Student Spending, state" +
    "Establishment Growth, county" +
    "Roads Disrepair, state" +
    "Bridges Disrepair, state") : (mostly_urban + mostly_rural + completely_rural), data=data)

Any ideas?

Non-syntactic variable names in R (such as those containing spaces, parentheses etc.) must be enclosed within backticks (``). Try that and let us know if you still face problems.

1 Like

Thanks! This solved it.

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