0

I am a running a linear mixed model analyses using the nlme package in R.
I get the above error when I try to plot the results from my model using ggplot. I have looked at other posts on stackoverflow but cant seem to see what the error is here. PLease could someone help with the sample data below?

My code is as follows:

m1<-lme(vis_hits~Group*session+nbacklevel, random=~session|subjno,
              data = sampledata,method="ML",na.action=na.omit)
    summary(m1)

m1_plot<ggplot(sampledata,aes(session,vis_hits,colour=Group))+
           stat_summary(fun.data=mean_se,geom="pointrange")+
           stat_summary(aes(y=fitted(m1),linetype=Group),fun.y=mean,geom="line",size=1.5)+
           labs(y="Correctly detected nbacks")

A sample of my data is below

Subjno  Group   session nbacklevel  vis_hits
1   1   1   1   0.83
1   1   1   2   0.5
1   1   1   3   0.33
1   1   1   3   
1   1   2   1   0.67
1   1   2   2   0.5
1   1   2   3   0.17
1   1   2   2   0.17
1   1   2   2   0.5
1   1   2   2   0.33
1   1   2   2   0.33
1   1   2   2   0.5
1   1   2   2   0.33
1   1   2   2   
1   1   2   1   0.67
1   1   2   2   0.67
1   1   2   3   0.33
1   1   2   3   0.33
1   1   2   3   0.17
1   1   3   1   0.67
1   1   3   2   0.5
1   1   3   2   0.5
1   1   3   2   0.33
1   1   3   2   0.33
1   1   3   2   0.5
1   1   3   2   0.33
1   1   3   2   0.33
1   1   3   2   0.17
1   1   3   2   0.33
1   1   3   2   0.33
1   1   3   2   0.67
1   1   3   3   0.17
1   1   3   3   0.17
1   1   3   3   0.17
1   1   3   3   0.17
1   1   4   1   1
1   1   4   2   1
1   1   4   3   0.5
1   1   4   2   1
1   1   4   3   0.67
1   1   4   3   0.5
1   1   4   2   1
1   1   4   3   0.67
1   1   4   4   0.67
1   1   4   3   0.67
1   1   4   2   0.83
1   1   4   3   1
1   1   4   4   0.67
1   1   5   2   0.83
1   1   5   3   0.33
1   1   5   3   0.33
1   1   6   2   0.83
1   1   6   3   0.5
1   1   6   3   0.67
1   1   6   3   0.17
2   0   1   1   1
2   0   1   2   1
2   0   1   2   1
2   0   2   1   1
2   0   2   2   1
2   0   2   3   0.83
2   0   2   2   1
2   0   2   3   0.83
2   0   2   2   1
2   0   2   2   0.83
2   0   2   2   1
2   0   2   2   1
2   0   2   3   0.83
2   0   2   2   1
2   0   2   3   0.5
2   0   2   2   1
2   0   3   1   1
2   0   3   2   1
2   0   3   3   0.83
2   0   3   2   1
2   0   3   2   1
2   0   3   2   1
2   0   3   2   1
2   0   3   3   0.33
2   0   3   2   0.67
2   0   3   2   1
2   0   3   2   0.83
2   0   3   2   1
2   0   3   3   0.83
2   0   3   2   1
2   0   4   1   1
2   0   4   2   1
2   0   4   3   0.33
2   0   4   2   1
2   0   4   3   0.67
2   0   4   2   1
2   0   4   3   0.67
2   0   4   2   0.5
2   0   4   1   0.83
2   0   4   1   0.83
2   0   4   1   0.83
2   0   4   2   1
2   0   4   3   0.33
2   0   4   2   0.83
2   0   5   2   0.5
2   0   5   1   1
2   0   5   2   0.83
2   0   5   2   0.83
2   0   6   2   1
2   0   6   3   0.67
2   0   6   2   1
2   0   6   3   0.83
2   0   6   3   0.83
3   1   1   1   0.67
3   1   1   2   0.67
3   1   1   3   0.33
3   1   1   3   0.33
3   1   2   1   0.67
3   1   2   2   0.33
3   1   2   1   0.83
3   1   2   2   1
3   1   2   3   0.33
3   1   2   2   0.67
3   1   2   2   0.67
3   1   2   3   
3   1   2   2   0.5
3   1   2   2   0.5
3   1   2   2   0.67
3   1   2   2   0.67
3   1   2   2   1
3   1   2   3   0.5
3   1   2   2   0.83
3   1   3   1   0.83
3   1   3   2   0.67
3   1   3   2   0.67
3   1   3   3   0.17
3   1   3   2   1
3   1   3   3   0.5
3   1   3   3   0.67
3   1   3   3   0.5
3   1   3   2   0.83
3   1   3   3   0.33
3   1   3   2   1
3   1   3   3   0.67
3   1   3   3   0.67
3   1   3   3   0.5
3   1   3   3   0.33
3   1   4   1   0.83
3   1   4   2   0.67
3   1   4   3   0.5
3   1   4   2   1
3   1   4   3   0.33
3   1   4   3   0.67
3   1   4   3   0.83
3   1   4   4   0.33
3   1   4   3   0.33
3   1   4   2   0.5
3   1   4   2   1
3   1   4   3   0.33
3   1   4   3   0.17
3   1   4   2   1
3   1   4   3   0.33
3   1   5   2   1
3   1   5   3   0.67
3   1   5   3   0.33
3   1   6   2   0.17
3   1   6   1   1
3   1   6   2   0.83
3   1   6   3   0.83
3   1   6   4   0.17
3   1   6   4   0.5
4   0   1   1   1
4   0   1   2   0.5
4   0   1   2   0.5
4   0   2   1   1
4   0   2   2   0.5
4   0   2   2   0.5
4   0   2   2   1
4   0   2   3   0.5
4   0   2   3   0.67
4   0   2   3   0.33
4   0   2   3   0.33
4   0   2   3   0.5
4   0   2   3   0.17
4   0   2   3   0.67
4   0   2   3   0.33
4   0   2   3   0.67
4   0   3   1   1
4   0   3   2   1
4   0   3   3   0.5
4   0   3   3   0.5
4   0   3   2   0.67
4   0   3   2   0.67
4   0   3   2   0.67
4   0   3   2   0.67
4   0   3   2   0.67
4   0   3   2   0.33
4   0   3   2   0.5
4   0   3   2   0.17
4   0   3   3   0.33
4   0   4   1   1
4   0   4   2   0.67
4   0   4   2   0.67
4   0   4   3   0.33
4   0   4   3   0.5
4   0   4   2   0.5
4   0   4   2   0.5
4   0   4   2   0.83
4   0   4   3   0.17
4   0   4   2   0.5
4   0   4   2   0.33
4   0   4   1   1
4   0   4   2   0.5
4   0   4   2   1
4   0   5   1   1
4   0   5   2   1
4   0   5   3   0.33
4   0   5   2   0.5
4   0   6   2   0.5
4   0   6   2   0.5
4   0   6   2   0.5
4   0   6   3   
6   0   1   1   1
6   0   1   2   1
6   0   1   3   0.67
6   0   1   2   0.83
6   0   2   1   1
6   0   2   2   1
6   0   2   3   0.17
6   0   2   2   1
6   0   2   3   0.67
6   0   2   3   0.83
6   0   2   3   0.67
6   0   2   2   1
6   0   2   3   1
6   0   2   4   0.5
6   0   3   1   1
6   0   3   2   1
6   0   3   3   0.5
6   0   3   2   1
6   0   3   3   0.67
6   0   3   3   0.5
6   0   3   2   1
6   0   3   3   0.67
6   0   3   4   0.67
6   0   3   3   0.67
6   0   3   2   0.83
6   0   3   3   1
6   0   3   4   0.67
6   0   4   2   1
6   0   4   3   0.67
6   0   4   3   0.67
6   0   4   3   1
6   0   4   4   0.33
6   0   4   3   0.67
6   0   4   3   0.83
6   0   4   2   1
6   0   4   3   1
6   0   4   4   0.67
6   0   4   3   1
6   0   4   4   0.5
6   0   4   3   0.67
6   0   5   2   1
6   0   5   3   1
6   0   5   4   0.5
6   0   6   2   0.83
6   0   6   3   1
6   0   6   4   0.83
6   0   6   4   0.83
Dave2e
  • 22,192
  • 18
  • 42
  • 50
shrutigarg
  • 11
  • 3

1 Answers1

0

There were a couple of issues here.

The first correction was converting the "Group" column from a numeric column to a factor in order to specify the linetype. It is better to use the function dput() to post your sample data instead of posting a table. dput will preserve the original data types.

Next, the original data had four rows of missing data, I assumed these values were NA and I removed them from the dataset before attempting to plot the data. This was necessary to ensure the number of fitted parameters was to equal the number of rows of data.

m1<-lme(vis_hits~Group*session+nbacklevel, random=~session|Subjno,
        data = sampledata,method="ML",na.action=na.omit)
summary(m1)

#Remove incomplete rows of values
sampledata<-sampledata[complete.cases(sampledata),]

# Assuming "Group" is a factor and not numeric
m1_plot<-ggplot(sampledata, aes(session, vis_hits, colour=factor(Group)))+
  stat_summary(fun.data=mean_se, geom="pointrange")+
 stat_summary(aes(y=fitted(m1), linetype=factor(Group)), fun=mean, geom="line", size=1.5)+
  labs(y="Correctly detected nbacks")


m1_plot

Here is my resulting plot.

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50