[R] Error in linear regression

David Winsemius dwinsemius at comcast.net
Fri Dec 18 07:28:28 CET 2015


> On Dec 17, 2015, at 7:58 PM, Saba Sehrish <sabasehrish at yahoo.com> wrote:
> 
> Hi
> 
> Please find the attachment with (.txt) extension and I hope the command is visible now.
> 
> library(lmtest)

Why is this library loaded? I see no calls to functions from that package.

> data<-read.csv(file="---",header=T,sep=",")

The first two lines of that file ARE:

DODGX,TRMCX
"739,171,876.13","-30,023,111.44"

So R will not unpack the character values that appear to be inside the comma separated values. The first value will be "739,171,876.13" and the second one will be "-30,023,111.44"

I'm guessing this is not what you expected. Learn to examine your data before diving into the manipulation and analysis of it.


> A<-as.matrix(data$DODGX)
> B<-as.matrix(data$TRMCX)

Shouldn't the building of a matrix have dimensions?

> 
> nrow<-nrow(A)
> A1<-matrix(NA,nrow,1)
> A2<-matrix(NA,nrow,1)
> A3<-matrix(NA,nrow,1)
> A4<-matrix(NA,nrow,1)
> A5<-matrix(NA,nrow,1)

You should describe in English what you are trying to accomplish.

-- 
David

> A1[2:nrow,1]<-A[1:(nrow-1),1]
> A2[3:nrow,1]<-A[1:(nrow-2),1]
> A3[4:nrow,1]<-A[1:(nrow-3),1]
> A4[5:nrow,1]<-A[1:(nrow-4),1]
> A5[6:nrow,1]<-A[1:(nrow-5),1]
> nrow<-nrow(B)
> B1<-matrix(NA,nrow,1)
> B2<-matrix(NA,nrow,1)
> B3<-matrix(NA,nrow,1)
> B4<-matrix(NA,nrow,1)
> B5<-matrix(NA,nrow,1)
> B1[2:nrow,1]<-B[1:(nrow-1),1]
> B2[3:nrow,1]<-B[1:(nrow-2),1]
> B3[4:nrow,1]<-B[1:(nrow-3),1]
> B4[5:nrow,1]<-B[1:(nrow-4),1]
> B5[6:nrow,1]<-B[1:(nrow-5),1]
> 
> reg1<-lm(A~A1+A2+A3+A4+A5+B1+B2+B3+B4+B5)
> reg2<-lm(B~B1+B2+B3+B4+B5+A1+A2+A3+A4+A5)
> 
> 
> 
> Following error is occurring:
> 
> Error in lm.fit(x,y,offset = offset, singular.ok = singular.ok, ...) :
>  NA/NaN/lnf in 'y'
> In addition: Warning message:
> In model.response(mf,"numeric") : NAs introduced by coercion
> 
> Regards
> Saba
> 
> 
> 
> 
> On Friday, 18 December 2015, 15:11, David Winsemius <dwinsemius at comcast.net> wrote:
> 
> 
> 
> > On Dec 17, 2015, at 1:13 PM, Saba Sehrish via R-help <r-help at r-project.org> wrote:
> > 
> > Hi I am trying to apply linear regression on the attached data
> 
> The is no attached data; please read the posting guide. Do not post with .csv or .doc files. You can have commas as separators but an attachment must have a .txt extension.
> 
> 
> > of two variables (DODGX, TRMCX) in R by taking into account time lag=5 for both of them. Each time I run this command, it gives me following error:
> > Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :  NA/NaN/Inf in 'y'In addition: Warning message:In model.response(mf, "numeric") : NAs introduced by coercion
> > Following is the programming I am using:
> > 
> > data<-read.csv(file="---",header=T)A<-as.matrix(data$DODGX)B<-as.matrix(data$TRMCX)
> > nrow<-nrow(A)A1<-matrix(NA,nrow,1)A2<-matrix(NA,nrow,1)A3<-matrix(NA,nrow,1)A4<-matrix(NA,nrow,1)A5<-matrix(NA,nrow,1)A1[2:nrow,1]<-A[1:(nrow-1),1]A2[3:nrow,1]<-A[1:(nrow-2),1]A3[4:nrow,1]<-A[1:(nrow-3),1]A4[5:nrow,1]<-A[1:(nrow-4),1]A5[6:nrow,1]<-A[1:(nrow-5),1]nrow<-nrow(B)B1<-matrix(NA,nrow,1)B2<-matrix(NA,nrow,1)B3<-matrix(NA,nrow,1)B4<-matrix(NA,nrow,1)B5<-matrix(NA,nrow,1)B1[2:nrow,1]<-B[1:(nrow-1),1]B2[3:nrow,1]<-B[1:(nrow-2),1]B3[4:nrow,1]<-B[1:(nrow-3),1]B4[5:nrow,1]<-B[1:(nrow-4),1]B5[6:nrow,1]<-B[1:(nrow-5),1]
> > reg1<-lm(A~A1+A2+A3+A4+A5+B1+B2+B3+B4+B5)reg2<-lm(B~B1+B2+B3+B4+B5+A1+A2+A3+A4+A5)
> 
> 
> I do not see the usual html delted message but nonetheless your code has arrived without any linebreaks. Linebreaks are syntactically necessary. So pleas learn to post with plain text in a format that does mangle the ability of humans to read this code.
> 
> 
> -- 
> David Winsemius
> Alameda, CA, USA
> 
> 
> 
> <Monthly data.txt>

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list