Perform regression from CSV file in R - r

I am new to R and want to perform a linear regression from the data in a CSV file as follows:
Data = read.csv("ErrorTest.csv",header=T, row.names=NULL)
regmodel=lm(Error ~ Const, data = Data)
However, I am getting the error message:
"Error in eval(expr, envir, enclos) : object 'Error' not found"
The CSV file is uploaded here: http://www.filedropper.com/errortest
Can someone please explain how to do the regression in R correctly?

The first few lines of your csv file look like this:
Error;Const
-0,44;1
-0,58;1
1,10;1
The read.csvassumes the use of , as separator between data and . as decimal point character. Meanwhile, judging from your data above, this is not the case. Therefore, you must modify the code used to read in the csv file. After that, you can run the regression model.
Data <- read.csv("ErrorTest.csv", sep = ";", dec = ",")
regmodel <- lm(Error ~ Const, data = Data)
EDIT: An even simpler way of reading in the data is using the built-in function read.csv2:
read.csv2("Errortest.csv")

Related

Error in is.factor(x) : object 'x' not found when 'x' is in original file

I am a new learner in R and am trying to create a ANOVA analysis by getting data from a .csv file, my initial code are as below:-
aovdata <- read.table ("Ritalin-by-condition.csv", header = T)
attach (aovdata)
Treatment <- as.factor(Treatment) ; Condition <- as.factor(Condition)
The console then prompt the below answer:-
Error in is.factor(x) : object 'Treatment' not found
I have checked the .csv file which contains the category 'Treatment' and 'Condition' .
Can you please let me know what I have done wrong? Many thanks!

My dataframe appears to have column names, but when I attempt a lm() function and name columns r says 'object not found'

I'm still very new to R, I have no other coding experience, and I don't understand some of the fundamentals, so please bear with me.
I'm trying to do a multiple regression on the data set found at:
https://studysites.sagepub.com/dsur/study/DSUR%20Data%20Files/Chapter%207/ChildAggression.dat
The website's answers don't mention any transformation of the data, but suggest one could just go ahead with the lm() function.
aggro <- read.delim("ChildAggression.dat", header = TRUE)
aggro.reg1 <- lm(Aggression ~ Parenting_Style + Sibling_Aggression, data = aggro)
Error in eval(predvars, data, env) : object 'Aggression' not found
I don't understand why it isn't finding the object.
Any help is much appreciated.
The default separator for read.delim is \t, but the file isn't tab separated. You want sep = "" instead.
Having read in the file as you did:
aggro <- read.delim("ChildAggression.dat", header = TRUE)
there are numerous ways to detect that something is wrong:
> dim(aggro) #number of columns is clearly wrong
[1] 666 1
> names(aggro) #only one long concatenated column name
[1] "Aggression.Television.Computer_Games.Sibling_Aggression.Diet.Parenting_Style"
> colnames(aggro) #only one long concatenated column name
[1] "Aggression.Television.Computer_Games.Sibling_Aggression.Diet.Parenting_Style"

Write col names while writing csv files in R

What is the proper way to append col names to the header of csv table which is generated by write.table command?
For example write.table(x, file, col.names= c("ABC","ERF")) throws error saying invalid col.names specification.Is there way to get around the error, while maintaining the function header of write.table.
Edit:
I am in the middle of writing large code, so exact data replication is not possible - however, this is what I have done:
write.table(paste("A","B"), file="AB.csv", col.names=c("A1","B1")) , I am still getting this error Error in write.table(paste("A","B"), file="AB.csv", col.names=c("A", : invalid 'col.names' specification.
Is that what you expect, tried my end
df <- data.frame(condition_1sec=1)
df1 <- data.frame(susp=0)
write.table(c(df,df1),file="table.csv",col.names = c("A","B"),sep = ",",row.names = F)

Print ncvTest to csv file

In R i run a ncvTest for heteroscedasticity. But i can't seem to print the result into a csv file. This is what i have done,
ncvt<-ncvTest(pol_reg)
outss<-file(paste0("hetero_test.csv"))
write.csv(ncvt,outss)
I get the following error message,
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""chisqTest"" to a data.frame
What am I suppose to do in order to save the result into a csv file. The result of ncvt looks as follows,
Non-constant Variance Score Test
Variance formula: ~ fitted.values
Chisquare = 75514.06 Df = 1 p = 0
You can pull out components of your ncvt list and make a dataframe out of it to write to a csv file:
ncvt<-ncvTest(pol_reg)
ds_ncvt <- data.frame(ncvt$formula.name, ncvt$ChiSquare, ncvt$Df, ncvt$p, ncvt$test)
outss<-file(paste0("hetero_test.csv"))
write.csv(ds_ncvt,outss)
Thanks, the following also works
ncvt<-ncvTest(pol_reg)
ds_ncvt <- as.matrix(ncvt)
outss<-file(paste0("hetero_test.csv"))
write.csv(ds_ncvt,outss)

Error importing SPSS data into R

I imported a dataset in the .sav SPSS format, and I'm getting an error that I haven't seen before.
1: In read.spss("C:\\Users\\acer\\Desktop\\X\\X\\PIREDEU\\ees2009_v0.9_20110622.sav", ... :
C:\Users\acer\Desktop\X\X\PIREDEU\ees2009_v0.9_20110622.sav: File contains duplicate label for value 1.1 for variable V200
Error in cat(list(...), file, sep, fill, labels, append) :
argument 2 (type 'list') cannot be handled by 'cat'
This came up after I typed warnings(PIREDEU). I imported the data using the foreign library:
library(foreign)
PIREDEU<-read.spss("C:\\Users\\acer\\Desktop\\X\\X\\PIREDEU\\ees2009_v0.9_20110622.sav", use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
I've fiddled with various combinations for the latter three arguments of the read.spss function, and I've gotten nowhere.
Anyone have any suggestions?
I used the below one and it worked perfectly, just ignore the warning message and check data by typing its name:
mydata4<-read.spss("C:\\Work\\data.sav",use.value.labels=F,to.data.frame=T)
mydata4 # check data
Do you have long strings in the file - longer than 8 bytes? Statistics uses some special arrangements to handle those. It looks like the problem is with the value labels. If you can delete those (using SPSS) you might be able to get the rest of the data.
Try to read data without labels.
library(foreign)
PIREDEU <- read.spss("C:\\Users\\acer\\Desktop\\X\\X\\PIREDEU\\ees2009_v0.9_20110622.sav",
use.value.labels = F,
to.data.frame = T)
Does it work?
Convert the spss datafile into .por (portable file) and in R, install the packages hMisc, memisc and foreign and load the package using library(foreign), library(hMisc) and library(memisc).
Then type the following:
mydata <- spss.get("c:/mydata.por", use.value.labels=TRUE)
# last option converts value labels to R factors

Resources