[R] Creating a loop with code from the mblm package

Jim Lemon drjimlemon at gmail.com
Sun Sep 4 10:55:55 CEST 2016


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.



More information about the R-help mailing list