Error: cannot coerce class ‘"lm"’ to a data.frame

22 views Asked by At

Prior to running the code in question:

library(car);
library(compute.es);
library(effects); 
library(ggplot2);
library(multcomp); 
library(pastecs); 
library(effectsize)
Data_Assignment7 = read.csv("Desktop/Adv. Data/Data_Assignment7.csv")
View(Data_Assignment7)
Data_Assignment7$group<-gl(3,10, labels = c("High Dose", "Low Dose", "Placebo"))
Weight_lbs <- Data_Assignment7$outcome
ggplot(Data_Assignment7, aes(group, Weight_lbs)) +
stat_summary(fun = mean, geom = "point", fill = "Black", colour = "Purple") +
  stat_summary(fun.data = mean_se, geom = "errorbar",width = 0.1) + labs(x = "group", y = "Weight_lbs")+
  geom_jitter(data=Data_Assignment7, aes(x=group,y=Weight_lbs), width = 0.1, color = "Red",alpha = 0.8)

#Data Summary

by(Data_Assignment7$outcome, Data_Assignment7$group, stat.desc)

#Check for homogeneity of variance

leveneTest(Data_Assignment7$outcome, Data_Assignment7$group)

Keep getting ERROR MESSAGE when I run:**** Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame

Other methods used below:

  1. Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame
  2. Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame
  3. Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame
  4. Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame
  5. Lin_reg<-lm(Data_Assignment7$model$Weight_lbs ~ Data_Assignment7$model$group, data=Data_Assignment7) Error in as.data.frame.default(data) : cannot coerce class ‘"lm"’ to a data.frame

Please note that all of the above blocks of code were run in an RMarkdown file and and r-blocked respectively

Professor's example

install.packages("effectsize")
install.packages("compute.es")
install.packages("effects")
library(car);
library(compute.es);
library(effects); 
library(ggplot2);
library(multcomp); 
library(pastecs); 
library(effectsize)

#First ANOVA

viagradata = read.csv("Desktop/Adv. Data/Viagra_Data.csv")
viagradata$dose<-gl(3,5, labels = c("Placebo", "Low Dose", "High Dose"))
ggplot(viagradata, aes(dose, libido)) +
stat_summary(fun = mean, geom = "point", fill = "White", colour = "Black") +
  stat_summary(fun.data = mean_se, geom = "errorbar",width = 0.1) + labs(x = "Dose", y = "Libido")+
  geom_jitter(data=viagradata, aes(x=dose,y=libido), width = 0.1, color = "Red",alpha = 0.8)

#Get data summary

by(viagradata$libido, viagradata$dose, stat.desc)

#Check for homogeneity of variance

leveneTest(viagradata$libido, viagradata$dose)

What I am EXPECTING, see below #Run regression - take a look at the F-value!

viagraModel<-lm(libido~dose, data = viagradata)
summary(viagraModel)

Call: lm(formula = libido ~ dose, data = viagradata)

Residuals: Min 1Q Median 3Q Max -2.0 -1.2 -0.2 0.9 2.0

Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 2.2000 0.6272 3.508 0.00432 ** doseLow Dose 1.0000 0.8869 1.127 0.28158 doseHigh Dose 2.8000 0.8869 3.157 0.00827 **

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.402 on 12 degrees of freedom Multiple R-squared: 0.4604, Adjusted R-squared: 0.3704 F-statistic: 5.119 on 2 and 12 DF, p-value: 0.02469

0

There are 0 answers