0

I created a dataset "before_database" via read.table(). Afterwards this data frame was grouped by do.call() and stored into a object "fits":

fits<-do.call('rbind', by(before_database, before_database$Serial_number, function(before_database) broom::tidy(lm(Amplification ~ Voltage-1, data = before_database))))

Now I want to see some results out of this. Summary(fits) works:

> summary(fits)
     term              estimate        std.error         statistic        p.value         
 Length:54          Min.   :0.3601   Min.   :0.06611   Min.   :2.884   Min.   :3.000e-09  
 Class :character   1st Qu.:0.4943   1st Qu.:0.11113   1st Qu.:3.384   1st Qu.:4.344e-05  
 Mode  :character   Median :0.5866   Median :0.14816   Median :3.934   Median :2.015e-04  
                    Mean   :0.6030   Mean   :0.16049   Mean   :4.026   Mean   :8.918e-04  
                    3rd Qu.:0.7058   3rd Qu.:0.21271   3rd Qu.:4.318   3rd Qu.:1.199e-03  
                    Max.   :0.9193   Max.   :0.27495   Max.   :6.410   Max.   :5.291e-03  
> 

but plot(fits) not. I receive:

> plot(fits)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In data.matrix(x) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

Also coef(fits) seems not to work, I just get "NULL". What is the matter? What do I should go after?

edit: Added the summary info and I've a further question, maybe connected to the issue. I want to fit all the grouped data separately (am I doing this right now?) so by trying to plot it is not specified what shall be plotted exactly (which of the subsets)?

I guess my problem can be reduced to cirumstance that I do not know how I can apply regular r commands to the grouped data/subsets generated via do.call()? In the end I need a specific value out of the fit. I know fitted() can do this but how to do now for each group? E.g. I need the corresponding voltage of an amplification of 150 and the slope at this point.

Data (only a part shown): Data

Grouped & fitted by do.call() (only a part shown):

Grouped & fitted

user3554004
  • 1,044
  • 9
  • 24
Ben
  • 1,432
  • 4
  • 20
  • 43

1 Answers1

2

The second issue (from the edit) is not connected, so I'll focus on the initial one. You are getting the error, because you are trying to plot a character() class, which doesn't work. In general, it's not the best idea to try to call plot the whole dataframe anyway - you probably want to plot some specific columns against one another, right? plot is kind of smart though, so it tries to plot something usually. Here's a reporducible example:

plot(data.frame(letters[1:10], 1:10, stringsAsFactors = F))   # error
plot(data.frame(1:10, sample(1:10,10), stringsAsFactors = F)) # sees two columns, makes a scatterplot
plot(data.frame(1:10, sample(1:10,10), sample(1:10,10), stringsAsFactors = F)) # sees 2+ columns, makes a scatterplot matrix
plot(data.frame(letters[1:5], 1:10, stringsAsFactors = T)) # if using factors (not character), plot wrangles data into a boxplot (recycling values!)
boxplot(data.frame(runif(10), runif(10)*2, runif(10)^2 )) # what I suspect you were after

In short, it's better to figure out what you want to plot and then plot that.

Edit to reply to comment/edit. To subset, you could either just limit the plot boundaries

plot(sample(1:100, 100), ylim=c(0,50), xlim=c(2,10))

or do a subset using logic operators, e.g.,

x = 1:10
x2 = x[which(x>5 & x < 10)]
plot(x2)
user3554004
  • 1,044
  • 9
  • 24
  • Thank you! Not exactly what I need now but instead later anyhow :) My goal is to plot an arbitrary fitted grouped/subset I created via do.call(). Or mainly: How do I apply regular r commands to subsets generated via do.call()? – Ben Jul 04 '17 at 13:41
  • 1
    @Ben Figure out what you mean by "plot" (boxplot, scatterplot, barplot, wordcloud...), and reword the question accordingly. Also, do.call doesn't make any special objects (check with `class()`). – user3554004 Jul 04 '17 at 13:44
  • At first thanks again!. In case of the data mtcars when I do lmfit<-lm(mpq ~ wt, mtcars) and then plot(wt, mpg, ..) I get a "plot" of wt against mpg. So this is what I would like to do for a group I have now. And then calculate some values out of this (group-wise and also whole frame-wise). Will have a look at class(). – Ben Jul 04 '17 at 13:48
  • @Ben If my answer answered your initial question though, you might want to mark it as accepted, and then flesh out your other question(s) by asking a separate question. – user3554004 Jul 04 '17 at 14:02
  • Just almost. How can I limit plot to a subset of the data frame? There are a lot of devices in it and each with a lot of data points (Amplification against voltage). But I only want to plot one device. I fitted the data with lm and grouped it by by(). I would like to see a plot with the data points and resulting lm fit (for e.g. one device). – Ben Jul 04 '17 at 14:18
  • I found it on https://stackoverflow.com/questions/8293547/how-to-plot-a-subset-of-a-data-frame-in-r By typing > with(before_database[(before_database$Voltage < 400) & (before_database$Amplification < 200),], plot(Voltage, Amplification)) I receive obviously all data with these requirements. How can I restrict the data to only one single device, e.q., SN = 819008650? – Ben Jul 04 '17 at 14:29
  • I found something better: xyplot(Amplification ~ Voltage | Serial_number, data=before) . But still I cannot plot only a specific serial number. How do I restrict this? – Ben Jul 04 '17 at 15:03
  • 1
    @Ben execute the following R command: `?"=="` and use the suitable operator. – user3554004 Jul 04 '17 at 15:05
  • Yeah, we're very close :) Thank you! Now two things left: Why is there a FALSE and TRUE plot? In both are different data. And how do I know that it is really the demanded serial number? Can I plot the serial number somewhere into the plot or on top of it? edit: For other, it is (in my case with the device with the serial number 912009913): xyplot(Amplification ~ Voltage | Serial_number==912009913, data=before) – Ben Jul 04 '17 at 15:09
  • @Ben these are completely separate questions; the comments section on this site is not meant for these, nor for extended discussion or Q&A session. I'd recommend accepting the answer to your current (already extended question), and asking a separate question to address your other issues (but they are all very basic, so before opening a new question, do a quick search on google/stackoverflow - it will give you numerous solutions to pick from) – user3554004 Jul 04 '17 at 15:12