0
votes

I am trying to do a simple linear regression in R and I keep getting an error. Here is my code:

> LinearMod <- lm('2015--teen pregnancy rates' ~ '2012 -- Domestic Violence Calls For Service per 1,000 Residents', data=Copy_of_BNIA_data_7_24_17)

I keep getting this error: Error in terms.formula(formula, data = data) : invalid term in model formula

Also here is some more info:

> str(Copy_of_BNIA_data_7_24_17)

$ 2012 -- Domestic Violence Calls For Service per 1,000 Residents : num 60.5 51.5 56... $ 2015--teen pregnancy rates : num 126.3 73.9 69 ...

> dput(head(Copy_of_BNIA_data_7_24_17, 10))

structure(list( 2012 -- Domestic Violence Calls For Service per 1,000 Residents = c(60.5, 51.5, 56.6), "2015--teen pregnancy rates"), row.names = c(NA, 10L), class = c("tbl_df", "tbl", "data.frame")

Please let me know what the issue is. Thank you!

1
Help us help you. Please give us some information about your data. To start with, please run both str(Copy_of_BNIA_data_7_24_17) and dput(head(Copy_of_BNIA_data_7_24_17, 10)) and paste the results into your question.G5W

1 Answers

1
votes

sample data:

library(data.table)

Copy_of_BNIA_data_7_24_17 <- data.table("2015--teen pregnancy rates" = c(0,1,4,5),
                 "2012 -- Domestic Violence Calls For Service per 1,000 Residents" = c(10,12,15,16))

Linear regression formula:

LinearMod <- lm(`2015--teen pregnancy rates` ~ `2012 -- Domestic Violence Calls For Service per 1,000 Residents`, data=Copy_of_BNIA_data_7_24_17)

In the lm formula you shouldn't put the single quote ' but the symbol above the tab button: `