在 R 中进行线性回归时出错

Error while doing Linear Regression in R

我试图在 R 中做一个简单的线性回归,但我一直收到错误。 这是我的代码:

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

我不断收到此错误: terms.formula(formula, data = data) 错误: 模型公式中的无效项

这里还有更多信息:

> str(Copy_of_BNIA_data_7_24_17)

$ 2012 -- 每 1,000 名居民的家庭暴力服务电话:数量 60.5 51.5 56... $ 2015--青少年怀孕率:num 126.3 73.9 69 ...

> dput(head(Copy_of_BNIA_data_7_24_17, 10))

结构(列表(2012 -- Domestic Violence Calls For Service per 1,000 Residents = c(60.5, 51.5, 56.6), "2015--青少年怀孕率"), row.names = c(NA, 10L), class = c("tbl_df", "tbl", "data.frame")

请告诉我问题出在哪里。谢谢!

示例数据:

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))

线性回归公式:

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

lm 公式中,您不应将单引号 ' 放在选项卡按钮上方的符号:`