Hello, I am a novice in coding in R and have come across an error I am having a hard time fixing. I am trying to use the mblm package to run a Theil-Sen linear model. The code for this function is: mblm(Y ~ X, dataframe, repeated = FALSE) My goal is to put this into a loop so that I can calculate the Theil-Sen slope of each column in my csv. file. The file contains one column of years (x value) and 3 columns of days of the year (y values). All columns are the same length. The code I currently have is: read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") attach(mydata) install.packages("mblm") library("mblm") for (i in c(1:3)){ x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE) print(x) } Which gives me the following error: Error in names(res$residuals) = as.character(1:length(res$residuals)) : 'names' attribute [2] must be the same length as the vector [0] Which I cannot seem to solve although as I understand it it is an error that I am causing in the mblm package. If anyone has any insight into how I could start fixing this that would be greatly appreciated! Bailey [[alternative HTML version deleted]]
Hi Bailey, Treat it as a guess, but try this: for (i in c(1:3)){ y<-mydata[,i] x <- mblm(y ~ Year, mydata, repeated = FALSE) print(x) } I'm not sure that you can mix indexed columns with column names. Also, Year is column 4, no? Jim On Sun, Sep 4, 2016 at 11:43 AM, Bailey Hewitt <bailster at hotmail.com> wrote:> Hello, > > > I am a novice in coding in R and have come across an error I am having a hard time fixing. I am trying to use the mblm package to run a Theil-Sen linear model. The code for this function is: > > mblm(Y ~ X, dataframe, repeated = FALSE) > > My goal is to put this into a loop so that I can calculate the Theil-Sen slope of each column in my csv. file. The file contains one column of years (x value) and 3 columns of days of the year (y values). All columns are the same length. The code I currently have is: > > > read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") > > mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") > > > attach(mydata) > > > install.packages("mblm") > > library("mblm") > > > for (i in c(1:3)){ > > x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE) > > print(x) > > } > > > Which gives me the following error: > > Error in names(res$residuals) = as.character(1:length(res$residuals)) : > > 'names' attribute [2] must be the same length as the vector [0] > > > Which I cannot seem to solve although as I understand it it is an error that I am causing in the mblm package. If anyone has any insight into how I could start fixing this that would be greatly appreciated! > > > Bailey > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Jim, Thank you for the suggestion. Unfortunately, when I tried this it gave me the same error as I was getting before. I was wondering the same thing as you, because of the way my function is set up is it even possible to iterate through columns in that type of function? And my Year column is the first column in my csv file, which I thought made it column 0. Am I mistaken, is it supposed to be column 1? Thanks! Bailey ________________________________ From: Jim Lemon <drjimlemon at gmail.com> Sent: September 4, 2016 4:55 AM To: Bailey Hewitt Cc: R-help Subject: Re: [R] Creating a loop with code from the mblm package Hi Bailey, Treat it as a guess, but try this: for (i in c(1:3)){ y<-mydata[,i] x <- mblm(y ~ Year, mydata, repeated = FALSE) print(x) } I'm not sure that you can mix indexed columns with column names. Also, Year is column 4, no? Jim On Sun, Sep 4, 2016 at 11:43 AM, Bailey Hewitt <bailster at hotmail.com> wrote:> Hello, > > > I am a novice in coding in R and have come across an error I am having a hard time fixing. I am trying to use the mblm package to run a Theil-Sen linear model. The code for this function is: > > mblm(Y ~ X, dataframe, repeated = FALSE) > > My goal is to put this into a loop so that I can calculate the Theil-Sen slope of each column in my csv. file. The file contains one column of years (x value) and 3 columns of days of the year (y values). All columns are the same length. The code I currently have is: > > > read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") > > mydata= read.csv("~/Documents/NH- Lake Mendota_SenSlope_Data2.csv", header = TRUE, sep = ",") > > > attach(mydata) > > > install.packages("mblm") > > library("mblm") > > > for (i in c(1:3)){ > > x <- mblm(mydata[,i] ~ Year, mydata, repeated = FALSE) > > print(x) > > } > > > Which gives me the following error: > > Error in names(res$residuals) = as.character(1:length(res$residuals)) : > > 'names' attribute [2] must be the same length as the vector [0] > > > Which I cannot seem to solve although as I understand it it is an error that I am causing in the mblm package. If anyone has any insight into how I [[elided Hotmail spam]] > > > Bailey > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-helpz.ch/mailman/listinfo/r-help> stat.ethz.ch The main R mailing list, for announcements about the development of R and the availability of new code, questions and answers about problems and solutions using R ...> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]