1

I am new to R and am trying to run a regression analysis. I have constructed arbitrary vectors with the c() function to learn the plot, lm, fit, abline, and summary functions. That has worked properly, but when trying to regress imported data, I receive the following error message. I don't know what's causing the error or how to fix it. Any thought? Thanks.

library(xlsx)  
Loading required package: xlsxjars   
Loading required package: rJava    
x <- "~/Desktop/x.xlsx"    
y <- "~/Desktop/y.xlsx"   
X <- read.xlsx(x,1)  
Y <- read.xlsx(y,1)  
dim(X)  
[1] 149   1  
dim(Y)  
[1] 149   1  
plot(X,Y)  
Error in stripchart.default(x1, ...) : invalid plotting method  
plot(X)  
plot(Y)

Also, I don't think I understand all of the arguments accepted in the read.xlsx function. For example, if sheetindex is meant to index the sheets, wouldn't, in this example, x be 1 and y be 2? But then:

X <- read.xlsx(x,1)  
Y <- read.xlsx(y,2)   
Error in sheets[[sheetIndex]] : subscript out of bounds

Furthermore, the dimension is incorrect. The .xlsx file has 1 column, 150 rows, and no header.

dim(X)   
[1] 149   1

When converting to a .csv file, which I don't particularly want to do b/c of the total number of .xlsx file I have, I still have the same plotting error, however the dimension seems to be correct. In this example, the number of rows and columns remain the same at 1 and 150 respectively, but there is a header.

x <- "~/Desktop/x.csv"   
y <- "~/Desktop/y.csv"   
X <- read.table(x, header = T)    
Y <- read.table(y, header = T)    
plot(X,Y)    
Error in stripchart.default(x1, ...) : invalid plotting method    
dim(X)   
[1] 150   1   
nograpes
  • 18,623
  • 1
  • 44
  • 67
Chris S.
  • 11
  • 1
  • Please make your situation reproducible, i.e. provide us with the data and the code needed to mimic your situation. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more tips on how to do this. – EDi Mar 12 '13 at 14:02
  • > Furthermore, the dimension is incorrect. The .xlsx file has 1 column, 150 rows, and no header.< Header is by default TRUE, so you should specify this in your read.xlsx call. – EDi Mar 12 '13 at 14:03

2 Answers2

3

The problem is that X and Y are objects called data frames (?data.frame for details) and not vectors. the plot function is actually a wrapper around a family of other object-specific plot functions and in this case is trying to plot using stripchart(), which is causing the problem. This reproduces the problem, and fixes it:

X=data.frame(x=1:100)
Y=data.frame(y=rnorm(100,mean=1:100,sd=5))
plot(X,Y)
names(X)
names(Y)
plot(X$x, Y$y)

Assuming your data always consist of just one column, you could fix your code above by converting from a data.frame into an object of type numeric (i.e., the same sort of object as X=c(1,2,3,5)) which can be done a variety of ways

X <- unlist(read.xlsx(x,1))
Y <- unlist(read.xlsx(y,1))

or, alternatively, it's better to just havie read.xlsx() return a list instead of a data.frame

X <- read.xlsx(x,1, as.data.frame=FALSE)
Y <- read.xlsx(y,1, as.data.frame=FALSE)

Or you can just access the first column of the data.frame when you call plot

plot(X[,1], Y[,1])

See the help files for what all these functions return (e.g. ?as.numeric, ?unlist, ?names, etc.) and also see ?class, ?mode and ?typeof for querying object properties.

Paul J Hurtado
  • 572
  • 1
  • 4
  • 12
1

Furthermore, the dimension is incorrect. The .xlsx file has 1 column, 150 rows, and no header.

Header is by default TRUE, so you should specify this in your read.xlsx call.

X <- read.xlsx(x,1, header = TRUE)

Regarding the plot error:

plot(X,Y)  
Error in stripchart.default(x1, ...) : invalid plotting method 

read.xlsx returns data.frames, thats why the error shows up. Here is an example:

X <- data.frame(rnorm(150))
Y <- data.frame(rnorm(150))
plot(X, Y)

# Error in stripchart.default(x1, ...) : invalid plotting method

Please read carefully the read.xlsx documentation and about R object types.

EDi
  • 13,160
  • 2
  • 48
  • 57
  • 1
    Solution: OP should convert data frames to vectors by `X <- unlist(X)` before using the plot function. – Blue Magister Mar 12 '13 at 14:16
  • Thanks for the responses. I'm still working through and compiling my follow-up questions. The regression seems to be running properly now. – Chris S. Mar 14 '13 at 14:49