Hi , I am working on a function . rm.outliers = function(dataset,model){ dataset$predicted = predict(model) dataset$stdres = rstudent(model) m = 1 for(i in 1:length(dataset$stdres)){ dataset$outlier_counter[i] = if(dataset$stdres[i] >= 3 | dataset$stdres[i] <= -3) {m} else{0} } j = length(which(dataset$outlier_counter >= 1)) while(j>=1){ print(dataset[which(dataset$outlier_counter >= 1),]) dataset = dataset[which(dataset$outlier_counter == 0),] dataset$predicted = predict(model) dataset$stdres = rstudent(model) m = m+1 for(k in 1:length(dataset$stdres)){ dataset$outlier_counter[k] = if(dataset$stdres[k] >= 3 | dataset$stdres[k] <= -3) {m} else{0} } j = length(which(dataset$outlier_counter >= 1)) } return(dataset) } When I pass rm.outliers(xsys,fitted.modely1.temp.l) fitted.modely1 .temp.l is mylinear model. It shows me error like Error in `$<-.data.frame`(`*tmp*`, "predicted", value = c(0.306726561735386, : replacement has 731 rows, data has 717 Called from: `$<-`(`*tmp*`, "predicted", value = c(0.306726561735386, 0.306726561) Help me out Xsys data has 331 rows and 18 column [[alternative HTML version deleted]]
Your problem is that you are subsetting the dataset on line 13 of the function, but then on the following line you attempt to add predicted values from the complete data used to fit the model. You'll have to subset that as well (and the same would be true for the following line where you attempt to add standardised residuals) David On 2 January 2015 at 12:58, Methekar, Pushpa (GE Transportation, Non-GE) < pushpa.methekar at ge.com> wrote:> Hi , > I am working on a function . > > rm.outliers = function(dataset,model){ > dataset$predicted = predict(model) > dataset$stdres = rstudent(model) > m = 1 > for(i in 1:length(dataset$stdres)){ > dataset$outlier_counter[i] = if(dataset$stdres[i] >= 3 | > dataset$stdres[i] <= -3) {m} > else{0} > } > j = length(which(dataset$outlier_counter >= 1)) > while(j>=1){ > print(dataset[which(dataset$outlier_counter >= 1),]) > dataset = dataset[which(dataset$outlier_counter == 0),] > dataset$predicted = predict(model) > dataset$stdres = rstudent(model) > m = m+1 > for(k in 1:length(dataset$stdres)){ > dataset$outlier_counter[k] = if(dataset$stdres[k] >= 3 | > dataset$stdres[k] <> -3) {m} else{0} > } > j = length(which(dataset$outlier_counter >= 1)) > } > return(dataset) > } > When I pass > rm.outliers(xsys,fitted.modely1.temp.l) > fitted.modely1 .temp.l is mylinear model. > It shows me error like > > Error in `$<-.data.frame`(`*tmp*`, "predicted", value > c(0.306726561735386, : > > replacement has 731 rows, data has 717 > > Called from: `$<-`(`*tmp*`, "predicted", value = c(0.306726561735386, > 0.306726561) > > > > Help me out > > > Xsys data has 331 rows and 18 column > > [[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. >[[alternative HTML version deleted]]
On Jan 2, 2015, at 4:58 AM, Methekar, Pushpa (GE Transportation, Non-GE) wrote:> Hi , > I am working on a function . > > rm.outliers = function(dataset,model){ > dataset$predicted = predict(model) > dataset$stdres = rstudent(model) > m = 1 > for(i in 1:length(dataset$stdres)){ > dataset$outlier_counter[i] = if(dataset$stdres[i] >= 3 | > dataset$stdres[i] <= -3) {m} > else{0} > } > j = length(which(dataset$outlier_counter >= 1)) > while(j>=1){ > print(dataset[which(dataset$outlier_counter >= 1),]) > dataset = dataset[which(dataset$outlier_counter == 0),] > dataset$predicted = predict(model) > dataset$stdres = rstudent(model) > m = m+1 > for(k in 1:length(dataset$stdres)){ > dataset$outlier_counter[k] = if(dataset$stdres[k] >= 3 | > dataset$stdres[k] <= -3) {m} else{0} > } > j = length(which(dataset$outlier_counter >= 1)) > } > return(dataset) > } > When I pass > rm.outliers(xsys,fitted.modely1.temp.l) > fitted.modely1 .temp.l is mylinear model. > It shows me error like > > Error in `$<-.data.frame`(`*tmp*`, "predicted", value = c(0.306726561735386, : > > replacement has 731 rows, data has 717When you get a mismatch of "data" and replacement lengths like that it suggests you have NA values in some of the model variables. If that's the case then the absence of an example means we are not be able to demonstrate that effect, but you should be able to make a more modest example and test that hypothesis.> > Called from: `$<-`(`*tmp*`, "predicted", value = c(0.306726561735386, 0.306726561) > > > > Help me out > > > Xsys data has 331 rows and 18 column > > [[alternative HTML version deleted]] >This is a plain text mailing list. Please read the Posting Guide more thoroughly. -- David.> > ______________________________________________ > 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.David Winsemius Alameda, CA, USA