subrinarafiq
subrinarafiq

Reputation: 89

abline only using two coefficients

Data Set

https://drive.google.com/file/d/1k_fVtpMKf9_4rUsKXkNQ9-it7hHzKs7U/view?usp=sharing

PHONE <- read.csv(file = "~/Desktop/311NY.csv")

Data311 = PHONE$count
Data311 = PHONE[,2]

Data311

plot(Data311, type="o", col="orange", xlab="Date", ylab="Calls", main="Time Series Analysis of 311 Calls")

abline(lm(Calls~Date, data=PHONE), col="black" )

Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :

plot.new has not been called yet

In addition: Warning message:

In abline(lm(Calls ~ Date, data = PHONE), col = "black") :

only using the first two of 365 regression coefficients

I keep receiving this error message proceeded by an empty graph. I've already tried the following to trouble shoot.

plot(Calls ~ Date, data =PHONE)
model <- lm(Calls ~ as.numeric(as.character(Date)), data=PHONE)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :

0 (non-NA) cases

In addition: Warning message:

In eval(predvars, data, env) : NAs introduced by coercion

Upvotes: 0

Views: 1267

Answers (1)

neilfws
neilfws

Reputation: 33782

Quite a lot of separate problems here.

First, Data311 <- PHONE$count won't work, because PHONE has no column named count. You want PHONE$Calls, if you don't want to use PHONE[, 2].

Second, plot.new has not been called yet means that your plot did not work for some reason. It's not clear why from the code that you have posted, which looks as though it should work provided Data311 exists.

Third, lm() is not working as expected because the Date column is of class "factor", not class "Date". So linear regression is treating each day as a factor, hence the message only using the first two of 365 regression coefficients.

So: assuming that you have PHONE, first convert Date to "Date":

PHONE$Date <- as.Date(PHONE$Date, "%m/%d/%y")

No reason why plot should not work:

plot(PHONE$Calls, 
     type = "o", 
     col = "orange", 
     xlab = "Date", 
     ylab = "Calls", 
     main = "Time Series Analysis of 311 Calls")

And your abline() should work if Date is now of type date. R will convert to numeric to do the regression, but you could also do that yourself.

But now the real question: why are you using linear regression here? Do you really expect a linear relationship where calls increase or decrease with time?

Upvotes: 1

Related Questions