why the predict function does have different rows in R

0 votes
I'm using linear regression to attempt and forecast certain data. Even though there is no fault, it is strange. In order to forecast data2, which only has 1000 rows, I used a linear regression that I generated from data1, which has 4000 rows.

So, in my opinion, the variable's output should only contain 1000 rows. However, the 4000 variables never stop appearing. I have no idea what's wrong.

model = lm(train$latitude train$crashes)

Model, newdata = test, prediction1 = predict

data.frame = csv1

(Id = c(1:1000), Predicted = prediction1)

From this code, I believe it just needs to predict "test" data which has only 1000 rows. But it says that "arguments imply differing number of rows: 1000, 4337" Here the 4337 is from the train data which has 4337 rows.
Jun 23, 2022 in Data Analytics by avinash
• 1,840 points
881 views

1 answer to this question.

0 votes

Alter this sentence:

model = lm(train$latitude train$crashes) to

lm(crashes latitude, data=train) is the model.
The dataset name should ideally only be supplied once in data= when utilising the formula interface, as opposed to being specified for each variable as train$crashes, etc.

The same crashes latitude formula will be used on the new dataset that you have supplied when you execute predict with newdata=.

You should have been alarmed by the following warning I received when attempting to reproduce your problem:

data.frame with the following formulas: train = crashes = rnorm(1000), latitude = sample(1:1000), test = crashes = rnorm(4377), sample(1:4377)

lm(train$crashes train$latitude) is the model.
prediction1 <- predict(model, newdata = test)
Warning message: 'newdata' had 4337 rows but variables found have 1000 rows

Transform data into actionable insights with our Data Analyst Certification – Enroll today!

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

What does the sink function do in R?

sink diverts R output to a connection ...READ MORE

answered May 24, 2019 in Data Analytics by Chandu
1,156 views
0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
56,679 views
+1 vote
2 answers

Which function can I use to clear the console in R and RStudio ?

Description                   Windows & Linux           Mac Clear console                      Ctrl+L ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
82,841 views
0 votes
1 answer

Why should I use set.seed function() in R?

set.seed(seed) Set the seed of R‘s random number ...READ MORE

answered Apr 24, 2018 in Data Analytics by zombie
• 3,790 points
1,829 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,047 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
1,224 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,909 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,155 views
0 votes
1 answer

why the predict() function does have different rows in R

Alter this sentence: model = lm(train$latitude train$crashes) to lm(crashes ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
837 views
0 votes
1 answer

Why variance in R is different? [duplicate]

There are usually two methods for calculating ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
614 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP