Anyone know how to do a LAG regression using R? Can someone show me how to do a basic time lag correlation in R ?
Where it tries out all combinations of lags, and finds the best one.
The best one clear meaning the one with the highest correlation coefficient.
What commands do I use?
Here is a sample data where the "solution" is that you are very happy exactly 3 hours after your fitness.  
Workout hour 6, and happiness spikes in hour 9
Workout hour 15, and happiness spikes in hour 18
Hour,Fitness,Happiness
0,0,5
1,0,5
2,0,5
3,0,5
4,0,5
5,0,6
6,1,4
7,0,5
8,0,5
9,0,9
10,0,4
11,0,5
12,0,4
13,0,5
14,0,4
15,1,4
16,0,5
17,0,6
18,0,9
19,0,4
20,0,4
21,0,5
22,0,5
23,0,4
24,0,4

 A: Generally, this is not the way that one would do a time series analysis. You don't generally just throw a whole bunch of lagged variables in a regression and see which one has the largest correlation coefficient.
Time series data exhibits correlations among data points which are close together in time, violating the i.i.d. assumption of a multiple linear regression. This will be the case even if you regress on some $X_{t-i}$ term (for some arbitrary $i$ and thereby remove its variance from the error term) because correlations in time between the other variables will still remain.
This function can be used to create lagged variables and you could write a for loop to generate an arbitrary number of lags, before putting them all in a linear model and using the one that has the smallest p value. However be advised that this will generate inaccurate statistics and is not recommended.
The more rational approach is to use the a package like dynlm, auto.arima or similar to create a dynamic linear model and have a look at this link for instructions on  which commands to use (and a general idea of the modelling workflow for time series models.) Auto.arima as described in that link is probably the closest to the function you need I suspect.
